Solved

Urgent: Compare two DataTables

Posted on 2004-09-01
14
3,023 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:Robinsonx6
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 7

Expert Comment

by:God_Ares
ID: 11950112
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
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 11950222
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.
0
 
LVL 7

Expert Comment

by:God_Ares
ID: 11950323
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:Robinsonx6
ID: 11950336
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
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 11950422
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.
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 11950458
im amazed there is row.compare method!
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 11950466
That makes life easier!
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 11950500
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!!

0
 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11953300
There is a shortcut that you can take...

you can convert the DataTable to XML then compare the XML!
0
 
LVL 7

Accepted Solution

by:
NipNFriar_Tuck earned 500 total points
ID: 11953348
Here is some methods that I wrote for just that reason....

            /// <summary>
            ///  Converts an XML string to a DataSet
            /// </summary>
            /// <param name="sXML"></param>
            /// <returns></returns>
            public static DataSet XMLToDataSet( string sXML ) {
                  DataSet oRet = null;
                  try {
                        System.IO.StringReader StString = new System.IO.StringReader(sXML);
                        oRet = new DataSet();
                        oRet.ReadXml(StString,System.Data.XmlReadMode.ReadSchema);
                  } catch(Exception Err) {
                        throw( new GenericDataAccessException(Err, sXML ));
                  }
                  return oRet;
            }
            /// <summary>
            /// Converts a data set to xml.
            /// </summary>
            /// <param name="DS"></param>
            /// <returns></returns>
            public static string DataSetToXML( DataSet DS ) {
                  string sRet = null;
                  try {
                        System.IO.StringWriter xml_stream = new System.IO.StringWriter();
                        DS.WriteXml(xml_stream,System.Data.XmlWriteMode.WriteSchema);
                        sRet = xml_stream.ToString();
                  } catch(Exception Err) {
                        throw( new GenericDataAccessException(Err, "" ));
                  }
                  return sRet;
            }


The one caviot that you need to be aware of is that a db null converts to a "" (empty string) in xml.
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 11954301
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
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 12034103
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
0
 
LVL 2

Author Comment

by:Robinsonx6
ID: 12199486
Sorry for the delay
0
 

Expert Comment

by:sudgupta
ID: 22033176
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.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question