Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

c# - SQL connection string for microsoft access 2010 .accdb

I am doing a simple login form using winforms and access 2010 database (.accdb) in C#.

I have the following code and it seems that the connection string is wrong. I have tried searching and found that .Jet is for access 07?? but this doesnt seem to work too. i am an amateur at databases (code referred from msdn). I am having trouble understand which should i use for this example too.

access table name: haha

ID (PK)  |   password
-----------------------
   1     |   testing
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:BC207est.accdb");
        System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand();

        comm.CommandText = "SELECT HAHA(*) FROM password";
        comm.CommandType = CommandType.Text;
        comm.Connection = conn;

        conn.Open();

        Object returnValue = comm.ExecuteScalar();
        conn.Close();

        MessageBox.Show((string)returnValue);

edited: the table's name is password, and the field that i want to get the value is ID.

SQL statement i wrote it as : SELECT ID FROM password

and yes, only one record in only one field in the table as the primary key.

anyway the problem is that the program hangs upon execution on the first line
-> Keyword not supported: 'provider'.

so i figured that I have a wrong connection string..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Acces databases (.mdb, .accdb, etc...), you want to use OleDbConnection, not SqlConnection (SQL Server), like this:

conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:BC207est.accdb")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...