InvalidOperationException:连接属性尚未在update命令中初始化?

时间:2018-02-02 03:34:05

标签: c# asp.net sql-server-2005

protected void btnUpdate_Click(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("update Students set RegNo='" + RegNo.Text + "',Name='" + Name.Text + "',Address=" + Address.Text);
    con.Open();
    int result = cmd.ExecuteNonQuery();
    con.Close();
    if (result == 1)
    {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:alert('Record Updated Successfully');", true);    
        Response.Write("Record saved successfully");
    }
    Response.Redirect("~/WebForm1.aspx"); 
}

此代码显示如下错误:

  

System.InvalidOperationException。 ExecuteNonQuery:尚未初始化Connection属性。

4 个答案:

答案 0 :(得分:0)

我认为,在SQL命令中,您需要分配连接

protected void btnUpdate_Click(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("update Students set RegNo='" + RegNo.Text + "',Name='" + Name.Text + "',Address=" + Address.Text);
    cmd.Connection = con;
    con.Open();
    int result = cmd.ExecuteNonQuery();
    con.Close();
    if (result == 1)
    {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:alert('Record Updated Successfully');", true);    
        Response.Write("Record saved successfully");
    }
    Response.Redirect("~/WebForm1.aspx"); 
}

答案 1 :(得分:0)

你需要告诉你的sql命令使用这个连接(con)来执行命令(cmd)。所以使用带有2个参数的SqlCommand类的重载构造函数(cmdText,connection)。

    SqlCommand cmd = new SqlCommand("update Students set RegNo='" +
     RegNo"',Name='" + Name.Text + "',Address=" + Address.text, con);

但是也可以使用参数less constructor创建SqlCommand类的实例,然后使用SqlCommand对象的CommandText和Connection属性指定命令文本和连接,如下所示。

SqlCommand cmd = new SqlCommand("update Students set RegNo='" + RegNo.Text + "',Name='" + Name.Text + "',Address=" + Address.Text);
 cmd.Connection = con;
 con.Open();

您可以使用自动处理资源的using语句。使用时,我们不必显式调用Close()方法。该连接将自动关闭。

         int result;
          using (SqlConnection con = new SqlConnection(constr))
                {
                SqlCommand cmd = new SqlCommand("update Students set RegNo='" + RegNo.Text + "',Name='" + Name.Text + "',Address=" + Address.Text, con);
                con.Open();
                result = cmd.ExecuteNonQuery();
                 } 
                if (result == 1)
                {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:alert('Record Updated Successfully');", true);    
                    Response.Write("Record saved successfully");
                }
                Response.Redirect("~/WebForm1.aspx"); 

答案 2 :(得分:0)

尝试使用以下示例代码

string constr ="Data Source=localhost;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=1111"
SqlConnection con = new SqlConnection(constr);

答案 3 :(得分:0)

   string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("update Student set Name='" + Name.Text + "',Address='" + Address.Text + "'where RegNo=" + RegNo.Text);

            cmd.Connection = con;//adding this line my error solved
            con.Open();
            int result = cmd.ExecuteNonQuery();
            con.Close();

我改变了上面的代码。