C# .net比较两个DataTable,查找修改、添加和删除的行
我有一个DataTable,它保存用户从UI修改的行。在用户保存更改后,我想检查行是否被添加、删除或修改,并返回它们C# .net比较两个DataTable,查找修改、添加和删除的行,c#,.net,datatable,compare,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; //r
//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
这是行不通的。你知道怎么了吗
//////////////////////////////////////////////
使用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();
}
}
}
}
您可以保留合并前和合并后的dtlast计数,然后可以进行比较,但是合并会添加相同的行,请检查我的代码。您可以保留合并前和合并后的dtlast计数,然后可以进行比较,但是合并会添加相同的行,请检查我的代码。
DataTable dtChanges = null;
dtCurrent.AcceptChanges();
dtCurrent.Merge(dtLast, true);
dtChanges = dtCurrent.GetChanges(DataRowState.Unchanged);
return dtChanges;