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

丶灬走出姿态 提交于 2019-12-20 02:47:20

问题


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.


回答1:


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.




回答2:


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?




回答3:


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.




回答4:


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



回答5:


web.config add

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


来源:https://stackoverflow.com/questions/8201489/data-source-name-not-found-and-no-default-driver-specified-for-creating-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!