.net比较两个DataTable,查找修改,添加和删除的行

时间:2016-01-27 09:42:24

标签: c# .net datatable compare

我有DataTable,用于保存用户从UI修改的行。用户保存更改后,我想检查是否添加,删除或修改了行并返回它们。

//dtLast - DataTable loaded on open
//dtCurrent - DataTable loaded on save

DataTable dtChanges = null;

dtLast.Merge(dtCurrent, true);
dtChanges = dtLast.GetChanges();

return dtChanges; //return rows that was added, deleted, modified

这不起作用。知道什么是错的吗?

////////////////////////////////////////////// < / p>

使用MSDN代码我写了这个:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        DemonstrateMergeTable();
    }

    private static void DemonstrateMergeTable()
    {
        // Create a new DataTable.
        DataTable table1 = new DataTable("Items");

        // Add two columns to the table:
        DataColumn column = new DataColumn("id", typeof(System.Int32));
        column.AutoIncrement = true;
        table1.Columns.Add(column);

        column = new DataColumn("item", typeof(System.String));
        table1.Columns.Add(column);

        // Set primary key column.
        table1.PrimaryKey = new DataColumn[] { table1.Columns[0] };

        // Add some rows.
        DataRow row;
        for (int i = 0; i <= 3; i++)
        {
            row = table1.NewRow();
            row["item"] = "Item " + i;

            table1.Rows.Add(row);
        }

        // Accept changes.
        table1.AcceptChanges();
        PrintValues(table1, "Original values");
        //
        //
        //


        // Using the same schema as the original table, 
        // modify the data for later merge.
        DataTable modifiedTable = table1.Copy();

        modifiedTable.Rows[0]["item"] = "NEW ITEM 0";
        modifiedTable.Rows[2].Delete();
        modifiedTable.Rows[3].Delete();

        DataRow DRrow = modifiedTable.NewRow();
        DRrow["id"] = 2;
        DRrow["item"] = "NEW ITEM 2";
        modifiedTable.Rows.Add(DRrow);

        modifiedTable.AcceptChanges();

        PrintValues(modifiedTable, "modified table");
        ////////////////////////////////////
        DataTable table1copy1 = table1.Copy();
        DataTable table1copy2 = table1.Copy();
        //DataTable table1copy3 = table1.Copy();

        DataTable modifiedTablecopy1 = modifiedTable.Copy();
        DataTable modifiedTablecopy2 = modifiedTable.Copy();

        ////////////////////////////////
        ////////////////////////////////

        table1copy1.Merge(modifiedTable, true);
        PrintValues(table1copy1, "table1copy1 after merge true");
        //
        DataTable dtChanges1 = null;
        dtChanges1 = table1copy1.GetChanges(DataRowState.Modified);
        PrintValues(dtChanges1, "dtChanges1");
        //
        //
        table1copy2.Merge(modifiedTable, false);
        PrintValues(table1copy2, "table1copy2 after merge false");
        //
        //DataTable dtChanges2 = null;
        //dtChanges2 = table1copy2.GetChanges();
        //PrintValues(dtChanges2, "dtChanges2");
        ////
        //
        modifiedTablecopy1.Merge(table1, true);
        PrintValues(modifiedTablecopy1, "modifiedTablecopy1 after merge true");
        //
        DataTable dtChanges3 = null;
        dtChanges3 = modifiedTablecopy1.GetChanges(DataRowState.Modified);
        PrintValues(dtChanges3, "dtChanges3");
        //
        //
        modifiedTablecopy2.Merge(table1, false);
        PrintValues(modifiedTablecopy2, "modifiedTablecopy2 after merge false");
        //
        //DataTable dtChanges4 = null;
        //dtChanges4 = modifiedTablecopy2.GetChanges();
        //PrintValues(dtChanges4, "dtChanges4");
        ////
        //


    }


    private static void PrintValues(DataTable table, string label)
    {
        // Display the values in the supplied DataTable:
        Console.WriteLine(label);
        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn column in table.Columns)
            {
                Console.Write("\t{0}  ", row[column, DataRowVersion.Original]);

                Console.Write("\t{0}  ", row[column, DataRowVersion.Current]);
            }
            Console.WriteLine();
        }
    }

}
}

但正如你所看到的那样即使我不修改行[1&#34;第1项和第34期],它也会出现在&#34; GetChanges()&#34;。

现在我一直坚持

        DataTable dtChanges = null;
        dtCurrent.AcceptChanges();

        dtCurrent.Merge(dtLast, true);
        dtChanges = dtCurrent.GetChanges(DataRowState.Unchanged);

        return dtChanges;

0 个答案:

没有答案