当我点击acceptBTN时,我想更新stockTBL中某个项目的数量
private void acceptBTN_Click(object sender, EventArgs e)
{
string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
string Query = "UPDATE stockTBL SET Quantity = Quantity+ '" + this.quantityTxt.Text + "' where [Item Name] = '" + this.itemTxt.Text + "';";
SqlCeConnection conDataBase = new SqlCeConnection(constring);
SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase);
try
{
conDataBase.Open();
MessageBox.Show("Sucess");
//displays a system error message if a problem is found
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
这是我的代码,当我点击acceptBTN时,它只显示MessageBox而不更新数量。
答案 0 :(得分:3)
你甚至没有执行刚刚创建它的查询。请先尝试执行它
conDataBase.Open();
cmdDataBase.ExecuteNonQuery();
conDataBase.Close();
一些建议:
SQL Injection
次攻击。始终将using statements
用于一次性物品,以确保它们妥善处理。
string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
string Query = "UPDATE stockTBL SET Quantity = Quantity + @quantity where [Item Name] = @name";
using(SqlCeConnection conDataBase = new SqlCeConnection(constring))
using(SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase))
{
cmdDataBase.Parameters.AddWithValue("@quantity", int.Parse(quantityTxt.Text));
cmdDataBase.Parameters.AddWithValue("@name", itemTxt.Text);
conDataBase.Open();
cmdDataBase.ExecuteNonQuery();
cmdDataBase.Close();
}
答案 1 :(得分:0)
你可能想要cmdDataBase.ExecuteNonQuery
。您打开了连接但从未执行过查询。