0

我有一个程序可以测试它是否可以建立数据库连接。它可以通过 ODBC 连接到 SQL Server db,也可以通过 ODBC 连接到 Microsoft Access (.mdb)。Sql 一工作正常,但我不知道如何让访问一工作。

System.Data.Odbc.OdbcConnection odbcConn = new System.Data.Odbc.OdbcConnection();
if ({SQL})//If SQL, setup SQL server connection string
    odbcConn.ConnectionString = @"Driver={SQL Server};Server={server};Database={DB};@";Uid=     {uid};Pwd={pwd};";
else //Otherwise it's access so setup access string
    odbcConn.ConnectionString = @"Driver={Microsoft Access Driver (*.mdb)};Dbq={path}\test.mdb;Pwd={pwd};";

string testQuery = "SELECT 1";
OdbcCommand odbcComm = new OdbcCommand(testQuery);
try
{
   odbcComm.Connection = odbcConn;
   odbcConn.Open();
   odbcComm.ExecuteNonQuery();
}

这适用于 SQL 服务器。不管我做什么,我都无法使用 Access 进行连接,即使我有另一个程序只是使用该连接字符串进行访问并且工作正常,但它在这里不起作用。我尝试执行以下操作:

odbcComm.CommandText = "Select 1";
OdbcDataReader data = odbcComm.ExecuteReader();

那也没有用。我错过了什么?我必须做什么才能打开 Access 连接?它在 odbcConn.Open() 行失败。

4

1 回答 1

0

Well just figured out by own problem about 2 minutes too late. Turns out there is no 64-bit driver for Microsoft Access. By app was targeting 64-bit and so it wasn't able to find the drier. I simply changed to x86 and it worked fine.

于 2013-02-16T08:00:22.640 回答