SoFunction
Updated on 2025-03-07

How to compare whether the contents of 2 datatables are the same

///    Compare whether the contents of two DataTables are equal. First, compare the quantity. If the quantity is equal, compare the content.
///   </summary>
///   <param   name= "dtA "> </param>
///   <param   name= "dtB "> </param>
private bool CompareDataTable(DataTable dtA, DataTable dtB)
{
    if ( == )
    {
if (CompareColumn(, ))
{
//Compare content
    for (int i = 0; i < ; i++)
    {
for (int j = 0; j < ; j++)
{
    if (![i][j].Equals([i][j]))
    {
return false;
    }
}
    }
    return true;
}
else
{
    return false;
}
    }
    else
    {
return false;
    }
}
///   <summary>
///    Compare whether the two field collections have names and the data types are the same
///   </summary>
///   <param   name= "dcA "> </param>
///   <param   name= "dcB "> </param>
///   <returns> </returns>
private bool CompareColumn( dcA, dcB)
{
    if ( == )
    {
foreach (DataColumn dc in dcA)
{
//Find the same field name
    if (() > -1)
    {
//Test data type
if ( != dcB[()].DataType)
{
    return false;
}
    }
    else
    {
return false;
    }
}
return true;
    }
    else
    {
return false;
    }
}