比较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                    break;
 69                }
 70
 71                int result = string.Compare(dr1[parmaryKey].ToString(), dr2[parmaryKey].ToString(), true);
 72
 73                switch (result)
 74                {
 75                    /**/////新表小于则移动新表
 76                    case -1:
 77                        i++;
 78                        rDr.ItemArray = dr1.ItemArray;
 79                        resultTable.Rows.Add(rDr);
 80                        break;
 81
 82                    /**/////相同加入结果,两个表同时向下移动
 83                    case 0:
 84                        i++; j++;
 85                        rDr.ItemArray = dr1.ItemArray;
 86                        resultTable.Rows.Add(rDr);
 87                        break;
 88
 89                    /**/////新表大于则移动旧表
 90                    case 1:
 91                        j++;
 92                        rDr.ItemArray = dr2.ItemArray;
 93                        rDr[colState] = stateValue;
 94                        resultTable.Rows.Add(rDr);
 95                        break;
 96
 97                }
 98            } while (i <= newTableCount);
 99
100            return resultTable;}
 

posted @ 2014-02-26 13:12  zzg168  阅读(178)  评论(0编辑  收藏  举报