Solved

Comparing 2 datatables

Posted on 2003-10-27
7
224 Views
Last Modified: 2010-04-16
I have created a dataset, added one table(Table1) to it, which is populated with the details of the folders in a particular directory. I want to add another table(Table2) which will get folder details from a table in a sql database. I would then like to compare the 2 tables and if there are rows(based on column -foldername), that are present in Table2 but not in Table1 then append only those(that are present in Table2 but not in Table1) to Table1. I would then bind Table 1 to a grid. What would be a good way to achieve the above. Code samples will be really helpful.
Thanks in advance.
0
Comment
Question by:Rintu
  • 5
  • 2
7 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 9630375


                  DataTable dt1 = new DataTable();
                  DataTable dt2 = new DataTable();



                  //Load Data into DataTables


                  DataRow resultant;
                  
                  foreach(DataRow dr in dt2.Rows)
                  {
                        resultant = dt1.Rows.Find(dr["foldername"].ToString());
                        if(resultant == null)
                              dt1.Rows.Add(dr);
                  }

                  dataGrid1.DataSource = dt1;
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9630396


In a nutshell, the above code has two DataTables

dt1 and dt2


the code also creates a new dataRow for temperary storage.


it then loops through each data row in dt2 and grabs the data from column "foldername" and tries to find that value in data table 1 (dt1).

if it finds the value then it sets the resultant (a datarow) to the value of the current datarow.  if it does not find a value, it sets the resultant to null.

If the resultant is null (i.e. the row was not found in dt1) then the current datarow in dt2 is added to dt1.

0
 

Author Comment

by:Rintu
ID: 9635618
The above solution does not seem to work in my case as the col. FolderName is not the primarykey. Can you please suggest an alternative  ?
Thanks in advance.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 22

Expert Comment

by:_TAD_
ID: 9635736


Do you have a primary key specified?

If not, the only alternative I see is to go through each and every directory in both tables and see if that value exists in the other table.

If your data tables have less than 100 rows each then you should not notice a performance hit, but many more than that and you may notice some delay.



I suppose you could create a query string and then execute a filter and see how many rows are available.

I haven't played with the filter command much, but I think it might work.

I'll play around with some code this afternoon and see what I can come up with that doesn't use the "find' function
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 9644377


Are each of the rows in your data table unique?

If the rows are unique, then you can use the row as your key since the Find() function uses an object as a key.


               DataTable dt1 = new DataTable();
               DataTable dt2 = new DataTable();



               //Load Data into DataTables


               DataRow resultant;
               
               foreach(DataRow dr in dt2.Rows)
               {
                    resultant = dt1.Rows.Find(dr);
                    if(resultant == null)
                         dt1.Rows.Add(dr);
               }

               dataGrid1.DataSource = dt1;
0
 
LVL 22

Accepted Solution

by:
_TAD_ earned 250 total points
ID: 9644415


The only problem with the code above is that I don't know if you will ever find a match.  I don't know if the find function is actually using the values of the data row or if it is using the datarow object identifier or possibly its hash value.
0
 

Author Comment

by:Rintu
ID: 9644416
No, the rows are not unique. However I do want to ensure that only rows which have a unique value for the the col.'foldername' are added to the table.
Thanks
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

16 Experts available now in Live!

Get 1:1 Help Now