Microsoft Access 2010 .accdb 的 SQL 连接字符串
我正在使用 winforms 制作一个简单的登录表单,并在 C# 中访问 2010 数据库 (.accdb)。
我有以下代码,看来连接字符串是错误的。我尝试搜索并发现.Jet用于访问07?但这似乎也不起作用。 我是数据库的业余爱好者(代码引用自msdn)。我也无法理解这个例子应该使用哪个。
访问表名:哈哈
ID (PK) | password ----------------------- 1 | testing
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BC207\test.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);
编辑:表名是password,我要获取值的字段是ID。
SQL语句我写成:SELECT ID FROM password
是的,表中只有一个字段中只有一条记录作为主键。
无论如何,问题是程序在第一行执行时挂起
<代码>->不支持关键字:'provider'。
所以我认为我有一个错误的连接字符串..
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:\BC207\test.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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Acces 数据库(.mdb、.accdb 等),您需要使用 OleDbConnection,而不是 SqlConnection (SQL Server),如下所示:
For Acces databases (.mdb, .accdb, etc...), you want to use OleDbConnection, not SqlConnection (SQL Server), like this:
编辑:正如所指出的,对于访问应该使用
OleDbConnection
,而不是SqlConnection
...您可以使用更紧凑的方式,也可以通过使用
using
语句,确保在任何可能的情况下关闭并处理连接,即使抛出异常也是如此:您的查询文本也可能是错误的,正如其他人所建议的那样...
编辑:< /strong> 您确定表 HAHA 只包含一行吗?因为 ExecuteScalar 仅返回一个值,所以如果您想从许多记录中获取 1 列,您可以使用 DataReader 或 DataSet...
Edit: as pointed out, for access
OleDbConnection
should be used, notSqlConnection
...you can use a much more compact way and also be sure connection is closed and disposed in any possible case even when exceptions are thrown, by using the
using
statements:your query text was also, probably wrong as others have suggested...
Edit: are you sure the table HAHA only contains one row? Because the ExecuteScalar returns only one value, if you want to get 1 column but from many records you could use a DataReader or a DataSet...
这是错误的。
“从哈哈中选择密码”
It´s wrong.
"SELECT password FROM HAHA"
您的 SQL 语句应该是,
或
编辑:
您应该更改 ConnectionString。
Your SQL statement should be,
OR
EDIT:
You should change the ConnectionString.