Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...