MS Access Connectionstring problem C#
I have the following code
mycon=new SqlConnection();
mycon.ConnectionString="'Provider =Microsoft.ACE.OLEDB.12.0';Data Source='G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb'";
myds=new DataSet();
mytable = new DataTable("Abbriviations");
myds.Tables.Add(mytable);
myadap=new SqlDataAdapter();
I am getting the following error.
Format of the initialization string does not conform to specification starting at index 0.
Can you please guide me the correct connectionstring for this.
Thanks Edit
public class dataManipulationClass
{
public OleDbConnection mycon;
public DataSet myds;
public DataTable mytable;
public SqlDataAdapter myadap;
public OleDbCommand mycomm;
public bool ManupulateData()
{
mycon = new OleDbConnection();
mycon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb";
myds=new DataSet();
mytable = new DataTable("Abbriviations");
myds.Tables.Add(mytable);
myadap=new SqlDataAdapter();
mycomm=new OleDbCommand();
开发者_Go百科 mycomm.CommandType=CommandType.Text;
mycomm.CommandText = "SELECT * FROM Abbriviations";
mycomm.Connection=mycon;
myadap.SelectCommand=mycomm;
return true;
}
}
Now i am getting the following error at mycommm.
Cannot implicitly convert type 'System.Data.OleDb.OleDbCommand' to 'System.Data.SqlClient.SqlCommand'
Thanks
Two things:
- You are using the wrong connection object;
SqlConnection
is for communicating with SQL Server databases, while you are trying to talk to an MS Access database. Try using anOleDbConnection
instead. This also means that you should use anOleDbDataAdapter
instead of anSqlDataAdapter
. - The string looks a bit odd with extra quotation marks and spaces.
Try this instead:
mycon=new OleDbConnection();
mycon.ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\\Abbriviations\\Abbriviations\\App_Data\\abbreviations.accdb";
// and then the rest of your code
As a side note, connectionstrings.com is a great resource for this kind of info (there is a page for Access connectionstrings).
精彩评论