【问题标题】:InvalidOperationException: Connection property has not been initialized in update command?InvalidOperationException:连接属性尚未在更新命令中初始化?
【发布时间】:2018-02-02 03:34:04
【问题描述】:
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:连接属性尚未初始化。

【问题讨论】:

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


【解决方案1】:

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

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

但也可以,使用无参数构造函数创建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 语句来自动释放资源。使用 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】:

    尝试使用如下示例代码

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

    【讨论】:

      【解决方案3】:

      我认为,您需要在 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"); 
      }
      

      【讨论】:

        【解决方案4】:
           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();
        

        我像上面一样更改了我的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-03
          • 2011-07-22
          • 2012-08-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多