Comparing Two DataTables

  • Thread starter Thread starter c_shah
  • Start date Start date
C

c_shah

Scenario: I have to update my SQL server table from an ORACLE database
to keep my SQL table in sync with ORACLE.

I have one data table that is populated form ORACLE and other one is
populating from SQL server (Both data tables are part of the same
dataset). How to programmatically compare two tables and update the SQL
server data table?

Thanks in advance.
 
Other than data, will the comparison need to include schema differences
between the two tables? If so, does it need to update the columns as
necessary in the SQL table?

If its only data, then you can use the datatable's find method to pull
back each row. Then iterate through the columns collection against both
rows and check the individual values.

Ie.

Dim dtOracle as datatable = dataset.Tables("OracleTable")
Dim dtSQL as datatable = dataset.Tables("SQLTable")
for each oracleRow as DataRow in dtOracle.Rows
Dim sqlRows() as datarow =
dtSQL.Find(CINT(oracleRow("primarykeycolumnname")))
if sqlRows.length > 0 then
for each field as datacolumn in dtOracle.Columns
if sqlRows(0)(field.Name) <> oracleRow(field.Name) then
sqlRows(0)(field.Name) = oracleRow(field.Name)
end if
next
end if
next



Something like that. My code is probably wrong, so look at it as pseudo
code!
 
Hi,

This completely depend on how independen the data is from the time, is that
not than you need beside a kind of code as Stevens pseudo code a timestamp
in all datarows that you want to merge.

I hope this helps,

Cor
 
Back
Top