Solved

Urgent: Compare two DataTables

Posted on 2004-09-01
14
3,025 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
Independent Software Vendors: 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 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

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…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

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