简体   繁体   English

C# SQL 查询 - ExecuteNonQuery:连接属性尚未初始化

[英]C# SQL Query - ExecuteNonQuery: Connection property has not been initialized

I have a number of blocks of code in my Windows Application that use the same structure to execute queries.我的 Windows 应用程序中有许多代码块使用相同的结构来执行查询。 After adding in a few new things to my code, these no longer work due to the error:在我的代码中添加了一些新内容后,由于错误,这些内容不再起作用:

"ExecuteNonQuery: Connection property has not been initialized" “ExecuteNonQuery:连接属性尚未初始化”

Blocks of code all look like this:代码块都如下所示:

sc.Open();
cmd = new SqlCommand("UPDATE bin SET serialNumber=" + tb_computername.Text + " WHERE binNumber=" + binNumber);
cmd.ExecuteNonQuery();
sc.Close();
break;

The new code does this:新代码执行以下操作:

//Find Open BIN
int binNumber = 0;
int binIndex = 0;
string queryString = "SELECT * FROM bin";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, scb);
DataSet binNumbers = new DataSet();
adapter.Fill(binNumbers, "bin");
for (int i = 0; i < 150; i++)
{
    binNumber++;                    
    if(binNumbers.Tables["bin"].Rows[binIndex]["serialNumber"].ToString() == "")
{
sc.Open();
cmd = new SqlCommand("UPDATE bin SET serialNumber=" + tb_computername.Text + " WHERE binNumber=" + binNumber);
cmd.ExecuteNonQuery();
sc.Close();
break;
}
binIndex++;

The connections for these are defined at the top of the class.这些连接在类的顶部定义。

You need to assign it a SqlConnection object.您需要为其分配一个SqlConnection对象。

 cmd.Connection = connection;

Where connection is a SqlConnection object with your connection string etc.其中connection是一个带有连接字符串等的SqlConnection对象。

Also for good practice you should wrap it in a using :同样为了良好的实践,您应该将其包装在using

 using (SqlConnection connection = new SqlConnection("ConnectionString")) { 
     cmd.Connection = connection;
 } 

And paramerterized queries to prevent SQL Injection attacks.以及参数化查询以防止 SQL 注入攻击。

We need to pass sqlconnection object to sqlcommand object, before executing it.在执行之前,我们需要将 sqlconnection 对象传递给 sqlcommand 对象。

Sqlcommand has has following constructors constructor: Sqlcommand 具有以下构造函数构造函数:

  1. SqlCommand() SqlCommand()
  2. SqlCommand(String) SqlCommand(字符串)
  3. SqlCommand(String, SqlConnection) SqlCommand(字符串,SqlConnection)
  4. SqlCommand(String, SqlConnection, SqlTransaction) SqlCommand(字符串、SqlConnection、SqlTransaction)
  5. SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting) SqlCommand(String, SqlConnection, SqlTransaction, SqlCommandColumnEncryptionSetting)

If we are using the 1. default constructor or 2. paramterized constructor with one parameter(query), then we need to set connection as如果我们使用 1. 默认构造函数或 2. 带一个参数(查询)的参数化构造函数,那么我们需要将连接设置为

   SqlCommand.Connection = SqlConnection;

Below is the working code snippet:下面是工作代码片段:

   //create a connection object
  using (SqlConnection connection = new SqlConnection(connectionString))
    {
     //create command object, and pass your string query & connection object.
     //we can call the default constructor also and later assign these values
       SqlCommand command = new SqlCommand(queryString, connection);   
    //open the connection here,
      command.Connection.Open();
    //execute the command.
      command.ExecuteNonQuery();
    }

To ensure that connections are always closed, we should open the connection inside of a using block, to ensure that the connection is automatically closed when the code exits the block.为确保连接始终关闭,我们应该在 using 块内部打开连接,以确保代码退出块时自动关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 ExecuteNonQuery:连接属性尚未初始化 C#(访问) - Error ExecuteNonQuery: Connection property has not been initialized C# (Access) SQL Server错误:ExecuteNonQuery:连接属性尚未初始化 - SQL Server error: ExecuteNonQuery: Connection property has not been initialized 异常错误“ ExecuteNonQuery:连接属性尚未初始化。”这是我的C#编码 - exception error “ExecuteNonQuery: Connection property has not been initialized.” this is my C# Coding ExecuteNonQuery:连接属性尚未初始化 - ExecuteNonQuery: Connection property has not been initialized &#39;ExecuteNonQuery:连接属性尚未初始化。&#39; - 'ExecuteNonQuery: Connection property has not been initialized.' ExecuteNonQuery:连接属性尚未初始化 - ExecuteNonQuery: Connection property has not been initialized “ ExecuteNonQuery:连接属性尚未初始化。” - “ExecuteNonQuery: Connection property has not been initialized.” 错误:ExecuteNonQuery:连接属性尚未初始化 - Error: ExecuteNonQuery: Connection property has not been initialized ExecuteNonQuery:连接属性尚未初始化。 - ExecuteNonQuery: Connection property has not been initialized. 错误ExecuteNonQuery:尚未初始化Connection属性 - Error ExecuteNonQuery: Connection property has not been initialized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM