简体   繁体   English

OLEDB连接到Access数据库(accdb)

[英]OLEDB connection to Access Database (accdb)

I want to make a simple application for an exercise, so it could be nice to connect to a simple database like Access (.accdb) 我想为一个练习做一个简单的应用程序,所以连接到像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: 我使用Visual Studio 2010.当我使用内置调试模式“Start without Debugging”(CTRL + F5)测试我的应用程序时,我收到此错误:

The 'Microsoft.ACE.OLEDB.14.0' provider is not registered on the local machine. “Microsoft.ACE.OLEDB.14.0”提供程序未在本地计算机上注册。

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 我曾尝试从Microsoft omepage下载并安装“Microsoft Access Database Engine 2010 Redistributable”(64位): 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. 执行myAdapptor.Fill()时仍然出现错误。 What is wrong? 怎么了?

您需要Access 2007 Runtime

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 对于对我的解决方案感兴趣的其他人,我发现Access 2010不支持Microsoft.ACE.OLEDB.14.0。相反,我使用了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 您可以从此站点下载其“2007 Office System驱动程序:数据连接组件”: 2007 Office System驱动程序:数据连接组件

Had a similar problem but just a plan old mdb in my case. 有一个类似的问题,但在我的情况下只是一个计划旧的mdb。 Provider=Microsoft.Jet.OLEDB.4.0 does the trick for that, no need to download any extra runtimes. Provider = Microsoft.Jet.OLEDB.4.0可以解决这个问题,无需下载任何额外的运行时。

add using System.Data.OleDb library. 使用System.Data.OleDb库添加。

now for the connection string 现在为连接 字符串

OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Aishwar NIGAM\\Documents\\indianOil.accdb");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM