2012-03-07 59 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;"); 
相關問題