2011-12-03 103 views
1

我在這裏做錯了什麼?OleDbConnection - ExecuteNonQuery需要一個開放且可用的Connection。連接的當前狀態已關閉

using System.Data; 
using System.Data.OleDb; 

namespace myProject.Account 
{ 
    public class DbManager 
    { 

     private OleDbConnection OpenDbConnection() 
     { 
      string connectionString = GetConnectionString(); 
      return new OleDbConnection {ConnectionString = connectionString}; 
     } 

     private string GetConnectionString() 
     { 
      return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myDataBase.accdb"; 
     } 

     public void InsertUser(string name, string loginName, string password) 
     { 
      OleDbConnection conn = OpenDbConnection(); 

      OleDbCommand command = new OleDbCommand(
       "INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)", 
       Conn); 

      command.Parameters.Add("@name", OleDbType.VarChar).Value = name; 
      command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName; 
      command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password; 
      command.ExecuteNonQuery(); 
     } 
    } 
} 

我得到這個錯誤:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

Source Error:

Line 31: command.ExecuteNonQuery();

我也想看看一些其他的線程,但沒有幫助:

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed

MS Access DB doesnt save changes after execution (C#)

+1

我建議將連接放置在'using'塊中以釋放資源。 –

+4

當我真的只創建數據庫連接時,調用一個方法'OpenDbConnection'似乎非常「危險」,但是**實際上並沒有**打開它!種類違背了「最少驚喜的原則」....要麼實際**打開**在該方法中的連接,或稱之爲「CreateDbConnection」或什麼.... –

+0

謝謝Uwe凱姆 - 我很新C#對於其他人感興趣,爲什麼使用「使用」看到線程:http://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp – Jedi

回答

2

OleDbConnection conn = OpenDbConnection(); 

添加

conn.Open(); 

另外,修改OpenDbConnection如下:

private OleDbConnection OpenDbConnection() 
    { 
     string connectionString = GetConnectionString(); 

     OleDbConnection conn = new OleDbConnection {ConnectionString = connectionString}; 

     conn.Open(); 

     return conn; 
    } 
+0

感謝您的幫助。 – Jedi

+0

經過一些調試後,我發現INSERT語句中的「Password」是SQL或ACCESS中的保留字。執行語句時出現語法錯誤。當我將「密碼」更改爲「UserPwd」時,聲明經過了:-) – Jedi

+0

其實我在你的代碼片段中做了你的工作 - 謝謝 – Jedi

2

你似乎忘記打開您的連接(並分配錯誤連接到命令)。試試:

 using(OleDbConnection conn = OpenDbConnection()) 
     { 
      using(OleDbCommand command = new OleDbCommand( 
       "INSERT INTO tblUser (UserName, LoginName, Password) VALUES (@name,@login,@pwd)")) 
      { 
      command.CommandType = CommandType.Text; 
      command.Parameters.Add("@name", OleDbType.VarChar).Value = name; 
      command.Parameters.Add("@login", OleDbType.VarChar).Value = loginName; 
      command.Parameters.Add("@pwd", OleDbType.VarChar).Value = password; 
      command.Connection = conn; 

      conn.Open(); 

      command.ExecuteNonQuery(); 
      } 
     } 

取而代之。我也推薦使用using語句。它會管理你的connection.close給你。

+0

感謝您的回答。我用的是sports_tech,但這個也是一個很好的例子:-) – Jedi

+0

一定要接受他們的回答吧!就我個人而言,我會將您的方法的名稱更改爲GetDbConnection,並且只在我打算按照上述方式使用它時打開()連接。我還會將它封裝在using語句中,以確保即使存在異常,連接也會關閉並處理。 – dash

相關問題