locked
how to compare the structure of two DataTables in C# RRS feed

  • Question

  • User1094269964 posted

    Hi Friends,


    We are doing oracle bulk upload functionality.


    Our application will let the user to generate the excel template.


    We will  do "select * from table where 1>1" with that metadata we will create the excelfile. 


    User will use the excel file and fill the data and upload the file.


    In our application we will read the uploaded excel file and create DataTable.


    Now here I want to compare DataTable created from the user uploaded excel data with the DataTable created using "select * from table where 1>1"


    Here we want to compare


    1)These two DataTables has same number of columns
    2)These two DataTables has same number of columns and it has same name?
    3)These two DataTables has same number of columns and it has same name and it has same datatype?


    I can do oracle bulkupload if the above 3 validations are success


    How can i do the above. Please help

    Wednesday, July 29, 2020 7:36 AM

All replies

  • User1535942433 posted

    Hi bsurendiran,

    1)These two DataTables has same number of columns

    You could compare two datatable columns' count.Just like this:

    if (dta.Columns.Count != dtb.Columns.Count)
     {
     ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('These datatable have different column number!');", true);
    }

    2)These two DataTables has same number of columns and it has same name?

    You could check  wheather one datatable column contains another datatable column name.Just like this:

       for (int i = 0; i < dta.Columns.Count; i++)
                {
                    if (!dtc.Columns.Contains(dta.Columns[i].ColumnName))
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('These datatable have different column name!');", true);
                    }
                }

    3)These two DataTables has same number of columns and it has same name and it has same datatype?

    You could compare two datatable column's datatype.Just like this:

      for (int i = 0; i < dta.Columns.Count; i++)
                {
                    for (int j = 0; j < dtd.Columns.Count; j++)
                    {
                        if (dta.Columns[i].DataType!=dtd.Columns[j].DataType)
                        {
                            ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('These datatable have different column datatype!');", true);
                        }
                    }
                }

    Best regards,

    Yijing Sun

    Thursday, July 30, 2020 9:43 AM