从asp.net更新SQL Server数据库中的特定列

时间:2015-02-28 22:14:40

标签: sql asp.net sql-server database

如何通过单击按钮

从数据库更新表格
protected void btnFinish_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    con.Open();

    string insCmd = "UPDATE Orders SET finish = 1  WHERE finish IS NULL";

    SqlCommand addOrder = new SqlCommand(insCmd, con);
    addOrder.Parameters.AddWithValue("finish", 1);

    con.Close();
}

请帮忙

2 个答案:

答案 0 :(得分:2)

您需要执行命令:

  SqlCommand addOrder = new SqlCommand(insCmd, con);
  addOrder.Parameters.AddWithValue("@finish", 1);
  addOrder.ExecuteNonQuery(); 

答案 1 :(得分:2)

您必须使用以下代码运行Sql语句:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();

        string insCmd = "UPDATE Orders SET finish = 1  WHERE finish IS NULL";

       SqlCommand addOrder = new SqlCommand(insCmd, con);
       addOrder.Parameters.AddWithValue("finish", 1);
       addOrder.ExecuteNonQuery();

       con.Close();