2012-08-17 65 views
0

這是代碼,我寫了添加一些文字手風琴面板上的按鈕點擊:無法訪問使用asp.net Microsoft Access數據庫和C#

protected void Button1_Click1(object sender, EventArgs e) 
    { 
     //Use a string variable to hold the ConnectionString. 
     string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;" 
      + "Data Source=D:\\C#Samples\\StudentDetails\\WebRole1\\App_Data\\Students1.accdb"; 
     System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection(); 
     cn.ConnectionString = connectString; 
     //Create an OleDbConnection object, and then pass in the ConnectionString to the constructor. 
     //OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString); 
     try 
     { 
      //Open the connection. 
      cn.Open(); 
     } 
     catch (Exception ex) 
     { 
      AccordionPane1.Controls.Add(new LiteralControl("Open Error")); 
     } 

      string selectString = "SELECT * FROM BasicInfo"; 

      //Create an OleDbCommand object. 
      //Notice that this line passes in the SQL statement and the OleDbConnection object 
      OleDbCommand cmd = new OleDbCommand(selectString, cn); 

      //Send the CommandText to the connection, and then build an OleDbDataReader. 
      //Note: The OleDbDataReader is forward-only. 

      try 
      { 
       OleDbDataReader reader=null; 
       try 
       { 
        reader = cmd.ExecuteReader(); 
       } 
       catch (Exception es) 
       { 
        AccordionPane1.Controls.Add(new LiteralControl(" datareader")); 
       } 
       string s = "s"; 
       reader.Read(); 
       s = reader["S_No"].ToString(); 

       AccordionPane1.Controls.Add(new LiteralControl(s)); 
       //Close the reader and the related connection. 
       reader.Close(); 
       cn.Close(); 
      } 
      catch (Exception ex) 
      { 
       AccordionPane1.Controls.Add(new LiteralControl(" Read Error")); 
      } 
    } 

我有我的Access 2007數據庫文件夾中的我在connectString中指定。當我在瀏覽器中查看時,在按鈕上單擊我得到三個例外: enter image description here

打開數據庫時可能會出現什麼問題?我是否需要進行其他更改?

+2

您能否提供例外的文字? – dantix 2012-08-17 14:19:18

+0

你先生正在吞嚥異常,這就是爲什麼你不能調試,並期望我們玩猜謎遊戲。 – banging 2012-08-17 14:21:57

回答

2

變化

提供商= Microsoft.Jet.OLEDB.4.0;

商= Microsoft.ACE.OLEDB.12.0

Provider=Microsoft.ACE.OLEDB.12.0;" 
      + "Data Source=D:\\C#Samples\\StudentDetails\\WebRole1\\App_Data\\Students1.accdb 

希望這將解決這一問題。

+0

我認爲這對於訪問2007年,但不知道這一點.. – 2012-08-17 14:23:37

+0

是的,你是對的 – MMK 2012-08-17 14:24:39

+0

上述連接字符串是正確的.accdb格式(2007/2010),這是OP使用。 – Fionnuala 2012-08-17 14:29:36

1

你的連接字符串可能isssue

string ConnStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\abc.mdb;"; 

OleDbConnection MyConn = new OleDbConnection(ConnStr); 

這個的原因爲2007年的訪問也檢查數據庫的路徑是cocrect。

1

可以使用|DataDirectory|real path代替,你必須改變Provider=Microsoft.ACE.OLEDB.12.0(由@MMK的建議)

string connectString = @"Microsoft.ACE.OLEDB.12.0; 
     Data Source=|DataDirectory|\Students1.accdb;Persist Security Info=False;"; 

,並一直使用using塊,妥善處置IDisposable的對象。

using(OleDbConnection cn=new OleDbConnection()) 
{ 
using(OleDbCommand cmd=new OleDbCommand()) 
{ 
    cn.ConnectionString=connectionString; 
    cmd.CommandText=selectString; 
    cmd.Connection=cn; 
    ... 
} 
} 
+0

謝謝。但我沒有理解爲什麼我們應該使用「使用」。你可以請解釋我 – Pramod 2012-08-17 15:32:11

+0

@Pramod - 閱讀 - [什麼是C#使用塊,爲什麼我應該使用它?](http://stackoverflow.com/questions/212198/what-is-the-c-sharp-using - 嵌段和爲什麼,應該-I的用它) – adatapost 2012-08-18 01:31:01

相關問題