简体   繁体   English

如何在SQL数据库中扣除数量?

[英]How to deduct quantity in SQL database?

enter image description here I'm trying to get the difference of Quantity and ItemBought in my Database and update the Quantity column based on what is the difference between the two.在此处输入图像描述我试图在我的数据库中获取 Quantity 和 ItemBought 的差异,并根据两者之间的差异更新 Quantity 列。

I tried different SQL code for this one, but I always got the same error我为此尝试了不同的 SQL 代码,但总是遇到相同的错误

Try getConnection()尝试 getConnection()

        sql = "SELECT (Quantity - ItemBought) AS [Quantity] FROM products WHERE ProductCode = @ProductCode;"

        cmd = New MySqlCommand
        With cmd
            .Connection = conn
            .CommandText = sql
            .Parameters.Clear()

            .Parameters.AddWithValue("@ProductCode", formPOS.ProductCodeTB.Text)
            .Parameters.AddWithValue("@ItemBought", formInventory.quantityTB.Text)
            result = cmd.ExecuteNonQuery()
            If result = 0 Then
                MsgBox("Error in updating the selected product!")
            Else
                MsgBox("Successfully updated the selected product!")
            End If"

UPDATE the table and with the modified value and refill the gridview.更新表并使用修改后的值并重新填充 gridview。 It will directly change the table value.它将直接更改表值。

Or或者

If you are looking for temporary change of data means follow the below step:如果您正在寻找临时更改数据的方法,请按照以下步骤操作:

"SELECT Quantity AS [Quantity] FROM products WHERE ProductCode = @ProductCode;"

Then subtract the ItemBought then display on the gridview.然后减去 ItemBought 然后显示在 gridview 上。

You are trying to exececutenonquery() so I believe you trying to update your products table.您正在尝试执行exececutenonquery()所以我相信您正在尝试更新您的产品表。

 sql = "UPDATE products set Quantity = (Quantity - @ItemBought) WHERE ProductCode = @ProductCode;"

        cmd = New MySqlCommand
        With cmd
            .Connection = conn
            .CommandText = sql
            .Parameters.Clear()

            .Parameters.AddWithValue("@ProductCode", formPOS.ProductCodeTB.Text)
            .Parameters.AddWithValue("@ItemBought", formInventory.quantityTB.Text)
            result = cmd.ExecuteNonQuery()
            If result = 0 Then
                MsgBox("Error in updating the selected product!")
            Else
                MsgBox("Successfully updated the selected product!")
            End If"

Just got the right answer, I solved it by setting the right TextBox and right Form of the column ProductCode刚刚得到正确答案,我通过设置 ProductCode 列的正确 TextBox 和正确 Form 解决了它

Try
        getConnection()

        sql = "UPDATE products SET Quantity = (Quantity - @ItemBought) WHERE ProductCode = @ProductCode;"

        cmd = New MySqlCommand
        With cmd
            .Connection = conn
            .CommandText = sql
            .Parameters.Clear()

            .Parameters.AddWithValue("@ProductCode", formPOS.ProductCodeTB.Text)
            .Parameters.AddWithValue("@ItemBought", formQuantity.quantityTB.Text)
            result = cmd.ExecuteNonQuery()
            If result = 0 Then
                MsgBox("Error in updating the selected product!")
            Else
                MsgBox("Successfully updated the selected product!")
            End If
        End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM