将MS Access表复制到不同的MS Access

时间:2015-09-28 20:38:27

标签: vb.net copy

请帮忙;我想将MS Access表及其数据复制到不同MS Access数据库的另一个表中。我遇到了数百个必须重新格式化的访问数据库,我决定编写VB.Net代码并格式化一个表,以便可以将其逐个复制到其他数百个格式化。

这是我的试用代码:

Dim cmdCreate as New OleDb.OleDbCommand("SELECT * INTO [Hierarchy] IN "c:\hierarchy.pdb" FROM [Hierarchy]  WHERE 0=1",  con1)

cmdCreate.ExecuteNonQuery

注意:con1数据源与table2目录相同。

1 个答案:

答案 0 :(得分:0)

这是 Access 中的一个函数,您可以使用适当的参数进行循环。 dbSource 和 dbTarget 是完整路径名。

Public Function ExportTableToDatabase (TableName As String, dbSource As String, dbTarget As String, Optional Append As Boolean = False) As Boolean
    On Error GoTo Error_Trap
    Dim SQL As String
    Dim db As DAO.Database
    Set db = CurrentDb()
    
    If Append Then
        'The sql will use APPEND TO TABLE syntax
        SQL = "INSERT INTO " & TableName & " IN """ & dbTarget & """ " _
            & "SELECT " & TableName & ".* FROM " & TableName & " IN """ & dbSource & """ "
    
    Else
        'The sql will use CREATE TABLE syntax
        SQL = "SELECT " _
            & TableName & ".* INTO " & TableName & " " _
            & "IN """ & dbTarget & """ " _
            & "FROM " & TableName & " " _
            & "IN """ & dbSource & """ "
    
    End If
    
    db.Execute SQL, dbFailOnError + dbSeeChanges

    ExportTableToDatabase = True
    
Cleanup:
    set db = nothing
    Exit Function

Error_Trap:
    msgbox Err.Number, Err.Description
    Resume Cleanup
    Resume
End Function