我正在尝试使用datagridview减去数据库中的多个特定行 [英] I'm trying to subtract a multiple specific row in database by using datagridview

查看:79
本文介绍了我正在尝试使用datagridview减去数据库中的多个特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim intQty As Integer

          For Each row As DataGridViewRow In Me.DataGridView1.Rows
              Dim intProductID As Integer
              Dim strSQL As String

              If row.Cells(0).Value > 0 Then
                  intProductID = row.Cells(0).Value
                  intQty = row.Cells(3).Value

                  strSQL = "UPDATE Items SET Quantity = Quantity - " & intQty & " WHERE Barcode = " & intProductID

                  Dim cmd_a As OleDbCommand = New OleDbCommand(strSQL, cnn)

                  cmd_a.ExecuteNonQuery()
              End If
          Next row





我尝试了什么:



我收到错误,查询值和目标字段数不一样。

我将使用什么代码?



What I have tried:

I'm getting an error, Number of query values and destination fields are not the same.
What code am i going to use?

推荐答案

首先,

(非常重要你正在邀请黑客使用SQL注入你的代码。你忽略了它,它们会到达正确的位置。



- 我希望你能分享正确的代码

- - 确保条形码是数据库中的int字段。如果不用一对单引号括起该值。

- 用一对括号括起减法,如(数量 - & intQty&)



如果您仍然收到错误,请告诉我。

希望,它有帮助:)
First of all,
(it's very very important) you are inviting hackers to have their hands on with your code using SQL Injection. You ignore this and they'll hit the right place.

-- I hope, you are sharing the correct piece of code
-- Make sure that your Barcode is an int field in database. If not enclose the value with pair of single quotes.
-- Enclose the subtraction with a pair of brackets like (Quantity - " & intQty & ")

If you still get the error, please let me know.
Hope, it helps :)


按照MSDN文档中的步骤操作:查询数量值和目标字段不相同。 (错误3346)  [Access 2007 Developer Reference] [ ^ ]以解决您的问题。



Suvendu Shekhar Giri [ ^ ]警告你关于sql注入。因此,您必须使用参数化查询而不是连接字符串,例如:

Follow the steps from MSDN documentation: Number of query values and destination fields are not the same. (Error 3346) [Access 2007 Developer Reference][^] to resolve your issue.

Suvendu Shekhar Giri[^] warned you about sql injection. So, you have to use parameterized query instead of concatenated string, such as:
strSQL = "UPDATE Items SET Quantity = Quantity - @qty WHERE Barcode = @barcode;"
cmd.Parameters.AddWithValue("@qty", intQty)
cmd.Parameters.AddWithValue("@barcode", intProductID)





别忘了添加[命令行末尾的; ]! MS Access非常喜欢它;)



有关详细信息,请参阅:

UPDATE语句(Microsoft Access SQL)  [Access 2007 Developer Reference] [ ^ ]

使用Access SQL从表中插入,更新和删除记录 [ ^ ]



Do NOT forget to add [;] at the end of command line! MS Access really likes it ;)

For further details, please see this:
UPDATE Statement (Microsoft Access SQL) [Access 2007 Developer Reference][^]
Insert, Update, and Delete Records From a Table Using Access SQL[^]


这篇关于我正在尝试使用datagridview减去数据库中的多个特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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