2016-07-26 7 views
1

다른 위치에 다른 연결을 가진 2 개의 MS Access 데이터베이스가 있습니다. 첫 번째 데이터베이스의 테이블에서 레코드를 삭제하는 것은 작동하지만 두 번째 데이터베이스의 테이블에서 복사 된 새 레코드를 삽입하는 것은 작동하지 않습니다. 첫 번째 데이터베이스의 테이블은 비어 있습니다. 두 번째 데이터베이스의 테이블에 새 레코드가 삽입 된 이유는 두 번째 데이터베이스가 복사되어 자신에게 삽입 된 레코드가 많기 때문입니다. 내가 원하는 것은 두 번째 데이터베이스의 복사 된 레코드가 첫 번째 데이터베이스에 붙여 넣기/삽입된다는 것입니다.한 테이블의 레코드를 다른 연결 문자열을 가진 다른 데이터베이스 테이블로 복사하는 방법은 무엇입니까?

SQLStr1는 첫 번째 데이터베이스 SQLStr2의 위치 문자열은

Dim conn As OleDbConnection 
    Dim conn2 As OleDbConnection 

    Dim cmd As OleDbCommand 
    Dim cmd2 As OleDbCommand 
    Dim SQLStr1 As String 
    Dim SQLStr2 As String 

    conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=psipop.accdb;Jet OLEDB:Database Password=cscfo13poppsi;") 
    'I used 'psipop' because the database is located the same with the application. 
    SQLStr1 = "DELETE * FROM pop 'psipop'" 
    conn.Open() 

    cmd = New OleDbCommand(SQLStr1, conn) 
    cmd.ExecuteNonQuery() 

    'I used " & TextBox3.Text & " because the textbox3 contains the path of the another database. 
    conn2 = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= '" & TextBox3.Text & "' ;Jet OLEDB:Database Password=cscfo13poppsi; ") 

    SQLStr2 = "INSERT INTO pop SELECT * FROM pop IN '" & TextBox3.Text & "'" 
    conn2.Open() 
    cmd2 = New OleDbCommand(SQLStr2, conn2) 
    cmd2.ExecuteNonQuery() 

이 저를 도와주세요 번째 databas의 위치 문자열입니다. 고맙습니다.

답변

0

예. 하나의 데이터베이스에서 DataTable을 채우고 다른에 행을 삽입합니다

Using sourceConnection As New OleDbConnection("source connection string here"), 
     destinationConnection As New OleDbConnection("source connection string here"), 
     insertCommand As New OleDbCommand("INSERT statement here", destinationConnection), 
     adapter As New OleDbDataAdapter("SELECT statement here", sourceConnection) With {.InsertCommand = insertCommand, 
                         .AcceptChangesDuringFill = False} 
    Dim table As New DataTable 

    adapter.Fill(table) 
    adapter.Update(table) 
End Using 

. 적절한 매개 변수를 사용하여 insertCommand을 구성해야합니다. AcceptChangesDuringFill 속성에 특히주의하십시오. DataRowsRowStateAdded이므로 삽입 할 준비가되었습니다. 이 값이 없으면 RowState 속성은 모두 Unchanged이고 아무 것도 저장되지 않습니다.