What is the ConnectionString to create a OdbcConnection for an access mdb file

3

I want to connect from c# to an Access MDB file by using Odbc.

When I try to execute

OdbcConnection con = new OdbcConnection(
    "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\temp\\test.mdb;");

I get the following exception:

exc {System.Data.Odbc.OdbcException (0x80131937): ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
   at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
   at System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection connection, OdbcConnectionString constr, OdbcEnvironmentHandle environmentHandle)
   at System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.Odbc.OdbcConnection.Open()
   :

What's the Problem here? It complains about "Data source name not found and no default driver specified", but I specified a Driver?

I got the connectionstring with help from here http://www.connectionstrings.com/access#net-framework-data-provider-for-odbc

c#
.net
ms-access
odbc
connection-string
asked on Stack Overflow Jan 28, 2013 by marc40000 • edited Jan 28, 2013 by Olivier Jacot-Descombes

3 Answers

3

Oh, I just found the source of my Problem. I think others might encounter it as well:

I'm writing a .net application which runs in the 32bit .net vm when the OS is a 32bit Windows, and in the 64bit vm when the OS is a 64bit Windows. Using the 2 odbcad32.exe in syswow and system32 (yes both are named odbcad32.exe even though the one in system32 is for 64bit, and yes that's correct as well) I found out I only have the Access MdB Driver installed for odbc32 bit. So when my application runs on a 64bit Windows, .net wants to use the 64bit Version of odbc and doesn't find the Driver.

Ok, so now I need a 64 bit Access Driver, which does not exist according to this enter link description here. Ok the post is old, so maybe there exists one now?

When I force the Plattform in my Project Settings to x86, it works. Of course, this also forces my app to be run in the 32bit .net vm.

answered on Stack Overflow Jan 28, 2013 by marc40000
0

Use web config setting like example below

<add      name="ODBCDataConnectionString"   connectionString="Driver=ODBCDriver;server=ODBCServer;"   providerName="System.Data.Odbc"   />
answered on Stack Overflow Jan 28, 2013 by Diggie
0

You can create a udl file, create your connection, and then open the file in notepad to see the connection string. This has helped me out in the past -

http://msdn.microsoft.com/en-us/library/e38h511e%28v=vs.71%29.aspx

Using this, you can create a regular connection like you would in odbc tool and verify that it works.

Hope that helps.

answered on Stack Overflow Jan 28, 2013 by czuroski

User contributions licensed under CC BY-SA 3.0