2014-05-15 54 views
0

我使用VB.Net並需要將9個表從ODBC連接(Gupta數據庫)複製到Access數據庫。 我不能使用sql語句SELECT INTO .....因爲odbc連接不支持這個。 在VB6中我用:VB.net從1數據庫(ODBC)複製到另一個(Access)而不使用select into

Set rsCopierenTabel = New Recordset 
    sqlCopierenTabel = "SELECT * FROM " & tblTabel 
    rsCopierenTabel.Open sqlCopierenTabel, conUnit4, adOpenKeyset, adLockOptimistic 

    If rsCopierenTabel.RecordCount > 0 Then 
     rsCopierenTabel.MoveFirst 

     Set rsPlakkenTabel = New Recordset 
     sqlPlakkenTabel = "SELECT * FROM " & strTabel 
     rsPlakkenTabel.Open sqlPlakkenTabel, conAccess, adOpenKeyset, adLockOptimistic 

      Do Until rsCopierenTabel.EOF 

       rsPlakkenTabel.AddNew 

        For i = 0 To rsCopierenTabel.Fields.Count - 1 

         rsPlakkenTabel.Fields(i) = rsCopierenTabel.Fields(i) 

        Next i 

        rsPlakkenTabel.Update 

      rsCopierenTabel.MoveNext 
      Loop 

     rsPlakkenTabel.Close 
     Set rsPlakkenTabel = Nothing 

    End If 

    rsCopierenTabel.Close 
    Set rsCopierenTabel = Nothing 

但是,這是VB6,現在我使用VB.Net。 是不是有一個簡單的方法來做到這一點? 我已經查看了具有鏈接到連接的數據集,但始終與填充它們的連接相同。

難道有人能指點我嗎? 我不想循環列,因爲我不知道列的名稱。 (這就是爲什麼域(i)是那麼容易)

由於提前, 布賴恩

回答

0

你可以在閱讀DataSet源表,這將保持9個DataTable對象。那些DataTable對象中的行將全部具有RowStateUnchanged。循環每個DataTable中的行,並在每一行上使用SetAdded方法。

一旦行是在Added行狀態,你可以使用下面的代碼塊將它們保存到您的DB:

Private Sub UpdateAddedRows(MyDbAdapter As DbDataAdapter, dt As DataTable) 
    Dim AddedRows As DataRow() = dt.[Select](Nothing, Nothing, DataViewRowState.Added) 
    If AddedRows.Length > 0 Then 
    Dim InsertCommand As DbCommand = NewDbCommand() 
    Dim SQL As String = "INSERT INTO " & Convert.ToString(dt.TableName) & " (" 
    Dim Vals As String = "" 
    Dim dc As List(Of String) = ColumnList(dt) 
    For dcInx As Integer = 0 To dc.Count - 1 
     Dim p As DbParameter = DataColumnParameter(dt.Columns(dc(dcInx))) 
     InsertCommand.Parameters.Add(p) 
     If dcInx > 0 Then 
      SQL += ", " 
      Vals += ", " 
     End If 
     SQL += dc(dcInx) 
     Vals += ParamNameDecorated(p.ParameterName) 
    Next 
    SQL += ") VALUES (" & Vals & ")" 

    InsertCommand.CommandText = SQL 
    MyDbAdapter.InsertCommand = InsertCommand 
    MyDbAdapter.Update(AddedRows) 
    End If 
End Sub 

該代碼可以調用塊讀取源表中的一個:

Private Function GetData(ByRef ds As DataSet, TableName As String, SQL As String, pst As Boolean) As DataSet 
    Dim MyDbAdapter As DbDataAdapter = NewDbDataAdapter() 
    Dim MyDbCommand As DbCommand = NewDbCommand() 
    MyDbCommand.CommandText = SQL 

    If ds Is Nothing Then 
    ds = New DataSet() 
    End If 
    If ds.Tables.Contains(TableName) Then 
    ds.Tables.Remove(TableName) 
    End If 
    Dim dt As New DataTable(TableName) 
    ds.Tables.Add(dt) 

    MyDbAdapter.SelectCommand = MyDbCommand 
    MyDbAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey 

    Try 
    MyDbAdapter.Fill(ds, TableName) 
    Catch ex As Exception 
    Debug.Print("Exception getting data. " + ex.Message) 
    Throw 
    End Try 
    Return ds 
End Function 
+0

謝謝,我有一個原始數據庫表的數據表,但沒有更改rowstate,所以當我將表保存到新數據庫時表未更新。現在我知道爲什麼。 – Brian

相關問題