Microsoft Access 2010 .accdb 的 SQL 连接字符串

发布于 2024-12-09 16:34:25 字数 1034 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤者何惧 2024-12-16 16:34:25

对于 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")

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:\BC207\test.accdb")
青衫负雪 2024-12-16 16:34:25

编辑:正如所指出的,对于访问应该使用OleDbConnection,而不是SqlConnection...

您可以使用更紧凑的方式,也可以通过使用 using 语句,确保在任何可能的情况下关闭并处理连接,即使抛出异常也是如此:

您的查询文本也可能是错误的,正如其他人所建议的那样...

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());
}

编辑:< /strong> 您确定表 HAHA 只包含一行吗?因为 ExecuteScalar 仅返回一个值,所以如果您想从许多记录中获取 1 列,您可以使用 DataReader 或 DataSet...

Edit: as pointed out, for access OleDbConnection should be used, not 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:

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

与酒说心事 2024-12-16 16:34:25
comm.CommandText = "SELECT HAHA(*) FROM password";

这是错误的。

“从哈哈中选择密码”

comm.CommandText = "SELECT HAHA(*) FROM password";

It´s wrong.

"SELECT password FROM HAHA"

灯角 2024-12-16 16:34:25

您的 SQL 语句应该是,

SELECT * from HAHA

 SELECT [Password] From HAHA

编辑:

您应该更改 ConnectionString

Your SQL statement should be,

SELECT * from HAHA

OR

 SELECT [Password] From HAHA

EDIT:

You should change the ConnectionString.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文