Compare two DataTable contents to be equal, first than the quantity, the quantity equal than the content

Source: Internet
Author: User
#region Compare whether the contents of two DataTables are equal. First, compare the quantity. If the quantity is equal, compare the contents.
        /// <summary>
        /// Compare whether the contents of two DataTables are equal. First, compare the quantity. If the quantity is equal, compare the contents.
        /// </ summary>
        /// <param name = "dtA"> </ param>
        /// <param name = "dtB"> </ param>
        public static bool CompareDataTable (DataTable dtA, DataTable dtB)
        {
            if (dtA.Rows.Count == dtB.Rows.Count)
            {
                if (CompareColumn (dtA.Columns, dtB.Columns))
                {
                    // than content
                    for (int i = 0; i <dtA.Rows.Count; i ++)
                    {
                        for (int j = 0; j <dtA.Columns.Count; j ++)
                        {
                            if (! dtA.Rows [i] [j] .Equals (dtB.Rows [i] [j]))
                            {
                                return false;
                            }
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// compare whether the two field collections have the same data type
        /// </ summary>
        /// <param name = "dcA"> </ param>
        /// <param name = "dcB"> </ param>
        /// <returns> </ returns>
        private static bool CompareColumn (System.Data.DataColumnCollection dcA, System.Data.DataColumnCollection dcB)
        {
            if (dcA.Count == dcB.Count)
            {
                foreach (DataColumn dc in dcA)
                {
                    // Find the same field name
                    if (dcB.IndexOf (dc.ColumnName)> -1)
                    {
                        // Test data type
                        if (dc.DataType! = dcB [dcB.IndexOf (dc.ColumnName)]. DataType)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion 


The above is the comparison of two DataTable content is equal, first than the quantity, the quantity is equal to the content content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.