开发者

MS Access Connectionstring problem C#

开发者 https://www.devze.com 2023-02-02 08:59 出处:网络
I have the following code mycon=new SqlConnection(); mycon.ConnectionString=\"\'Provider =Microsoft.ACE.OLEDB.12.0\';Data Source=\'G:\\\\Abbriviations\\\\Abbriviations\\\\App_Data\\\\abbreviations.ac

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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消