OLEDB 连接到 Access 数据库 (accdb)
我想为练习制作一个简单的应用程序,因此连接到像 Access (.accdb) 这样的简单数据库可能会很好。
我的程序如下所示:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
namespace myProject.Account
{
public class DbManager
{
private OleDbConnection _dbConnection;
public void OpenDbConnection()
{
_dbConnection = new OleDbConnection {ConnectionString = GetConnectionString()};
}
private string GetConnectionString()
{
return "Provider=Microsoft.ACE.OLEDB.14.0;Data Source=exercise1.accdb";
}
public void CloseDbConnection()
{
_dbConnection.Close();
}
public void GetUser()
{
DataSet myDataSet = new DataSet();
var myAdapptor = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand("SELECT * FROM tblUser", _dbConnection);
myAdapptor.SelectCommand = command;
myAdapptor.Fill(myDataSet, "tblUser");
}
}
}
我使用 Visual Studio 2010。 当我使用内置调试模式“启动而不调试”(CTRL+F5) 测试我的应用程序时,出现此错误:
“Microsoft.ACE.OLEDB.14.0”提供程序未在本地计算机上注册。
我尝试从 Microsoft omepage 下载并安装“Microsoft Access Database Engine 2010 Redistributable”(64 位): http://www.microsoft.com/download/en/details.aspx?id=13255
不幸的是它没有解决问题。 执行 myAdapptor.Fill() 时我仍然收到错误。 怎么了?
I want to make a simple application for an exercise, so it could be nice to connect to a simple database like Access (.accdb)
My program looks like this:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
namespace myProject.Account
{
public class DbManager
{
private OleDbConnection _dbConnection;
public void OpenDbConnection()
{
_dbConnection = new OleDbConnection {ConnectionString = GetConnectionString()};
}
private string GetConnectionString()
{
return "Provider=Microsoft.ACE.OLEDB.14.0;Data Source=exercise1.accdb";
}
public void CloseDbConnection()
{
_dbConnection.Close();
}
public void GetUser()
{
DataSet myDataSet = new DataSet();
var myAdapptor = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand("SELECT * FROM tblUser", _dbConnection);
myAdapptor.SelectCommand = command;
myAdapptor.Fill(myDataSet, "tblUser");
}
}
}
I using Visual Studio 2010.
When I test my application by using the built in debug mode "Start without Debugging" (CTRL+F5) I get this error:
The 'Microsoft.ACE.OLEDB.14.0' provider is not registered on the local machine.
I have tried to download and install "Microsoft Access Database Engine 2010 Redistributable" (64 bit) from Microsoft omepage: http://www.microsoft.com/download/en/details.aspx?id=13255
Unfortunately it sah not solved the problem.
I still got the error when the myAdapptor.Fill() is executed.
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要Access 2007 Runtime。
You need the Access 2007 Runtime.
使用 System.Data.OleDb 库添加。
现在是连接 字符串
add using
System.Data.OleDb
library.now for the connection string
对于对我的解决方案感兴趣的其他人,我发现 Access 2010 不支持 Microsoft.ACE.OLEDB.14.0。相反,我使用了 Microsoft.ACE.OLEDB.12.0
您可以下载他们的“2007 Office System 驱动程序:数据连接组件” ”来自此站点:2007 Office 系统驱动程序:数据连接组件
For others that are interested in my solution, I figured out that Microsoft.ACE.OLEDB.14.0 is not supported for Access 2010. Instead I used Microsoft.ACE.OLEDB.12.0
You can download their "2007 Office System Driver: Data Connectivity Components" from this site: 2007 Office System Driver: Data Connectivity Components
有类似的问题,但在我的情况下只是一个旧的 mdb 计划。 Provider=Microsoft.Jet.OLEDB.4.0 就可以做到这一点,无需下载任何额外的运行时。
Had a similar problem but just a plan old mdb in my case. Provider=Microsoft.Jet.OLEDB.4.0 does the trick for that, no need to download any extra runtimes.