C# “executenonquery 连接属性尚未初始化”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15852958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:09:27  来源:igfitidea点击:

"executenonquery connection property has not been initialized"

c#asp.netsqlsql-serversql-update

提问by Mirek

SqlConnection cn = new SqlConnection(@"DataSource=dbedu.cs.vsb.cz\SQLDB;Persist Security Info=True;User ID=*****;Password=*******");

SqlCommand cmd = new SqlCommand();
string finish = DropDownListFi.SelectedValue;
cn.Open();
String Name = Request.QueryString["Name"];
cmd.CommandText = "UPDATE navaznost_ukolu SET finish=@finish where Name='" + Name + "'";
cmd.Parameters.Add(new SqlParameter("@finish", finish));
cmd.ExecuteNonQuery();
cmd.Clone();

The error message

错误信息

Executenonquery connection property has not been initialized.

Executenonquery 连接属性尚未初始化。

采纳答案by John Woo

the problem with your current code is that you have not set the Connectionproperty of the SqlCommandobject. Try this,

您当前代码的问题在于您尚未设置对象的Connection属性SqlCommand。尝试这个,

SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;

and you must also parameterized the values set on the name

并且您还必须参数化设置在 name

String Name = Request.QueryString["Name"];
cmd.CommandText = "UPDATE navaznost_ukolu SET finish=@finish where Name=@name";
cmd.Parameters.Add(new SqlParameter("@finish", finish));
cmd.Parameters.Add(new SqlParameter("@name", Name));

FULL CODE

完整代码

string finish = DropDownListFi.SelectedValue;
String Name = Request.QueryString["Name"];
string connStr = @"DataSource=dbedu.cs.vsb.cz\SQLDB;
                   Persist Security Info=True;
                   User ID=*****;
                   Password=*******";
string sqlStatement = @"UPDATE navaznost_ukolu 
                        SET    finish = @finish 
                        WHERE  Name = @Name";

using (SqlConnection conn = new SqlConnection(connStr))
{
    using(SqlCommand cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = sqlStatement;
        cmd.CommandType = CommandType.Text;

        cmd.Parameters.Add(new SqlParameter("@finish", finish));
        cmd.Parameters.Add(new SqlParameter("@name", Name));

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch(SqlException e)
        {
            // do something with the exception
            // do not hide it
            // e.Message.ToString()
        }
    }
}

For proper coding

为了正确编码

  • use usingstatement for propr object disposal
  • use try-catchblock to properly handle objects
  • using用于 propr 对象处理的use语句
  • 使用try-catch块来正确处理对象

回答by Tim Schmelter

The error is self-explanatory, you have not assigned the connection to the command. You can use the constructor:

该错误不言自明,您尚未将连接分配给命令。您可以使用构造函数:

using(var cn = new SqlConnection(@"DataSource=dbedu.cs.vsb.cz\SQLDB;Persist Security Info=True;User ID=*****;Password=*******"))
using(var cmd = new SqlCommand(
    "UPDATE navaznost_ukolu SET finish=@finish where Name=@Name"
    , cn))
{
   string finish = DropDownListFi.SelectedValue;
   cn.Open();
   String Name = Request.QueryString["Name"];
   cmd.Parameters.AddWithValue("@finish", finish);
   cmd.Parameters.AddWithValue("@Name", Name);
   cmd.ExecuteNonQuery();
}

Note that i've also used a sql-parameter for the Nameand usingstatements to ensure that anything implementing IDisposablegets disposed, even in case of an exception. This will also close the connection.

请注意,我还为Nameandusing语句使用了一个 sql 参数,以确保任何实现IDisposable都得到处理,即使在出现异常的情况下也是如此。这也将关闭连接。