有没有快速的方法来使用vb.net更新访问表?

时间:2015-03-13 14:19:46

标签: database vb.net ms-access

我试图从我已设置为访问数据库的vb.net表单中复制数据表。我使用其他用户发布的代码片段(包含一些更改)(Casbar27,Records added to ms access database with vb 2010 not saving)。

    Back_EndDataSet.tblTest.Clear()
    Dim adapter As New Back_EndDataSetTableAdapters.tblTestTableAdapter
    Dim rowCode As DataRow = Back_EndDataSet.tblTest.NewtblTestRefreshxinRow

    For Each row As DataRow In source.Rows
        adapter.Fill(Back_EndDataSet.tblTest)

        rowCode.Item(0) = row.Item(0)
        rowCode.Item(1) = row.Item(1)
        rowCode.Item(2) = row.Item(2)
        rowCode.Item(3) = row.Item(3)
        rowCode.Item(4) = row.Item(4)
        rowCode.Item(5) = row.Item(5)
        rowCode.Item(6) = row.Item(6)
        rowCode.Item(7) = row.Item(7)
        rowCode.Item(8) = row.Item(8)
        rowCode.Item(9) = row.Item(9)
        Back_EndDataSet.tblTest.AddtblTestRow(rowCode)


        adapter.Update(Back_EndDataSet.tblTest)
    Next

虽然这是有效的,但它很慢并且如果它运行了几次就产生同一记录的倍数。我希望这在开始时擦除访问表并重写它,最好在一分钟之内。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

按照@Gord Thompson的建议,我写了OleDbCommand来删除表格,并决定继续使用OleDbCommand重写我的填充方法。它工作得很好,完成速度很快,我不再有重复的记录。谢谢戈德!

Private Sub UpdateDataBase(ByVal source As DataTable)
    Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= H:\AppDev\Visual Studio 2010\Projects\RawFixerTest1\Raw Fixer Back End.accdb")
    Dim cmdClr As New OleDbCommand
    Dim cmdFill As New OleDbCommand
    Dim i = 0

    con.Open()
    cmdClr.CommandText = "DELETE * FROM tblTestRefreshxin"
    cmdClr.Connection = con
    cmdClr.ExecuteNonQuery()

    cmdFill.Connection = con
    For Each row As DataRow In source.Rows
        cmdFill.CommandText = "INSERT INTO tblTestRefreshxin (numCode, featDesc, alphaCode, clientCode, DTMexclude, attrCode1, attrCode2, attrCode3, attrCode4, lineToPrev) VALUES(" & "'" & row.Item(0) & "','" & row.Item(1) & "','" & row.Item(2) & "','" & row.Item(3) & "','" & row.Item(4) & "','" & row.Item(5) & "','" & row.Item(6) & "','" & row.Item(7) & "','" & row.Item(8) & "','" & row.Item(9) & "')"
        cmdFill.ExecuteNonQuery()
    Next
    con.Close()
End Sub