当前位置:编程学习 > C#/ASP.NET >>

比较DataTable中新旧数据


内容不写了,代码上都做了写注释。

  1   /**//// <summary>
  2        /// 比较两个数据表,并返回比较结果表
  3        /// 比较条件:
  4        /// 1.两个表结构相同;
  5        /// 2.两个表排序都是按主键顺序排序;
  6        /// 3.两个表都不为空;
  7        /// </summary>
  8        /// <param name="newTable">新数据</param>
  9        /// <param name="oldTable">旧数据</param>
 10        /// <param name="parmaryKey">表主键名称</param>
 11        /// <param name="colState">状态列名称</param>
 12        /// <param name="stateValue">不相同时状态值</param>
 13        /// <returns>结果数据表  </returns>
 14        public static DataTable CompareData(
 15            this DataTable newTable,
 16            DataTable oldTable,
 17            string parmaryKey,
 18            string colState,
 19            object stateValue)
 20        {
 21            int newTableCount = newTable.Rows.Count;
 22            int oldTableCount = oldTable.Rows.Count;
 23
 24            /**/////比较结果表
 25            DataTable resultTable = newTable.Clone();
 26
 27            int i = 0, j = 0;
 28            do
 29            {
 30                DataRow dr1 = null;
 31                DataRow dr2 = null;
 32
 33                /**/////结果行
 34                DataRow rDr = resultTable.NewRow();
 35
 36                /**/////如果超出旧表,则新表所有其他列进行添加
 37                if (j < oldTableCount)
 38                {
 39                    dr2 = oldTable.Rows[j];
 40                }
 41                else
 42                {
 43                    for (int k = i; k < newTableCount; k++)
 44                    {
 45                        DataRow rDr2 = resultTable.NewRow();
 46                        rDr2.ItemArray = newTable.Rows[k].ItemArray;
 47                        resultTable.Rows.Add(rDr2);
 48                    }
 49                    /**/////添加完成后退出
 50                    break;
 51                }
 52
 53                /**/////如果超出新表,则将旧表剩余数据设置状态值并添加到结果表中
 54                if (i < newTableCount)
 55                {
 56                    dr1 = newTable.Rows[i];
 57                }
 58                else
 59                {
 60                    for (int k = j; k < oldTableCount; k++)
 61                    {
 62                        DataRow rDr2 = resultTable.NewRow();
 63                        rDr2.ItemArray = oldTable.Rows[k].ItemArray;
 64                        rDr2[colState] = stateValue;
 65                        resultTable.Rows.Add(rDr2);
 66                    }
 67                    /**/////添加完成后退出
 68                  &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,