0
votes

I have a program in which it tests to see if it can make a Database connection. It either connects to a SQL Server db via ODBC or a Microsoft Access (.mdb) via ODBC. The Sql one works fine but I can't not figure out how to get the access one to work.

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();
}

This works fine for SQL server. No matter what I do though, I can't get it to connect using Access, even though I have another program that simply uses that connection string for access and works fine as well, but it won't work here. I tried doing the following:

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

and that didn't work either. What am I missing? What do I have to do to open an Access connection? It fails right at the odbcConn.Open() line.

1

1 Answers

0
votes

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.