OleDbCommand cmd = new OleDbCommand("SELECT Stock FROM Products WHERE ID=" + ProductID + ";", conn); //run the database query
OleDbDataReader cusReader = cmd.ExecuteReader(); //read the result of the query
cusReader.Read();
ProductStock = (int)cusReader.GetValue(0);
cusReader.Close();
MessageBox.Show((ProductStock).ToString()); // checks that the form is being accessed and the SELECT query works
OleDbCommand cmd1 = new OleDbCommand("UPDATE Products SET Stock=" + (ProductStock - 1) + "WHERE ID= " + ProductID +";", conn);
try
{
if (cusReader.RecordsAffected > 0)
{
MessageBox.Show("no issue was experienced");
}
else
{
MessageBox.Show("An issue occured when decreasing stock");
}
cusReader.Close();
}
catch (Exception ex)
{
MessageBox.Show("An error occured in query.\n" + ex.Message);
}
更新查询返回"发生的问题"信息。显示变量Productstock的消息框确实返回正确的值。有人可以解释如何解决这个问题吗?
答案 0 :(得分:2)
在WHERE
+ "WHERE ID= " +
之前放置一个空格
另外,请务必在ExecuteNonQuery
上致电cmd1
。
答案 1 :(得分:0)
OleDbCommand cmd1 = new OleDbCommand("UPDATE Products SET Stock=" + (ProductStock - 1) + " WHERE ID= " + ProductID +";", conn);
请使用上述命令。
问题:库存值与
之间没有空格答案 2 :(得分:0)
请尝试这个。它应该工作
OleDbCommand cmd1 = new OleDbCommand("UPDATE Products SET Stock=" + (ProductStock - 1) + " WHERE ID= " + ProductID, conn);
答案 3 :(得分:0)
尝试一次性更新(不要将select
和update
分开:有人可以在您执行之后修改数据 {{ 1}}但之前您已启动select
),然后检查受影响的行。让你的SQL 可读以避免明显的错误。
update