从DataTable批量插入到Access数据库

时间:2014-06-10 22:52:17

标签: vb.net ms-access

已经填充了DataTable,但需要将其整个表内容转储到Access数据库中。

Using (conn)
    If conn.State <> ConnectionState.Open Then conn.Open()
    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM " & sMdbTableName, conn)
    adapter.InsertCommand = New OleDbCommand("INSERT INTO " & sMdbTableName & " SELECT * FROM " & sMdbTableNameSource & ";", conn)
    adapter.TableMappings.Add(sMdbTableName, ds.Tables.Item(sMdbTableNameSource).ToString)
    adapter.Update(ds.Tables.Item(sMdbTableNameSource))
End Using

我试图做这样的事情,但没有错误,也没有添加数据。需要表现最佳的方式。

由于

编辑:

更新的代码很慢,需要大量类型的访问导入:

        Using (conn)
            If conn.State <> ConnectionState.Open Then conn.Open()
            Dim adapter As OleDbDataAdapter = New OleDbDataAdapter()
            adapter.InsertCommand = New OleDbCommand("INSERT INTO " & sMdbTableName & "(Field1, Field2, Field3, Field4, Field5) VALUES(@Field1, @Field2, @Field3, @Field4, @Field5);", conn)
            adapter.InsertCommand.Parameters.Add("Field1", OleDbType.VarChar, 16, "Field1")
            adapter.InsertCommand.Parameters.Add("Field2", OleDbType.VarChar, 16, "Field2")
            adapter.InsertCommand.Parameters.Add("Field3", OleDbType.VarChar, 16, "Field3")
            adapter.InsertCommand.Parameters.Add("Field4", OleDbType.VarChar, 16, "Field4")
            adapter.InsertCommand.Parameters.Add("Field5", OleDbType.VarChar, 16, "Field5")
            adapter.TableMappings.Add(sMdbTableName, ds.Tables.Item(sMdbTableNameSource).ToString)
            adapter.Update(ds.Tables.Item(sMdbTableNameSource))
        End Using

3 个答案:

答案 0 :(得分:1)

问题在于,RowStateDataRow的{​​{1}} DataTableUnchanged,因此当您致电Update时,系统会忽略它们。如果要插入这些行,则其RowState必须为Added。您可以通过在调用AcceptChangesDuringFill之前将数据适配器的False属性设置为Fill来实现此目的。这会阻止在填充AcceptChanges后调用DataTable,这会将RowState属性从Added更改为Unchanged。如果那不是一个选项,那么你必须遍历这些行并在每个行上调用SetAdded

答案 1 :(得分:0)

DataTables基本上使用所有逐行处理。

因此,如果您需要从.net代码推送到Access,循环遍历表行并使用行中的值运行一个简单的Update语句 - 在单独的命令中 - 并不是最糟糕的事情。

插入/更新&#34;语句&#34;在DataTable中将导致相同的行为。因此,如果您感到疲倦并无法让他们工作,您可以挽救单独的更新:)。

如果您要使用SQLServer,SQLBulkCopy对象将是完美的,应该更快。不幸的是,我不知道Access这样的对象 这将是搜索的内容。

答案 2 :(得分:0)

Private Sub CopyDatatableToAccess(dt As DataTable, AccsessAddress As String, TableName As String)
    Try
        Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & AccsessAddress & ";Persist Security Info=False;"
        Dim accConnection As New OleDb.OleDbConnection(connString)
        Dim selectCommand As String = "SELECT <field1>, <field2> and so on FROM <table>" & TableName
        Dim accDataAdapter As New OleDb.OleDbDataAdapter(selectCommand, accConnection)
        Dim accCommandBuilder As New OleDb.OleDbCommandBuilder()
        accDataAdapter.InsertCommand = accCommandBuilder.GetInsertCommand()
        accDataAdapter.UpdateCommand = accCommandBuilder.GetUpdateCommand()
        Dim accDataTable As DataTable = dt.Copy()
        ''Just to make sure, set the RowState to added to make sure an Insert is performed'
        For Each row As DataRow In accDataTable.Rows '
            If row.RowState = DataRowState.Added Or DataRowState.Unchanged Then '
                row.SetAdded() '
            End If '
        Next '
        accDataAdapter.Update(accDataTable)
    Catch ex As Exception
    End Try
End Sub