错误 ExecuteNonQuery:连接属性尚未初始化 C# (Access)

Posted

技术标签:

【中文标题】错误 ExecuteNonQuery:连接属性尚未初始化 C# (Access)【英文标题】:Error ExecuteNonQuery: Connection property has not been initialized C# (Access) 【发布时间】:2022-01-07 11:49:54 【问题描述】:

我有一些这样的代码:

var queryIns = "...";

try

    var rowInsert = u.insert(queryIns);

catch (Exception)

    var msg = "Error";
    MessageBox.Show(msg,...);

我的命令是:

public int Command(string queryCommand)

    using var conn = openConnection(); //private method that returns the connection with connection string
    using OleDbCommand cmd = getCommand(queryCommand, conn); //private method that returns the command with connection and query
    return cmd.ExecuteNonQuery();

最后是这些方法:

private OleDbCommand getCommand(string queryString, OleDbConnection conn)

    using var cmd = new OleDbCommand(queryString, conn);
    return cmd;


private OleDbConnection openConnection()

    using var conn= new OleDbConnection(connString);
    conn.Open();
    return conn;

问题是这一切都会引发异常

ExecuteNonQuery:连接属性尚未初始化

有人可以帮我T_T吗?

【问题讨论】:

问题是你在那个方法中有using var conn,它会在方法结束时关闭连接,这意味着它返回一个关闭的连接。相反,您应该保留创建连接并以一种方法一起执行查询的代码。 简化您的生活:***.com/questions/69712612/… 【参考方案1】:

你使用disposed(关闭所有资源释放)连接:

private OleDbConnection openConnection()

    // you create the connection
    using var conn= new OleDbConnection(connString); 
    conn.Open();
    return conn;
                                                    // <- and here you dispose it 

Command 完全相同的问题:您返回的cmd 已处理。

private OleDbCommand getCommand(string queryString, OleDbConnection conn)

    using var cmd = new OleDbCommand(queryString, conn); // <- command created 
    return cmd;
                                                        // <- and disposed

您可以在这两种方法中删除using

private OleDbConnection openConnection()

    // we create connection
    conn = new OleDbConnection(connString); 

    try  
      // try it to open  
      conn.Open(); 
      // return opened (and not disposed!) connection
      return conn;
    
    catch 
      // on open failure we dispose conenction (to prevent resources leakage) 
      conn.Dispose();
      // and rethrow the exception (to know the exact problem)
      throw; 
    
 

// Here we just return a command
private OleDbCommand getCommand(string queryString, OleDbConnection conn) =>
  new OleDbCommand(queryString, conn);

或者可以将方法合并为一个:

public int Command(string queryCommand) 

  using var conn = new OleDbConnection(connString); 
  conn.Open();

  using var cmd = new OleDbCommand(queryString, conn);

  return cmd.ExecuteNonQuery();
 // here both conn and cmd will be disposed

【讨论】:

好的,非常感谢。所以问题出在 getCommand() 方法的使用上,对吧? @galidan:问题出在using misusingusing 同时处理了连接和命令。应该是Command 方法Dispose 连接和命令【参考方案2】:

Using 将关闭您使用 using、connection、command、httprequest 等所使用的所有内容。

 using (var conn= new OleDbConnection(connString)) 
       using (var cmd = new OleDbCommand(queryString, conn)) 
              conn.Open();
               return cmd.ExecuteNonQuery();
         //private method that returns the command with connection and query
  ; //private method that returns the connection with connection string
   
   

【讨论】:

以上是关于错误 ExecuteNonQuery:连接属性尚未初始化 C# (Access)的主要内容,如果未能解决你的问题,请参考以下文章

ExecuteNonQuery:连接属性尚未初始化。

VB.NET SQL Server 插入 - ExecuteNonQuery:连接属性尚未初始化

C# 中的事务和 ExecuteNonQuery 错误

ExecuteNonQuery:尚未初始化Connection属性。

Dapper事务操作

OleDb 错误:“ExecuteNonQuery() 需要打开且可用的连接”[关闭]