如何在VB.Net中更新具有相同ID的项目

时间:2017-03-13 10:54:41

标签: sql vb.net

我正在使用VB.Net 2015,并使用MS Access DataBase。 我有一个包含许多项目的发票,例如发票号码2有橙色,Apple和香蕉,当在DataGrid中查看发票编号2并使用

将DataGrid中的香蕉项目更改为柠檬时
"UPDATE Item_Invoice SET Item_Name=@Item_Name,Item_Type=@Item_Type,Item_Quantity=@Item_Quantity,Item_Purchase_Price=@Item_Purchase_Price,Item_Total_Cost=@Item_Total_Cost Where Invoice_No=" & CInt(txtInvoiceNo.Text) & "  " ,  all items become Lemon.

所有项目变为柠檬,因为更新语句使用Invoice_No 2更新所有项目,任何想法如何克服并解决问题。在此先感谢

 For i = 0 To DGPurchase.Rows.Count - 2
                ObjCommand.Parameters.Clear()
                ObjCommand.CommandText = "UPDATE Item_Invoice SET Item_Name=@Item_Name,Item_Type=@Item_Type,Item_Quantity=@Item_Quantity,Item_Purchase_Price=@Item_Purchase_Price,Item_Total_Cost=@Item_Total_Cost Where Invoice_No=" & CInt(txtInvoiceNo.Text) & "  "
                ObjCommand.Connection = myConnection

                ItmName = DGPurchase.Rows(i).Cells(0).Value.ToString
                ItmType = DGPurchase.Rows(i).Cells(1).Value.ToString
                ItmQuantity = CDbl(DGPurchase.Rows(i).Cells(2).Value)
                itmTotalCost = CDbl(DGPurchase.Rows(i).Cells(3).Value)
                ItmPurPrice = CDbl(DGPurchase.Rows(i).Cells(4).Value)

                ObjCommand.Parameters.AddWithValue("Item_Name", ItmName)
                ObjCommand.Parameters.AddWithValue("Item_Type", ItmType)
                ObjCommand.Parameters.AddWithValue("Item_Quantity", ItmQuantity)
                ObjCommand.Parameters.AddWithValue("Item_Total_Cost", itmTotalCost)
                ObjCommand.Parameters.AddWithValue("Item_Purchase_Price", ItmPurPrice)
                ' ObjCommand.Parameters.AddWithValue("Invoice_No", InvNo)
                ObjCommand.ExecuteNonQuery()
                'ObjCommand.Dispose()

            Next I

DB Table

2 个答案:

答案 0 :(得分:0)

您应该为数据库中的每一行创建一个唯一的ID(P_Id PRIMARY KEY AUTOINCREMENT),并使用该ID来标识该行。否则,我看不到用于识别行的简单解决方案。也许你应该在发票ID之外的where子句中保留旧值,以便找到需要更新的行。

答案 1 :(得分:0)

谢谢大家。

现在可以通过删除和重新创建(插入)新信息来正常工作。实际上,如果用户向DatagridView添加了新项,则使用insert而不是update会更有用。 这是完美代码完美的工作:

 ObjCommand.CommandText = " Delete from Item_Invoice where Invoice_No=" & CInt(txtInvoiceNo.Text) & " "

                ObjCommand.ExecuteNonQuery()
                ObjCommand.Parameters.Clear()

                For i = 0 To DGPurchase.Rows.Count - 2
                    ObjCommand.Parameters.Clear()

                    ObjCommand.CommandText = "Insert into Item_Invoice (Invoice_No,Item_Name,Item_Type,Item_Quantity,Item_Total_Cost,Item_Purchase_Price) values (@Invoice_No,@Item_Name,@item_Type, @Item_Quantity,@Item_Total_Cost,@Item_Purchase_Price)"

                    ItmName = DGPurchase.Rows(i).Cells(0).Value.ToString
                    ItmType = DGPurchase.Rows(i).Cells(1).Value.ToString
                    ItmQuantity = CDbl(DGPurchase.Rows(i).Cells(2).Value)
                    itmTotalCost = CDbl(DGPurchase.Rows(i).Cells(3).Value)
                    ItmPurPrice = CDbl(DGPurchase.Rows(i).Cells(4).Value)

                    ObjCommand.Parameters.AddWithValue("Invoice_No", InvNo)
                    ObjCommand.Parameters.AddWithValue("Item_Name", ItmName)
                    ObjCommand.Parameters.AddWithValue("Item_Type", ItmType)
                    ObjCommand.Parameters.AddWithValue("Item_Quantity", ItmQuantity)
                    ObjCommand.Parameters.AddWithValue("Item_Total_Cost", itmTotalCost)
                    ObjCommand.Parameters.AddWithValue("Item_Purchase_Price", ItmPurPrice)
                    ObjCommand.ExecuteNonQuery()


                Next I

感谢您的有用评论。