问题描述
如果标签中的内容与数据库的column1(订单ID列)中的单元格内容匹配(与登录表单类似的概念),则更改该特定行中单元格4的值(订单状态)(并且只有那一行)到文本框中的特定值
但是当我运行程序并检查数据库以查看位置单元格中的文本是否已更新时,它没有。我已经检查过,我不知道它在哪里。
If the content in the label matches a cell content in 'column1' (the order ID column) of the database (similar concept to a login form), change value of cell 4 (the order status) in that specific row (and only that row) to specific value in textbox
However when I run the program and check the database to see in the text in the location cell has been updated, it hasnt . I have checked and i'm not sure where it is.
if (result == DialogResult.Yes)
{
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [OrderID] = @orderID", MAcon); //'"+orderID+"'", MAcon);
da.SelectCommand.Parameters.AddWithValue("@OrderID", orderID); //.Text
DataTable dtbl = new DataTable();
da.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
OleDbCommand cmd = new OleDbCommand("UPDATE [Customer Orders] SET OrderStatus= @OrderStatus WHERE OrderID = @orderId", MAcon);
MAcon.Open();
cmd.Parameters.AddWithValue("@OrderID", orderID);
cmd.Parameters.AddWithValue("@OrderStatus", Location.Text);
cmd.ExecuteNonQuery();
MAcon.Close();
DialogResult finish = MessageBox.Show("Production has begun", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
我的尝试:
What I have tried:
if (result == DialogResult.Yes)
{
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [OrderID] = @orderID", MAcon); //'"+orderID+"'", MAcon);
da.SelectCommand.Parameters.AddWithValue("@OrderID", orderID); //.Text
DataTable dtbl = new DataTable();
da.Fill(dtbl);
if (dtbl.Rows.Count == 1)
{
OleDbCommand cmd = new OleDbCommand("UPDATE [Customer Orders] SET OrderStatus= @OrderStatus WHERE OrderID = @orderId", MAcon);
MAcon.Open();
cmd.Parameters.AddWithValue("@OrderID", orderID);
cmd.Parameters.AddWithValue("@OrderStatus", Location.Text);
cmd.ExecuteNonQuery();
MAcon.Close();
DialogResult finish = MessageBox.Show("Production has begun", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
推荐答案
OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [OrderID] = @orderID", MAcon);
并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行它并仔细观察)以找出原因。
从有什么价值开始单编号?然后找出SELECT查询返回的内容。
if
成功吗?如果没有,为什么不呢?如果是这样,那么Location.Text中的内容以及数据库中该OrderID值的内容是什么?
抱歉,我们不能为您做到 - 是时候学习一门新的(非常非常有用的)技能:调试!
And run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Start with "what is the value in orderID?" and then find out what the SELECT query returns.
Does the if
succeed? If not, why not? If so, then what is in Location.Text, and what is in your Database for that OrderID value?
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
这篇关于如何使用UPDATE peramatized querys更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!