You could read the source tables in a DataSet
, that would hold the 9 DataTable
objects. The rows in those DataTable
objects would all have a RowState
of Unchanged
. Loop the rows in each DataTable
and use the SetAdded
method on each row.
Once the rows are in an Added
row state you can use the following code block to save them to your 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
This code block can be called to read one of the source tables:
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