2012-03-07 105 views
0

连接总是超时,即使它在trycatch内也不会生成错误消息。我怀疑连接字符串有问题。这是我目前有:使用SqlDataReader连接到本地.mdb文件的正确方法?

  string path = @"C:\PATH\TO\wantedDB.mdb"; 

      if (!File.Exists(path)) 
       throw new FileNotFoundException("File not found."); 
      else 
       Console.WriteLine("File found."); // File is found, so nothing wrong with that. 

      string connectionstring = "Database=wantedDB;AttachDBFilename=" + 
      path + ";Server=(local)"; 

      using (SqlConnection conn = new SqlConnection(connectionstring)) 
      { 
       Console.WriteLine("Opening connection..."); 
       conn.Open(); 
       Console.WriteLine("Connection opened."); // Program never gets here. 

我也尝试了关系路径的连接字符串,如:

string connectionstring = "Database=wantedDB;AttachDBFilename=\"wantedDB.mdb\";Server=(local)"; 

的DB是不是受密码保护。我有MS Access安装,这是否影响这个莫名其妙?我错过了什么?

+0

有什么问题是什么呢?任何错误消息? – ChrisWue 2012-03-07 05:34:59

+1

[SQLConnection.Open();抛出异常](http://stackoverflow.com/questions/2398628/sqlconnection-open-throwing-exception) – ChrisWue 2012-03-07 05:37:15

+0

我编辑了问题的开始。 – hannu40k 2012-03-07 05:37:46

回答

1

连接到您应该使用OLEDB连接一个mdb文件:

var con = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=C:\\wantedDB.mdb;"); 
相关问题