2

我的代码应该更新旧记录,同时如果找到新记录,它也应该将其插入数据库中......我正在使用 table adpater 来执行此方法。

这是代码:

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

如果您需要其他任何东西来帮助我解决这个问题,请询问。谢谢你

4

1 回答 1

0

您可以使用“DataTable”更新表中的 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)

我希望这有帮助

于 2014-09-06T06:13:07.980 回答