Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Urgent: Compare two DataTables

Posted on 2004-09-01
14
Medium Priority
?
3,028 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

597 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