Link to home
Start Free TrialLog in
Avatar of Robinsonx6
Robinsonx6Flag for Mauritius

asked on

Urgent: Compare two DataTables

Hi we have two data tables which have the same structure just different data as one is from a local access db, and one is from a remote access db.
            
private DataTable dtlocal = new DataTable();
private DataTable dtRemote = new DataTable();

how do i check if the dtlocal is missing any rows? and
how do i check if the dtlocal has any changes?

Regards and thanks in advance.
Avatar of God_Ares
God_Ares

Robinsonx6,..

imo, you have to write your own compare code,.. sort your data on an primary key so you can make a few assumptions, think about it,..

in other words there is no simple compare,.. you have to write code,...

few hints,..

you could use a hashtable to store rows on id
Robinson6x,

If you are certain the remote datatable is correct, you *could* do something along these lines:

foreach(DataRow dr1 in dtRemote.Rows)
{
      bool found = false;
      foreach(DataRow dr2 in dtLocal.Rows)
      {
            if(dr2["key"] == dr1["key"])
                  found = true;
      }
      if(found)
            continue;
      else
            Console.WriteLine(dr1["key"].ToString() + " not found.");
}

This does assume that if the 'key' column is the same then the rest of the columns are...

Wint.
Avatar of Robinsonx6

ASKER

God Ares i knew id have to write a load of code for this one and thanks for your comments. thanks winterMuteUK this is the way i was hoping i didnt have to do it.

:-)

With Thanks

Ill leave this open for a bit incase anyone else has any bright ideas but i think the points are yours winter
Yup, I understand, it's by no means an ideal solution... I spose there are things you can do to make it more efficient, checking the count of the rows first etc... but still - it's not as nice as a simple 'Equals' method!

Sorry it's not more help!

Wint.
im amazed there is row.compare method!
That makes life easier!
think i will need to write myown 'row.compare(Row MasterRow,Row SlaveRow) method, think i might go to the pub first and get pissed though, not looking forward to this!!

There is a shortcut that you can take...

you can convert the DataTable to XML then compare the XML!
ASKER CERTIFIED SOLUTION
Avatar of NipNFriar_Tuck
NipNFriar_Tuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ah ok this seems interesting thanks NipNfriar Tuck, i am trying it this evening

will post my points in the morning once i know what is happeneing

Thanks
Hi i havnt forgot this, sorry for the deelay im currently in a different country, i will get  round to this shortly on my return
Thanks for your patience
Sorry for the delay
I have to compare two large data table which contains millions of records.your method takes lot of time to compare.

So the problem is that is there a way to compare and find difference by executing a single Query.
like we do in SQL.