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
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.
Use web config setting like example below
<add name="ODBCDataConnectionString" connectionString="Driver=ODBCDriver;server=ODBCServer;" providerName="System.Data.Odbc" />
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.
User contributions licensed under CC BY-SA 3.0