Click here to Skip to main content
15,889,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Developer,
I have to compare 2 table. I want to compare the each record entry of the both table in c# or vb.net coding.If any record is miss from one table to other table then i insert that record on that table.
Any body tell wats logic i implement to compare the each record of 2 table or its greatful for me if anybody share small code for implementing this logic.

In both table have 3 coloum "Trans_Id", "number" ,"Amount"

Thanks,
Ravi Sharma
Posted
Comments
Kschuler 19-Jul-11 15:16pm    
What code do you have so far? What isn't working?

1 solution

You could always run a query that returns only the different records:

SELECT Trans_Id, number ,Amount FROM myTable1
EXCEPT
SELECT Trans_Id, number ,Amount FROM myTable2
This returns the records that are in myTable1 but not in myTable2



"thanks for your reply, but this i have to do with c# coding."

Then it's a simple merge job:

1) Set up two SqlCommand objects, one per table.
2) Return each table, ordered by the three fields in the same order, into a separate reader.
3) Loop round, building two lists of records that need to be added.
3a) If one side runs out, add all remaining records from the other side to it's list.
3b) If Table1 current record is greater than Table2, add Table 2 record to Table1 list and advance Table 2. Do the same for Table2. If they are the same, move both on.
4) When you run out of records, add the appropriate list to the appropriate table.
 
Share this answer
 
v2
Comments
Ravi Sharma 2 19-Jul-11 15:20pm    
thanks for your reply, but this i have to do with c# coding.
wizardzz 19-Jul-11 15:35pm    
Can't your c# code use SQL? This seems like a strange requirement, or is this HW?
Uday P.Singh 19-Jul-11 15:22pm    
my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900