Solved

Urgent: Compare two DataTables

Posted on 2004-09-01
14
3,020 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now