我有一个 Winform,它通过编辑两个 TextBox 产品名称和产品成本来更新 SQL 数据库,但是它不会更新数据库,这是我的示例代码
private void simpleButton5_Click(object sender, EventArgs e)
{
string id = comboBox2.Items[comboBox2.SelectedIndex].ToString();
string name = txtProdName.Text;
string cost = txtProductCost.Text;
cn.Open();
string query = "UPDATE [Product1] SET [Product_Name]= @Product_Name,[Product_Cost]= @Product_Cost where [Product_ID]= @Product_ID";
SqlCommand cmd = new SqlCommand(query, cn);
cmd.Parameters.AddWithValue("@Product_ID", id);
cmd.Parameters.AddWithValue("@Product_Name", name);
cmd.Parameters.AddWithValue("@Product_Price", cost);
try
{
cmd.ExecuteNonQuery();
MessageBox.Show("Update Succesfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
cn.Close();
}
id 的数据类型是char
,Product_Name 是nvarchar(50)
,Porduct_Cost 是bigint
。任何我会感激的想法