从数据集添加新记录

时间:2015-09-24 14:30:34

标签: html vb.net ms-access dataset

我是新手,所以我要求一些余地和理解。我正在使用VB.NET来筛选网站上的表格并将其放入数据集中。我可以把它DataGridView并使用Insert将数据集保存到MS Access数据库。当我第二天使用该程序并添加新记录时,会出现问题。我无法弄清楚如何仅添加新记录。有人可以帮我弄清楚如何只将新记录添加到MS Access数据库而不是完整的数据集吗?

这是我的Insert代码:

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    Try
        con2.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=O:\Maintenance\XXXXXX.accdb;"
        com2.Connection = con2

        com2.CommandText = "INSERT INTO INCOMING2 ([DriverNumber],[Name],[Terminal],[Region],[PostDate],[InspectionDate],[Citation],[InspState],[UnitType],[InspLevel],[Basic],[Section],[Description],[OOS],[Severity],[OOSWeight],[TimeWeight],[Points],[License],[State],[VIN],[Unit],[CostCenter],[UnitDesc],[HomeTerm],[TerminalName],[ShopEmail],[TerminalEmail],[TermMgr],[Count]) VALUES " & _
                                     "(@DriverNumber,@Name,@Terminal,@Region,@PostDate,@InspectionDate,@Citation,@InspState,@UnitType,@InspLevel,@Basic,@Section,@Description,@OOS,@Severity,@OOSWeight,@TimeWeight,@Points,@License,@State,@VIN,@Unit,@CostCenter,@UnitDesc,@HomeTerm,@TerminalName,@ShopEmail,@TerminalEmail,@TermMgr,@Count)"
        con2.Open()
        For Each dr As DataRow In table.Rows
            With com2.Parameters
                .Clear()
                .AddWithValue("@DriverNumber", dr("Driver Number"))
                .AddWithValue("@Name", dr("Name"))
                .AddWithValue("@Terminal", dr("Terminal"))
                .AddWithValue("@Region", dr("Region"))
                .AddWithValue("@PostDate", Convert.ToDateTime(dr("Post Date")))
                .AddWithValue("@InspectionDate", Convert.ToDateTime(dr("Inspection Date")))
                .AddWithValue("@Citation", dr("Citation"))
                .AddWithValue("@InspState", dr("Insp State"))
                .AddWithValue("@UnitType", dr("Unit Type"))
                .AddWithValue("@InspLevel", dr("Insp Level"))
                .AddWithValue("@Basic", dr("Basic"))
                .AddWithValue("@Section", dr("Section"))
                .AddWithValue("@Description", dr("Description"))
                .AddWithValue("@OOS", dr("OOS"))
                .AddWithValue("@Severity", dr("Severity"))
                .AddWithValue("@OOSWeight", dr("OOS Weight"))
                .AddWithValue("@TimeWeight", dr("Time Weight"))
                .AddWithValue("@Points", dr("Points"))
                .AddWithValue("@License", dr("License"))
                .AddWithValue("@State", dr("State"))
                .AddWithValue("@VIN", dr("VIN"))
                .AddWithValue("@Unit", dr("Unit"))
                .AddWithValue("@CostCenter", dr("Cost Center"))
                .AddWithValue("@UnitDesc", dr("Unit Descr"))
                .AddWithValue("@HomeTerm", dr("Home Term"))
                .AddWithValue("@TerminalName", dr("Terminal Name"))
                .AddWithValue("@ShopEmail", dr("Shop E-mail"))
                .AddWithValue("@TerminalEmail", dr("Terminal E-mail"))
                .AddWithValue("@TermMgr", dr("Term Mgr"))
                .AddWithValue("@Count", dr("Count"))
            End With
            'com2 = New OleDb.OleDbCommand(Query, con2)
            com2.ExecuteNonQuery()

        Next
        MessageBox.Show("Captured Data has Updated Incoming CSA", "Required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        con2.Close()


    Catch ex As Exception
        MsgBox(ex.ToString)
    Finally
        If con2.State = ConnectionState.Open Then
            con2.Close()
        End If
        ds.Tables.Clear()
    End Try
End Sub

1 个答案:

答案 0 :(得分:1)

尝试

        con2.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=O:\Maintenance\BWALLER\CSA AUTO\CSA.accdb;"
        com2.Connection = con2

        com2.CommandText = "DELETE FROM T_INCOMING2"
        con2.Open()
        com2.ExecuteNonQuery()
        con2.Close()
        com2.Connection = con2

        com2.CommandText = "DELETE FROM INCOMING3"
        con2.Open()
        com2.ExecuteNonQuery()
        con2.Close()
        com2.Connection = con2


        com2.CommandText = "INSERT INTO T_INCOMING2 ([DriverNumber],[Name],[Terminal],[Region],[PostDate],[InspectionDate],[Citation],[InspState],[UnitType],[InspLevel],[Basic],[Section],[Description],[OOS],[Severity],[OOSWeight],[TimeWeight],[Points],[License],[State],[VIN],[Unit],[CostCenter],[UnitDesc],[HomeTerm],[TerminalName],[ShopEmail],[TerminalEmail],[TermMgr],[Count]) VALUES " & _
                                     "(@DriverNumber,@Name,@Terminal,@Region,@PostDate,@InspectionDate,@Citation,@InspState,@UnitType,@InspLevel,@Basic,@Section,@Description,@OOS,@Severity,@OOSWeight,@TimeWeight,@Points,@License,@State,@VIN,@Unit,@CostCenter,@UnitDesc,@HomeTerm,@TerminalName,@ShopEmail,@TerminalEmail,@TermMgr,@Count) "
        con2.Open()
        For Each dr As DataRow In table.Rows
            With com2.Parameters
                .Clear()
                .AddWithValue("@DriverNumber", dr("Driver Number"))
                .AddWithValue("@Name", dr("Name"))
                .AddWithValue("@Terminal", dr("Terminal"))
                .AddWithValue("@Region", dr("Region"))
                .AddWithValue("@PostDate", Convert.ToDateTime(dr("Post Date")))
                .AddWithValue("@InspectionDate", Convert.ToDateTime(dr("Inspection Date")))
                .AddWithValue("@Citation", dr("Citation"))
                .AddWithValue("@InspState", dr("Insp State"))
                .AddWithValue("@UnitType", dr("Unit Type"))
                .AddWithValue("@InspLevel", dr("Insp Level"))
                .AddWithValue("@Basic", dr("Basic"))
                .AddWithValue("@Section", dr("Section"))
                .AddWithValue("@Description", dr("Description"))
                .AddWithValue("@OOS", dr("OOS"))
                .AddWithValue("@Severity", dr("Severity"))
                .AddWithValue("@OOSWeight", dr("OOS Weight"))
                .AddWithValue("@TimeWeight", dr("Time Weight"))
                .AddWithValue("@Points", dr("Points"))
                .AddWithValue("@License", dr("License"))
                .AddWithValue("@State", dr("State"))
                .AddWithValue("@VIN", dr("VIN"))
                .AddWithValue("@Unit", dr("Unit"))
                .AddWithValue("@CostCenter", dr("Cost Center"))
                .AddWithValue("@UnitDesc", dr("Unit Descr"))
                .AddWithValue("@HomeTerm", dr("Home Term"))
                .AddWithValue("@TerminalName", dr("Terminal Name"))
                .AddWithValue("@ShopEmail", dr("Shop E-mail"))
                .AddWithValue("@TerminalEmail", dr("Terminal E-mail"))
                .AddWithValue("@TermMgr", dr("Term Mgr"))
                .AddWithValue("@Count", dr("Count"))
            End With

            com2.ExecuteNonQuery()
        Next
        con2.Close()
        com2.CommandText = "INSERT INTO INCOMING2 " & _
                            "SELECT T_INCOMING2.* " & _
                            "FROM INCOMING2 RIGHT JOIN T_INCOMING2 ON INCOMING2.[Name] = T_INCOMING2.[Name] AND INCOMING2.[PostDate] = T_INCOMING2.[PostDate] AND INCOMING2.[Section] = T_INCOMING2.[Section] " & _
                            "WHERE ((((T_INCOMING2.[Name]) Not In (INCOMING2.[Name])) And ((T_INCOMING2.[PostDate]) Not In (INCOMING2.[PostDate])) And ((T_INCOMING2.[Section]) Not In (INCOMING2.[Section]))));"

        con2.Open()
        com2.ExecuteNonQuery()
        con2.Close()

        com2.CommandText = "INSERT INTO INCOMING3 " & _
                            "SELECT * " & _
                            "FROM INCOMING2;"
        con2.Open()
        com2.ExecuteNonQuery()
        con2.Close()
        com2.Connection = con2

        MessageBox.Show("Captured Data has Updated Incoming CSA", "Required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

    Catch ex As Exception
        MsgBox(ex.ToString)
    Finally
        If con2.State = ConnectionState.Open Then
            con2.Close()
        End If
        ds.Tables.Clear()
    End Try