2017-03-13 168 views
0

我正在使用VB.Net 2015,并使用MS Access DataBase。 我有了很多的项目,例发票编号2一张发票有橘子,苹果和香蕉,当使用如何在VB.Net中更新具有相同ID的项目

"UPDATE Item_Invoice SET [email protected]_Name,[email protected]_Type,[email protected]_Quantity,[email protected]_Purchase_Price,[email protected]_Total_Cost Where Invoice_No=" & CInt(txtInvoiceNo.Text) & " " , all items become Lemon. 

所有项目视图发票号码2在DataGrid和DataGrid中更改香蕉项柠檬成为柠檬,由于Update语句使用Invoice_No 2更新所有项目,任何想法如何克服和解决问题。由于提前

For i = 0 To DGPurchase.Rows.Count - 2 
       ObjCommand.Parameters.Clear() 
       ObjCommand.CommandText = "UPDATE Item_Invoice SET [email protected]_Name,[email protected]_Type,[email protected]_Quantity,[email protected]_Purchase_Price,[email protected]_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

+0

你能粘贴你的表结构,好吗?这将是有用的 –

+0

嗯..我想你应该添加你想要改变你的条款 – Pikoh

+0

你的项目名称是最困难的方式可能。如果您使用的是DGV,那么也可以使用DataTable和DataAdapter。然后它就是'myDA.Update(myDT)'来更新任何需要更新的东西。 DB Provider对象完成所有工作。你也应该参加[游览]。 – Plutonix

回答

0

你应该从你的数据库的每一行创建一个唯一的ID(P_ID PRIMARY KEY AUTOINCREMENT),并使用该ID识别的行。否则,我不会看到用于识别该行的简单解决方案。也许你应该在发票ID之外的where子句中查找哪些行需要更新。

+0

@Pikoh该项目来自datagridview,我正在与发票10,20中的许多项目交谈。此外,我想更新行信息而不是项目名称。 –

+0

你可以通过代码来清楚,如果我添加了一个主键,例如1橙,2苹果,3香蕉,并且将香蕉改为柠檬,只需使用主键和发票号= 2就可以更改为香蕉。顺便说一下发票号码,但它的名称可以是许多发票相同。谢谢 –

+0

在你的gridview中,你应该保留一个带有主键的隐藏列,并且这个行是唯一的。更新查询应该使用where子句中的主键来标识该行。 – Luci

0

谢谢大家。

现在工作正常通过删除并重新创建(插入)使用新的信息,其实这是比较有用的使用插入,而不是更新的情况下,用户添加新的项目到DataGridView。 这是finall代码工作完美:

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 

感谢您有用的意见。

相关问题