Tassadaque
Tassadaque

Reputation: 8199

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();
            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

Upvotes: 0

Views: 9807

Answers (1)

Fredrik Mörk
Fredrik Mörk

Reputation: 158289

Two things:

  1. 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 an OleDbConnection instead. This also means that you should use an OleDbDataAdapter instead of an SqlDataAdapter.
  2. 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).

Upvotes: 2

Related Questions