Click here to Skip to main content
16,019,740 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Hi,

I have same table in both two databases(one is ource and other is target). I want to compare those two tables using C# amd find out which records are inserted and which are updated.Based on that I have to generate the final CSV file or Excel file, which has to be inserted into the target database into the same table.


Thanks in advance.
Posted
Updated 24-Jul-13 6:35am
v2
Comments
Ron Beyer 24-Jul-13 11:45am    
OK, so what is your question? I see what you want to do, but nothing saying what you've tried or where you are stuck?
sivared 24-Jul-13 12:08pm    
I have same table in both two databases(one is ource and other is target). I want to compare those two tables using C# amd find out which records are inserted and which are updated.Based on that I have to generate the final CSV file or Excel file, which has to be inserted into the target database into the same table.
[no name] 24-Jul-13 11:45am    
Okay... and? Did you maybe have some sort of a question?
sivared 24-Jul-13 12:20pm    
I have same table in both two databases(one is ource and other is target). I want to compare those two tables using C# amd find out which records are inserted and which are updated.Based on that I have to generate the final CSV file or Excel file, which has to be inserted into the target database into the same table.
[no name] 24-Jul-13 12:27pm    
So? How is you repeating this over and over make this somehow a question? What is the specific issue that you are having with the code that you have written?

1 solution

You can compare the amount of lines ...

If their are more lines in your new CSV then that means records have been added. This will only work if you know that other data is the same.

C#
[] oldCSV = System.IO.File.ReadAllLines("LOCATION");
[] newCSV = System.IO.File.ReadAllLines("LOCATION");

if ((newCSV.Length - 1) > (oldCSV.Length)) {
    //their are differences so get the new differences
    for (diff = Convert.ToInt32(((newCSV.Length - 1) > (oldCSV.Length))); diff <= newCSV.Length; diff++) {
        //do what you want with the changes
    }
} else {
    //no changes
}


If you want to compare each line to see if a certain value has been changed then you can loop through both of them.

C#
[] oldCSV = System.IO.File.ReadAllLines("LOCATION");
    [] newCSV = System.IO.File.ReadAllLines("LOCATION");

    foreach (object oldRecord_loopVariable in oldCSV) {
        oldRecord = oldRecord_loopVariable;
        foreach (object newRecord_loopVariable in newCSV) {
            newRecord = newRecord_loopVariable;
            if (oldRecord == newRecord) {
            //no record changed
            } else {
                //something is not the same
            }
        }
    }
 
Share this answer
 
Comments
sivared 24-Jul-13 21:35pm    
Thanks. I will try this one.
sivared 24-Jul-13 21:39pm    
If I have two data tables reading from xsd.Each table is same and is getting from different databases in whcih each table has 30 columns, and 28000 records. Tables differ in data in most of the columns. How can I compare the two datatables which I am reading from xsd. and create the final table with all the different records with both inserts and updates into excel sheet. Can I identify them uniquely thiat is updated record and inserted record.
[no name] 25-Jul-13 8:36am    
ill have to look up what xsd is because i am not familiar with it

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