Solved

Urgent: Compare two DataTables

Posted on 2004-09-01
14
3,022 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 82
C#  Radio button search for Date not DateTime 4 46
SSRS subreport with parameter 3 34
C# winforms programmitically move panels 6 29
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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