2

私のコードは古いレコードを更新する必要があり、同時に新しいレコードが見つかった場合は、同様にDBに挿入する必要があります...この方法を実行する際にテーブルアダプターを使用しています。

コードは次のとおりです。

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 に答える