使用主键更新1行 [英] Update 1 row using primary key

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

问题描述

我在form1中有一个datagridview,用户选择一行,相关的详细信息将发送到form2中的相关文本框。从这里用户点击一个按钮,该按钮应该更新所选行中的单元格(在数据库中)并将其更改为生产中(这是orderstatus文本框。



我希望能够更新与客户ID相关的行,这是数据库中的主键...当前软件更新数据库表中的所有内容...



我试过调试它我看不到错误



I have a datagridview in form1, the user selects a row and relevant details are sent to the correlating textboxes in form2. from here the user clicks a button which should update a cell in the row selected (in the database) and change it to 'In production'(which is the orderstatus textbox.

I want to be able to update a row which correlates to the customer ID, which is a primary key in the database... currently the software updates everything within the database table ...

I have tried to debug it i cannot see the error

MAcon.Open();
OleDbCommand cm = new OleDbCommand("UPDATE [Customer Orders] SET [Order Status]=@OrderStatus WHERE [CustomerID]=@CustomerID", MAcon);
cm.Parameters.AddWithValue("@OrderStatus", textBox4.Text);
cm.Parameters.AddWithValue("@CustomerID", CustID.Text);
cm.ExecuteNonQuery();
MAcon.Close();





我尝试过:





What I have tried:

MAcon.Open();
OleDbCommand cm = new OleDbCommand("UPDATE [Customer Orders] SET [Order Status]=@OrderStatus WHERE [CustomerID]=@CustomerID", MAcon);
cm.Parameters.AddWithValue("@OrderStatus", textBox4.Text);
cm.Parameters.AddWithValue("@CustomerID", CustID.Text);
cm.ExecuteNonQuery();
MAcon.Close();

推荐答案

我已经解决了这个问题,
I have solved the issue,
[CustomerID]

应该

[Customer ID]

而不是


SQL无法正常工作。它只是没有。除非它们都具有相同的id,或者正在运行不同的SQL,否则这不会发生。



您是否考虑过使用实体框架?这个字符串mashing SQL很糟糕
SQL doesn't work that way. It just doesn't. Unless they all have the same id, or different SQL is running, this just won't happen.

Have you considered using Entity Framework? This string mashing SQL is awful


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

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