Compare whether the contents of two DataTables are equal. First compare the quantity. If the quantity is equal, compare the contents.

黄舟
Release: 2017-02-16 11:33:53
Original
1424 people have browsed it

    #region 比较两个DataTable内容是否相等,先是比数量,数量相等就比内容
        ///   <summary> 
        ///   比较两个DataTable内容是否相等,先是比数量,数量相等就比内容 
        ///   </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))
                {
                    //比内容 
                    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> 
        ///   比较两个字段集合是否名称,数据类型一致 
        ///   </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)
                {
                    //找相同字段名称 
                    if (dcB.IndexOf(dc.ColumnName) > -1)
                    {
                        //测试数据类型 
                        if (dc.DataType != dcB[dcB.IndexOf(dc.ColumnName)].DataType)
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
Copy after login

The above is to compare whether the contents of two DataTables are equal. First, compare the quantity. If the quantity is equal, compare the content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template