使用复选框选中的值更新数据库中的特定行

本文关键字:数据库 更新 复选框 | 更新日期: 2023-09-27 18:00:09

我有一个web应用程序,它根据用户输入生成条形码图像。用户选择存储在数据库中的值,然后这些值显示在数据绑定复选框中。我的过程在数据库中插入用户输入值没有问题,但它在数据库中创建了新数据。我想弄清楚的是,如何从某种意义上确定Where子句,无论它是在我的存储过程中,还是在我的代码中,无论哪个数据绑定复选框被选中,它都会更新我数据库中的特定行,而不是创建新行。请注意,它只是一个复选框,而不是复选框列表。

protected void gen_barcode(object sender, EventArgs e)
{
    int n;
    int i = Int32.Parse(amount.Text);
    string date_picker = datepicker.Text;
    SqlConnection conn = new SqlConnection(GetConnectionString());
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "barcode_insert"
    cmd.Parameters.AddWithValue("@Amount", amount.Text);
    cmd.Parameters.AddWithValue("@Date", datepicker);
    if (CheckBox_Code.Checked)
    {
          //generate image code
    }
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

复选框绑定

public void CheckBoxBind()
{
    SqlConnection conn = new SqlConnection(GetConnection());
    using (SqlCommand cmd = new SqlCommand())
    {
       cmd.CommandText = "Select CheckBoxCode FROM CodeTable Where CheckBoxCode LIKE 'EX'"
       cmd.Connection = conn;
       conn.Open();
       using (SqlDataReader rdr = cmd. ExecuteReader())
       {
          while (rdr.Read()
          {
             CheckBox_Code.Text = rdr["CheckBoxCode"].ToString()
             cmd.Parameters.AddWithValue("@CheckBoxCode", CheckBox_Code)
          }
       }
       conn.Close();
    }
} 

使用复选框选中的值更新数据库中的特定行

CheckBoxBind()的第6行需要使用select语句中的参数名称:

cmd.CommandText = "Select CheckBoxCode FROM CodeTable Where CheckBoxCode = @CheckBoxCode"