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

时间:2023-01-29 16:27:56
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连接属性尚未初始化。

2 个解决方案

#1


3  

the problem with your current code is that you have not set the Connection property of the SqlCommand object. Try this,

当前代码的问题是,您没有设置SqlCommand对象的连接属性。试试这个,

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

and you must also parameterized the values set on the 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 using statement for propr object disposal
  • 使用using语句进行propr对象处理
  • use try-catch block to properly handle objects
  • 使用try-catch块来正确处理对象

#2


2  

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 Name and using statements to ensure that anything implementing IDisposable gets disposed, even in case of an exception. This will also close the connection.

注意,我还对名称使用了sql参数,并使用语句确保实现IDisposable的任何东西都得到了处理,即使是在出现异常的情况下。这也将关闭连接。

#1


3  

the problem with your current code is that you have not set the Connection property of the SqlCommand object. Try this,

当前代码的问题是,您没有设置SqlCommand对象的连接属性。试试这个,

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

and you must also parameterized the values set on the 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 using statement for propr object disposal
  • 使用using语句进行propr对象处理
  • use try-catch block to properly handle objects
  • 使用try-catch块来正确处理对象

#2


2  

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 Name and using statements to ensure that anything implementing IDisposable gets disposed, even in case of an exception. This will also close the connection.

注意,我还对名称使用了sql参数,并使用语句确保实现IDisposable的任何东西都得到了处理,即使是在出现异常的情况下。这也将关闭连接。