2013-02-18 110 views
2

我的代碼應該更新舊記錄,同時如果找到了新記錄,它應該同樣將其插入到數據庫中...我使用表adpater在做這種方法。使用vb.net中的tableadapter更新舊數據並添加新數據

下面是代碼:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 

    Dim pta As New PHDSTableAdapters.productdatabaseTableAdapter 
    pta.Updateproduct(TextBox1.Text, ComboBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text) 
    pta.Fill(myds.productdatabase) 
    Dim lta As New PHDSTableAdapters.lotnoTableAdapter 
    Dim lt = lta.GetDataBylotno(TextBox5.Text) 
    Dim l As phaccess.PHDS.lotnoRow = lt.Rows(0) 
    Dim i As Integer 
    For i = 0 To DGV.Rows.Count - 1 

     For Each l In myds.lotno 
      Dim lot As String = DGV.Rows(i).Cells(1).Value 
      Dim del As Date = DGV.Rows(i).Cells(2).Value 
      Dim exp As Date = DGV.Rows(i).Cells(3).Value 
      Dim quantity As Integer = DGV.Rows(i).Cells(4).Value 
      Dim sup = DGV.Rows(i).Cells(5).Value 
      Dim disc = DGV.Rows(i).Cells(6).Value 

      If l.productid = TextBox5.Text Then 
       Dim lotnumber As String = l.lotnumber 
       If l.lotnumber <> lot Then 
       'the error occurs in the insert statement as it would create duplicates 'of the index...the index of the table is the lot number 
        lta.Insert(TextBox5.Text, lot, del, exp, quantity, sup, disc) 
       Else 
        lta.Updateedit(del, exp, quantity, sup, disc, lot) 
        lta.Fill(myds.lotno) 
       End If 
      End If 
      If lot = "" Then 
       closeform() 
       lta.Fill(myds.lotno) 
       Button3.Enabled = False 
       Button1.Visible = True 
       Button3.Visible = False 
       Button1.Enabled = False 
       Exit Sub 
      End If 

     Next 
    Next 
End Sub 

如果您有什麼需要幫助我解決這個請不要問。 謝謝

回答

0

您可以使用 「數據表」,以upadate的DataGridView在表

用於顯示數據:用於更新

Dim sql As String = "SELECT * FROM table_name" 
    sCommand = New SqlCommand(sql, conn) 
    sAdapter = New SqlDataAdapter(sCommand) 
    sBuilder = New SqlCommandBuilder(sAdapter) 
    sDs = New DataSet() 
    sAdapter.Fill(sDs, "table_name") 
    sTable = sDs.Tables("table_name") 
    DataGridView1.DataSource = sTable 

sAdapter.Update(sTable) 

我希望這有助於

相關問題