2011-12-03 88 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

相关问题