我有一個WinForm通過編輯兩個TextBox產品名稱和產品成本更新SQL數據庫,但是它不更新數據庫,這裏是我的示例代碼更新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
。 任何想法我會感謝
你有任何錯誤信息接收? – 2013-04-25 11:43:35
你得到的錯誤信息是什麼,你在數據庫中的成本是雙倍或者是int數據類型。 – Rahul 2013-04-25 11:50:10
沒有錯誤,它只是不起作用 – user2102572 2013-04-25 11:55:56