多个 datagridview 数据更新到数据库 mysql - vb.net
multiple datagridview data update to database mysql - vb.net
我创建了一个具有两个不同界面的购物系统。管理界面用于 add/update/delete 项目到数据库和客户界面购买产品。用户购买产品后,我在管理界面更新数量时遇到问题。管理界面中的数量不断更新,直到数值变为负数。
Try
For Each row As DataGridViewRow In formPOS.ProductAddToCartDGV.Rows
dbconnection()
sql = "UPDATE products_database SET Product_Quantity = Product_Quantity - @ProductQuantity WHERE Product_Code = @ProductCode;"
cmd = New MySqlCommand
With cmd
.Connection = conn
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue("@ProductCode", row.Cells(0).Value)
.Parameters.AddWithValue("@ProductQuantity", row.Cells(3).Value)
result = .ExecuteNonQuery()
If result = 0 Then
MsgBox("Error in updating stocks")
Else
MsgBox("Successfully update stocks")
End If
End With
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
是否要确保客户输入的值小于或等于剩余总数?如果是这样,您可以在 WHERE
子句中添加条件 Product_Quantity >= CAST(@ProductQuantity AS int)
。
For i As Integer = 0 To CustomerDGV.Rows.Count - 2
Dim constr As String = "connection string"
Dim sql As String = "UPDATE products_database SET Product_Quantity = Product_Quantity - @ProductQuantity WHERE Product_Code = CAST(@ProductCode AS int) AND Product_Quantity >= CAST(@ProductQuantity AS int)"
Using conn As SqlConnection = New SqlConnection(constr)
conn.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.Parameters.AddWithValue("@ProductCode", CustomerDGV.Rows(i).Cells(0).Value)
cmd.Parameters.AddWithValue("@ProductQuantity", CustomerDGV.Rows(i).Cells(1).Value)
Dim rows As Integer = cmd.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show($"Make sure {CustomerDGV.Rows(i).Cells(0).Value} ProductQuantity <= Product_Quantity")
End If
End Using
Next
注意:这是一个 SQL 服务器演示。
我创建了一个具有两个不同界面的购物系统。管理界面用于 add/update/delete 项目到数据库和客户界面购买产品。用户购买产品后,我在管理界面更新数量时遇到问题。管理界面中的数量不断更新,直到数值变为负数。
Try
For Each row As DataGridViewRow In formPOS.ProductAddToCartDGV.Rows
dbconnection()
sql = "UPDATE products_database SET Product_Quantity = Product_Quantity - @ProductQuantity WHERE Product_Code = @ProductCode;"
cmd = New MySqlCommand
With cmd
.Connection = conn
.CommandText = sql
.Parameters.Clear()
.Parameters.AddWithValue("@ProductCode", row.Cells(0).Value)
.Parameters.AddWithValue("@ProductQuantity", row.Cells(3).Value)
result = .ExecuteNonQuery()
If result = 0 Then
MsgBox("Error in updating stocks")
Else
MsgBox("Successfully update stocks")
End If
End With
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
是否要确保客户输入的值小于或等于剩余总数?如果是这样,您可以在 WHERE
子句中添加条件 Product_Quantity >= CAST(@ProductQuantity AS int)
。
For i As Integer = 0 To CustomerDGV.Rows.Count - 2
Dim constr As String = "connection string"
Dim sql As String = "UPDATE products_database SET Product_Quantity = Product_Quantity - @ProductQuantity WHERE Product_Code = CAST(@ProductCode AS int) AND Product_Quantity >= CAST(@ProductQuantity AS int)"
Using conn As SqlConnection = New SqlConnection(constr)
conn.Open()
Dim cmd As SqlCommand = New SqlCommand()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.Parameters.AddWithValue("@ProductCode", CustomerDGV.Rows(i).Cells(0).Value)
cmd.Parameters.AddWithValue("@ProductQuantity", CustomerDGV.Rows(i).Cells(1).Value)
Dim rows As Integer = cmd.ExecuteNonQuery()
If rows = 0 Then
MessageBox.Show($"Make sure {CustomerDGV.Rows(i).Cells(0).Value} ProductQuantity <= Product_Quantity")
End If
End Using
Next
注意:这是一个 SQL 服务器演示。