[英]SQL connection string for microsoft access 2010 .accdb
I am doing a simple login form using winforms and access 2010 database (.accdb) in C#. 我正在使用winforms进行简单的登录表单,并在C#中访问2010数据库(.accdb)。
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??
我试过搜索,发现.Jet是用于访问07 ?? but this doesnt seem to work too.
但这似乎也没有用。 i am an amateur at databases (code referred from msdn).
我是数据库的业余爱好者(代码来自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. 编辑:表的名称是密码,我想要获取值的字段是ID。
SQL statement i wrote it as : SELECT ID FROM password
SQL语句我把它写成:
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.. 所以我想我有一个错误的连接字符串..
对于Acces数据库(.mdb,.accdb等...),您希望使用OleDbConnection ,而不是SqlConnection(SQL Server),如下所示:
conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BC207\test.accdb")
Edit : as pointed out, for access OleDbConnection
should be used, not SqlConnection
... 编辑 :正如所指出的,对于访问应该使用
OleDbConnection
,而不是SqlConnection
...
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: 您可以使用更紧凑的方式,并确保连接被关闭并在任何可能的情况下处理,即使抛出异常,也可以使用
using
语句:
your query text was also, probably wrong as others have suggested... 您的查询文本也可能是其他人建议的错误...
using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\BC207\test.accdb"))
using (var comm = conn.CreateCommand())
{
comm.CommandText = "SELECT password FROM HAHA";
comm.CommandType = CommandType.Text;
conn.Open();
var returnValue = comm.ExecuteScalar();
MessageBox.Show(returnValue.ToString());
}
Edit: are you sure the table HAHA only contains one row? 编辑:你确定表HAHA只包含一行吗? 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...
因为ExecuteScalar只返回一个值,如果你想获得1列但是从许多记录中你可以使用DataReader或DataSet ......
comm.CommandText = "SELECT HAHA(*) FROM password";
It´s wrong. 这是不对的。
"SELECT password FROM HAHA" “从HAHA中选择密码”
Your SQL statement should be, 你的SQL语句应该是,
SELECT * from HAHA
OR 要么
SELECT [Password] From HAHA
EDIT: 编辑:
You should change the ConnectionString . 您应该更改ConnectionString 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.