简体   繁体   中英

“Data source name not found and no default driver specified” for creating access connection

This is my connection to an access database in .NET:

OdbcConnection conn = new OdbcConnection("Driver={Microsoft Access Driver (*.mdb)};Dbq=" + path + "\\Access.mdb;Uid=;Pwd=;");

And I got this problem:

base {System.Data.Common.DbException} = {"ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified"}

I have tried couple of database connection strings from here: http://www.connectionstrings.com/access but none of them working.

Any suggestions for this?

Thanks in advance.

There is an easy way to get the connection string, try it as follow:

  • Create a text file and change it's extension to .udl .
  • Open the new file by double click it and choose to open it with any text editor, notepad for example.
  • In the opened window, choose your provider and your database and click OK.
  • Open this file with notepad as a text and you will find the connectoin string inside it.

See This for more details.

as you can see in the same site you have linked above, the default way to connect to access database is specifying the Microsoft.Jet.OLEDB.4.0 Provider:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;

does this work and if not what kind of errors do you get?

web.config add

add name="odbcConnectionString"
    connectionString="Driver={Microsoft Access Driver (*.mdb)};DBQ=|DataDirectory|info.mdb; "
    providerName="System.Data.OleDb"

First, is path a valid field?

Second, try to output the String you are using to connect with the database file, as a sanity check. Make sure the output string matches what you are expecting, and that the file specified exists.

Try this

http://www.connectionstrings.com/

More specifically this one

http://www.connectionstrings.com/access

I would change your code to the following:

OdbcConnectionStringBuilder sb = new OdbcConnectionStringBuilder();
sb.Driver = "Microsoft Access Driver (*.mdb)";
sb.Add("Dbq", "C:\\info.mdb");
sb.Add("Uid", "Admin");
sb.Add("Pwd", "pass!word1");
OdbcConnection con = new OdbcConnection(sb.ConnectionString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM