microsoft access 2010 .accdb 的 SQL 连接字符串 [英] SQL connection string for microsoft access 2010 .accdb

查看:34
本文介绍了microsoft access 2010 .accdb 的 SQL 连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 winforms 做一个简单的登录表单,并在 C# 中访问 2010 数据库 (.accdb).

我有以下代码,似乎连接字符串是错误的.我尝试搜索并发现 .Jet 用于访问 07??但这似乎也不起作用.我是数据库的业余爱好者(从 msdn 引用的代码).我也无法理解我应该在此示例中使用哪个.

访问表名:哈哈

<前>身份证(PK)|密码-----------------------1 |测试

 System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:BC207	est.accdb");System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand();comm.CommandText = "SELECT HAHA(*) FROM 密码";comm.CommandType = CommandType.Text;comm.Connection = conn;conn.Open();对象 returnValue = comm.ExecuteScalar();conn.Close();MessageBox.Show((string)returnValue);

表名是密码,我想取值的字段是ID.

SQL 语句我写成:SELECT ID FROM password

是的,表中只有一个字段中的一条记录作为主键.

无论如何问题是程序在第一行执行时挂起
<代码>->不支持关键字:'provider'.

所以我想我有一个错误的连接字符串..

解决方案

对于 Acces 数据库(.mdb、.accdb 等...),您希望使用 OleDbConnection,不是SqlConnection(SQL Server),像这样:

conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:BC207	est.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:BC207	est.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..

解决方案

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	est.accdb")

这篇关于microsoft access 2010 .accdb 的 SQL 连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆