在我的数据库中,我有一个名为[productlist]的表,里面是(名称),(股票)(品牌),(大小)
我有这样的列表视图:
|Qty| Name | Brand | Size |
| 2 | Name 1| Brand 1| Size 1|
| 2 | Name 2| Brand 2| Size 2|
我想要发生的是当我点击保存按钮 我的列表视图中“数量”的值将被添加到我的数据库中“股票”的值
(我的列表视图中的数量值)+(我的产品清单表中库存的当前值)=(我的产品清单表中的库存)
我不知道如何开始,所以我尝试了这段代码,但它没有用:
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim MyConnection As New MySqlConnection
Dim MyTransaction As MySqlTransaction = Nothing
MyConnection.ConnectionString = "Server=localhost;Database=empiredb;Uid=root;"
MyConnection.Open()
MyTransaction = MyConnection.BeginTransaction
For Each item As ListViewItem In lvwProductPurchases.Items
Dim SQL As String = "update productlist set [Stock] = [Stock] + @0 where Name = @1 and Brand = @2 and Size = @3"
Dim CMD4 As New MySqlCommand
CMD4.Connection = MyConnection
CMD4.Transaction = MyTransaction
CMD4.CommandText = SQL
CMD4.Parameters.AddWithValue("@0", item.Text)
CMD4.Parameters.AddWithValue("@1", item.SubItems(1).Text)
CMD4.Parameters.AddWithValue("@2", item.SubItems(2).Text)
CMD4.Parameters.AddWithValue("@3", item.SubItems(3).Text)
CMD4.ExecuteNonQuery()
CMD4.Dispose()
Next
MsgBox("updated")
'try and catch i will put transactionrollback
End sub
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在“[Stock] = [Stock] +'2'附近使用正确的语法,其中Name ='kahoy',Brand ='Brand 1',Size ='m'在行1
答案 0 :(得分:1)
我还没有在VB.net上做过多少工作,但我确实看到了你的MySQL查询有一个问题。在MySQL中,在列名称周围放置括号以转义/引用它们是无效的语法。它在SQL Server,Access和其他一些数据库中都有效。我建议您使用以下语法进行查询:
Dim SQL As String = "UPDATE productlist SET Stock = Stock + @0 " _
"WHERE Name = @1 AND Brand = @2 AND Size = @3"