简体   繁体   中英

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

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM