更新所选的数据库行 [英] Update selected row of database

查看:66
本文介绍了更新所选的数据库行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新用户选择的行中的一个单元格。因此,当单击开始按钮时,该行中的单元格将更新为生产开始



但是,当我检查数据库时,特定列下的每个单元格已经更新并且已经'生产开始')而不是只有行列中的单元格点击

因此更新已应用于该列下的所有单元格...任何人都可以帮忙吗?





I'm trying to update one cell in the row the user selects. So when the start button is clicked the cell in that row is updated to say 'production started'

However, when I check the database, every cell under the specific column has updated and has 'production started') instead of only the cell in the column of the row click
So the update has been applied to all cells under that column ... can anyone help ?


private void Start_Click(object sender, EventArgs e)
{
    MAcon.Open();
    OleDbCommand cm = new OleDbCommand("UPDATE [Production] SET [Product Status]=@ProductStatus WHERE [OrderID]=@OrderID", MAcon);
    cm.Parameters.AddWithValue("@ProductStatus", Access.Text);
    cm.Parameters.AddWithValue("@OrderID", OrderIDTxt.Text);
    cm.ExecuteNonQuery();
    MAcon.Close();
}











所以即选择第一行 - 当用户点击开始 - 第1行时,产品状态应该说'生产开始'



我是什么尝试过:








So i.e. row one is selected - when user clicks start - row 1, product status should say 'Production started'

What I have tried:

{
    MAcon.Open();
    OleDbCommand cm = new OleDbCommand("UPDATE [Production] SET [Product Status]=@ProductStatus WHERE [OrderID]=@OrderID", MAcon);
    cm.Parameters.AddWithValue("@ProductStatus", Access.Text);
    cm.Parameters.AddWithValue("@OrderID", OrderIDTxt.Text);
    cm.ExecuteNonQuery();
    MAcon.Close();
}

推荐答案

检查数据的OrderID列中的值 - 您的命令将每次更新OrderID与OrderIDText TextBox中给出的值匹配的行。
Check what value is in the OrderID column of your data - your command will update every row where the OrderID matches the value you gave it in the OrderIDText TextBox.


您需要确认Access.Text实际上包含您的想法。



You need to comfirm that "Access.Text" actually contains what you think it does.

cm.Parameters.AddWithValue("@ProductStatus", Access.Text);


假设订单中有1个以上的产品,您需要告诉您要更新的订单中的哪个产品或行,否则所有订单产品都会更新。

此SQL代码仅更新1行。

您需要检查此代码是否多次执行。

一种方法是添加文本框并在每次执行代码时附加一些文本。包括OrderID。

另一种方法是使用调试器来查看你的代码是什么。

-----

你的代码不要按照你期望的方式行事,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [ ^ ]

调试器只显示你的代码正在做,你的任务是与它应该做的事情进行比较。
Assuming an order have more that 1 product, you need to tell which product or row in the order you want to update, otherwise all products of order are updated.
This SQL code updates only 1 row.
You need to check if this code is executed more than once.
One way to do it is to add a textbox and append some text each time the code is executed. include the OrderID.
Another way is to use the debugger to see what is doing your code.
-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于更新所选的数据库行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆