这是一个简单的问题,但由于我是C#的新手,我不确定如何解决此问题。 基本上,我有一个datagridview显示来自MySQL表的记录。我有一个删除按钮,通过点击执行SQL查询,这工作正常,并且也意味着从datgridview中删除选定的行。但实际发生的是,即使只选择了一行,它也会删除多行。 下面是该查询:试图从datagridview中删除选定的行,但它删除了多行
private void delete_btn_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
string constring = @"server = localhost; user id = root; password = pass; persistsecurityinfo = false; database = mapping; allowuservariables = false";
using (MySqlConnection con = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand("UPDATE deletion SET date_time = UTC_TIMESTAMP() where product_id =" + proid_txtbx.Text, con))
{
cmd.Parameters.AddWithValue("@product_id", row.Cells["product_id"].Value);
cmd.Parameters.AddWithValue("@product_name", row.Cells["product_name"].Value);
cmd.Parameters.AddWithValue("@category_id", row.Cells["category_id"].Value);
cmd.Parameters.AddWithValue("@date_time", row.Cells["date_time"].Value);
con.Open();
cmd.ExecuteNonQuery();
}
foreach(DataGridViewRow item in this.dataGridView1.SelectedRows)
{
dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
}
}
}
您的命令不_even_有参数。当你运行它时,你可能会得到一个异常。 –
cmd.Parameters.AddWithValue(“@ product_id”,row.Cells [“product_id”]。Value); –