gpt4 book ai didi

c# - SQL 更新查询返回错误

转载 作者:行者123 更新时间:2023-11-30 23:20:10 25 4
gpt4 key购买 nike

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 ID= "+ 中的 WHERE 之前放置一个空格

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com