如何连接到使用C#中的MS Access文件(MDB)?

如何连接到使用C#中的MS Access文件(MDB)?

问题描述:

我试图连接到一个mdb文件,我知道,我需要 Microsoft.OLEDB.JET.4.0 数据提供者。不幸的是,我没有它(大学)计算机上安装。
因为,他们不提供供应商,我认为应该有一个绕方式。

I'm trying to connect to a mdb file and I understand that I would need Microsoft.OLEDB.JET.4.0 data provider. Unfortunately, I do not have it installed on the (University) machine. Since, they don't provide that provider, I believe there should be a way around.

我怎么可以连接到文件而不 Microsoft.OLEDB.JET.4.0 或有任何替代?

How can I connect to the file without Microsoft.OLEDB.JET.4.0 or is there any alternative ?

我有以下提供:

我一直在使用的OLE DB提供程序Microsoft目录服务,试过它在测试方面,我得到'测试成功,但有些设置没有被提供者所接受。我把这个字符串,反正用它,我得到了 ADsDSOObject失败,没有可用的错误信息,结果code:DB_E_ERRORSINCOMMAND(0x80040E14)

I have tried using OLE DB Provider for Microsoft Directory Services, to which while testing connection, I get 'Test succeeded but some settings were not accepted by the provider'. I took that string and used it anyway and I got ADsDSOObject' failed with no error message available, result code: DB_E_ERRORSINCOMMAND(0x80040E14).

连接最简单的方式是通过OdbcConnection使用code这样的

The simplest way to connect is through an OdbcConnection using code like this

using System.Data.Odbc;

using(OdbcConnection myConnection = new OdbcConnection())
{
    myConnection.ConnectionString = myConnectionString;
    myConnection.Open();

    //execute queries, etc

}

其中myConnectionString是这样的

where myConnectionString is something like this

myConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};" + 
"Dbq=C:\mydatabase.mdb;Uid=Admin;Pwd=;

请参阅的ConnectionStrings

在替代你可以创建一个DSN,然后使用该DSN在连接字符串中

In alternative you could create a DSN and then use that DSN in your connection string


  • 打开控制面板 - 管理工具 - ODBC数据源
    经理

  • 转至系统DSN页面,并添加一个新的DSN

  • 选择Microsoft Access驱动程序(* .mdb)和preSS END

  • 将DSN(选择MyDSN在这个例子中)的名称

  • 要使用
  • 选择数据库

  • 尝试紧凑型或恢复命令,看是否连接正常工作

现在您的connectionString可以写成这样

now your connectionString could be written in this way

myConnectionString = "DSN=myDSN;"