locked
Comparing two datatables RRS feed

  • Question

  • I am new to C#. I am passing in two variables from an SSIS package into two different datatables. I want to validate that the data in table1 is equal to the data in table two. If there is an inequivilency, I want to output it to another datatable. I have 5 columns and about 13 rows of data, so a row by row comparison should work. How should I write this code?

     

    Thursday, May 20, 2010 2:14 PM

Answers

  • yes loop through the rows of One Table and the inner loop will go to the number of columns ,and in the next line you have to check that the 2nd table rows and columns count ,if it not exceeded then check the value in it,

    suppose we have two tables dataTable1,dataTable2

    for(int i = 0 ; i < dataTable1.rows.Count; i ++)

    {

    for(int j = 0 ; j < dataTable1.Columns.Count; j ++)

    {

    if(i <dataTable2.rows.Count && j< dataTable2.Columns.Count)

    {

    // here compare the values on both tables

    }

    }

    }

    // this is all the manual way

    • Proposed as answer by Chao Kuo Wednesday, May 26, 2010 10:35 AM
    • Marked as answer by Chao Kuo Thursday, May 27, 2010 12:29 PM
    Thursday, May 20, 2010 3:04 PM
  • Is your data stored in MS Sequel Server databases or Oracle?

    In any case, if you get your tables into a dataset you could try doing something like this (here I'm assuming that data1 is the name of your dataset and Table1, Table2 and Table3 are the names of the tables therein):

    foreach (DataRow dr in data1.Tables["Table1"].Rows) // For every row in the Table1
    
    {
    
    foreach (DataRow dr2 in data1.Tables["Table2"].Rows)
    
    {
    
    if (dr[0].ToString() != dr2[0].ToString) // if there is no match
    
    {
    
    // do what you need here
    
    }
    
    break; // get out of the inner for loop
    }
    
    }
    
    • Proposed as answer by Chao Kuo Wednesday, May 26, 2010 10:35 AM
    • Marked as answer by Chao Kuo Thursday, May 27, 2010 12:29 PM
    Thursday, May 20, 2010 3:45 PM
  • Hi

    YOu can use writexml method to compare 2 Datatable Easily

     

    Try this code

     

         DataTable dt = new DataTable("dt1");
          DataColumn dc;
          dc = new DataColumn("Id", typeof(int));
          dt.Columns.Add(dc);
          dc = new DataColumn("Name", typeof(string));
          dt.Columns.Add(dc);
    
          DataTable dt1 = new DataTable("dt1");
          dc = new DataColumn("Id", typeof(int));
          dt1.Columns.Add(dc);
          dc = new DataColumn("Name", typeof(string));
          dt1.Columns.Add(dc);
    
          dt.Rows.Add(1, "Hello");
          dt.Rows.Add(2, "Hello");
          dt.Rows.Add(3, "Hello");
          dt.Rows.Add(4, "Hello");
    
          dt1.Rows.Add(1, "Hello");
          dt1.Rows.Add(2, "Hello");
          dt1.Rows.Add(3, "Hello");
          dt1.Rows.Add(4, "Hello1");
    
           System.IO.StringWriter sw = new System.IO.StringWriter();
           dt.WriteXml(sw, XmlWriteMode.IgnoreSchema, false);
           sw.Flush();
    
           System.IO.StringWriter sw1 = new System.IO.StringWriter();
           dt1.WriteXml(sw1, XmlWriteMode.IgnoreSchema, false);
           sw1.Flush();
    
           if (sw.ToString() == sw1.ToString())
           {
             MessageBox.Show("Value Matched");
           }
           else
           {
             MessageBox.Show("Value Not Matched");
           }


    Happy Coding, RDRaja
    • Proposed as answer by Chao Kuo Wednesday, May 26, 2010 10:34 AM
    • Marked as answer by Chao Kuo Thursday, May 27, 2010 12:29 PM
    Thursday, May 20, 2010 5:15 PM

All replies

  • yes loop through the rows of One Table and the inner loop will go to the number of columns ,and in the next line you have to check that the 2nd table rows and columns count ,if it not exceeded then check the value in it,

    suppose we have two tables dataTable1,dataTable2

    for(int i = 0 ; i < dataTable1.rows.Count; i ++)

    {

    for(int j = 0 ; j < dataTable1.Columns.Count; j ++)

    {

    if(i <dataTable2.rows.Count && j< dataTable2.Columns.Count)

    {

    // here compare the values on both tables

    }

    }

    }

    // this is all the manual way

    • Proposed as answer by Chao Kuo Wednesday, May 26, 2010 10:35 AM
    • Marked as answer by Chao Kuo Thursday, May 27, 2010 12:29 PM
    Thursday, May 20, 2010 3:04 PM
  • Is your data stored in MS Sequel Server databases or Oracle?

    In any case, if you get your tables into a dataset you could try doing something like this (here I'm assuming that data1 is the name of your dataset and Table1, Table2 and Table3 are the names of the tables therein):

    foreach (DataRow dr in data1.Tables["Table1"].Rows) // For every row in the Table1
    
    {
    
    foreach (DataRow dr2 in data1.Tables["Table2"].Rows)
    
    {
    
    if (dr[0].ToString() != dr2[0].ToString) // if there is no match
    
    {
    
    // do what you need here
    
    }
    
    break; // get out of the inner for loop
    }
    
    }
    
    • Proposed as answer by Chao Kuo Wednesday, May 26, 2010 10:35 AM
    • Marked as answer by Chao Kuo Thursday, May 27, 2010 12:29 PM
    Thursday, May 20, 2010 3:45 PM
  • Hi

    YOu can use writexml method to compare 2 Datatable Easily

     

    Try this code

     

         DataTable dt = new DataTable("dt1");
          DataColumn dc;
          dc = new DataColumn("Id", typeof(int));
          dt.Columns.Add(dc);
          dc = new DataColumn("Name", typeof(string));
          dt.Columns.Add(dc);
    
          DataTable dt1 = new DataTable("dt1");
          dc = new DataColumn("Id", typeof(int));
          dt1.Columns.Add(dc);
          dc = new DataColumn("Name", typeof(string));
          dt1.Columns.Add(dc);
    
          dt.Rows.Add(1, "Hello");
          dt.Rows.Add(2, "Hello");
          dt.Rows.Add(3, "Hello");
          dt.Rows.Add(4, "Hello");
    
          dt1.Rows.Add(1, "Hello");
          dt1.Rows.Add(2, "Hello");
          dt1.Rows.Add(3, "Hello");
          dt1.Rows.Add(4, "Hello1");
    
           System.IO.StringWriter sw = new System.IO.StringWriter();
           dt.WriteXml(sw, XmlWriteMode.IgnoreSchema, false);
           sw.Flush();
    
           System.IO.StringWriter sw1 = new System.IO.StringWriter();
           dt1.WriteXml(sw1, XmlWriteMode.IgnoreSchema, false);
           sw1.Flush();
    
           if (sw.ToString() == sw1.ToString())
           {
             MessageBox.Show("Value Matched");
           }
           else
           {
             MessageBox.Show("Value Not Matched");
           }


    Happy Coding, RDRaja
    • Proposed as answer by Chao Kuo Wednesday, May 26, 2010 10:34 AM
    • Marked as answer by Chao Kuo Thursday, May 27, 2010 12:29 PM
    Thursday, May 20, 2010 5:15 PM