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的消息框确实返回正确的值。有人可以解释如何解决此问题吗?

最佳答案

WHERE+ "WHERE ID= " +之前的此处放置一个空格

另外,请确保您在ExecuteNonQuery上调用cmd1

关于c# - SQL更新查询返回错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39870068/

10-11 13:42