Compare new and old data in a able

Source: Internet
Author: User


NO content is written, and comments are written on the code.

1/** // <summary>
2 // compare two data tables and return the comparison result table
3 // comparison conditions:
4 // 1. The two tables have the same structure;
5 // 2. Both tables are sorted by the primary key order;
6 // 3. Both tables are not empty;
7 /// </summary>
8 /// <param name = "newTable"> new data </param>
9 /// <param name = "oldTable"> old data </param>
10 /// <param name = "parmaryKey"> table primary key name </param>
11 /// <param name = "colState"> name of the Status column </param>
12 /// <param name = "stateValue"> Status values of different States </param>
13 /// <returns> result table </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/** // compare the result table
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/** // result line
34 DataRow rDr = resultTable. NewRow ();
35
36/*** // if the number of columns in the old table exceeds the limit, add all other columns in the new table.
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/** // exit after adding
50 break;
51}
52
53/** // if the new table is exceeded, set the status value for the remaining data of the old table and add it to the result table.
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/** // exit after adding
68 & nbs

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.