请考虑以下情况:
使用.NET的DataSet.Merge(DataSet)
方法,我们可以将 B 应用于 A ,创建一个新的DataSet C ,即{{ 1}}。
C = A.Merge(B)
合并方法的问题在于,对于 B 中标记为已删除的 r 行, r 将在 C 中标记为已修改。显然,C中的 r 也应标记为已删除。
一个可行但但我觉得没有吸引力的解决方案是手动枚举 B 中的所有已删除行,并使用其标识符在 C 中找到相同的行,此后将 C 中该行的RowState更改为Deleted。
上述问题的正确解决方案是什么?
答案 0 :(得分:2)
我一直在玩数据集并想出下面的 democode (基于MSDN)来测试你问的一些事情。 (.net framework 4.0 VS2010)
我发现合并无效,因此在查看您的问题时,我们有A.Merge(B),A与B合并。
正常合并(没有preserveChanges标志)使B成为领先,所以当在B中删除某些内容并且它出现在A中时它也会在新的A中被删除。如果我正确地阅读了你的问题,这就是你想要的行为。 (testoutput A)
如果你将preservechanges标志设置为True而不是奇怪的事情开始发生,那么删除的标志例如将在新的A中被修改。(testoutput B)
如果将preservechnages标志设置为true,请将其设置为false。据我所知,默认行为是你想要的,而你不是你所看到的。我怀疑别的东西是错的,希望这可以帮助你朝着正确的方向发展
private static void DemonstrateMergeTable()
{
// Create a DataSet with one table, two columns, and data.
DataSet originalDataSet = new DataSet("dataSet");
DataSet mergeDataSet = new DataSet("mergeSet");
DataTable originalTable = CreateItemsTable(originalDataSet);
DataTable mergeTable = CreateTableToMergeWith(originalTable, mergeDataSet);
CreateTestSets(originalTable, mergeTable);
PrintValues(originalDataSet, "Original values");
PrintValues(mergeDataSet, "Table to merge with");
// Merge the table into the DataSet
Console.WriteLine("Merging");
//originalDataSet.Merge(mergeTable,false,MissingSchemaAction.Add);
//originalDataSet.Merge(mergeTable,true, MissingSchemaAction.Add);
originalDataSet.Merge(mergeTable);
PrintValues(originalDataSet, "Merged With table.");
}
private static void CreateTestSets(DataTable originalTable, DataTable mergeTable)
{
// a new row to the mergetable, no matching row in the original
mergeTable.Rows.Add(new Object[] { 30, 0 });
//deletedRow in original, added row in mergetable
originalTable.Rows[0].Delete(); //id = 0
mergeTable.Rows.Add(new Object[] { 0, 100 });
//deletedRow in mergeset, corresponding row in original
mergeTable.Rows[0].Delete(); //id = 5
//deletedRow in original, corrosponding row in merging
originalTable.Rows[6].Delete();
}
private static DataTable CreateItemsTable(DataSet dataSet)
{
DataTable table = new DataTable("Items");
// Add columns
DataColumn c1 = new DataColumn("id",
Type.GetType("System.Int32"), "");
DataColumn c2 = new DataColumn("Item",
Type.GetType("System.Int32"), "");
table.Columns.Add(c1);
table.Columns.Add(c2);
// DataColumn array to set primary key.
DataColumn[] keyCol = new DataColumn[1];
// Set primary key column.
keyCol[0] = c1;
table.PrimaryKey = keyCol;
// Add rows.
for (int i = 0; i < 10; i++)
{
DataRow row = table.NewRow();
row["id"] = i;
row["Item"] = i;
table.Rows.Add(row);
}
// Add table to the DataSet
dataSet.Tables.Add(table);
// Accept changes.
dataSet.AcceptChanges();
return table;
}
private static DataTable CreateTableToMergeWith(DataTable table, DataSet mergeSet)
{
DataTable t2 = table.Clone();
// Add rows.
for (int i = 5; i < 15; i++)
{
DataRow row = t2.NewRow();
row["id"] = i;
row["Item"] = i;
t2.Rows.Add(row);
}
mergeSet.Tables.Add(t2);
t2.AcceptChanges();
return t2;
}
private static void PrintValues(DataSet dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach (DataTable table in dataSet.Tables)
{
PrintTable(table);
}
}
private static void PrintTable(DataTable table)
{
Console.WriteLine("TableName: " + table.TableName);
foreach (DataRow row in table.Rows)
{
DataRowState dataRowState = row.RowState;
foreach (DataColumn column in table.Columns)
{
string columnName = column.ColumnName;
Object o = dataRowState != DataRowState.Deleted ? row[column] : row[column, DataRowVersion.Original];
Console.Write("\t {0} \t:{1}",columnName, o);
}
Console.Write("\t ({0})", dataRowState);
Console.WriteLine();
}
}
TestOutput A(preservechanges = false)
Original values
TableName: Items
id :0 Item :0 (Deleted)
id :1 Item :1 (Unchanged)
id :2 Item :2 (Unchanged)
id :3 Item :3 (Unchanged)
id :4 Item :4 (Unchanged)
id :5 Item :5 (Unchanged)
id :6 Item :6 (Deleted)
id :7 Item :7 (Unchanged)
id :8 Item :8 (Unchanged)
id :9 Item :9 (Unchanged)
Table to merge with
TableName: Items
id :5 Item :5 (Deleted)
id :6 Item :6 (Unchanged)
id :7 Item :7 (Unchanged)
id :8 Item :8 (Unchanged)
id :9 Item :9 (Unchanged)
id :10 Item :10 (Unchanged)
id :11 Item :11 (Unchanged)
id :12 Item :12 (Unchanged)
id :13 Item :13 (Unchanged)
id :14 Item :14 (Unchanged)
id :30 Item :0 (Added)
id :0 Item :100 (Added)
Merging
Merged With table.
TableName: Items
id :0 Item :100 (Modified)
id :1 Item :1 (Unchanged)
id :2 Item :2 (Unchanged)
id :3 Item :3 (Unchanged)
id :4 Item :4 (Unchanged)
id :5 Item :5 (Deleted)
id :6 Item :6 (Modified)
id :7 Item :7 (Unchanged)
id :8 Item :8 (Unchanged)
id :9 Item :9 (Unchanged)
id :10 Item :10 (Unchanged)
id :11 Item :11 (Unchanged)
id :12 Item :12 (Unchanged)
id :13 Item :13 (Unchanged)
id :14 Item :14 (Unchanged)
id :30 Item :0 (Added)
测试输出B(保留更改为true)
Original values
TableName: Items
id :0 Item :0 (Deleted)
id :1 Item :1 (Unchanged)
id :2 Item :2 (Unchanged)
id :3 Item :3 (Unchanged)
id :4 Item :4 (Unchanged)
id :5 Item :5 (Unchanged)
id :6 Item :6 (Deleted)
id :7 Item :7 (Unchanged)
id :8 Item :8 (Unchanged)
id :9 Item :9 (Unchanged)
Table to merge with
TableName: Items
id :5 Item :5 (Deleted)
id :6 Item :6 (Unchanged)
id :7 Item :7 (Unchanged)
id :8 Item :8 (Unchanged)
id :9 Item :9 (Unchanged)
id :10 Item :10 (Unchanged)
id :11 Item :11 (Unchanged)
id :12 Item :12 (Unchanged)
id :13 Item :13 (Unchanged)
id :14 Item :14 (Unchanged)
id :30 Item :0 (Added)
id :0 Item :100 (Added)
Merging
Merged With table.
TableName: Items
id :0 Item :0 (Deleted)
id :1 Item :1 (Unchanged)
id :2 Item :2 (Unchanged)
id :3 Item :3 (Unchanged)
id :4 Item :4 (Unchanged)
id :5 Item :5 (Modified)
id :6 Item :6 (Deleted)
id :7 Item :7 (Modified)
id :8 Item :8 (Modified)
id :9 Item :9 (Modified)
id :10 Item :10 (Unchanged)
id :11 Item :11 (Unchanged)
id :12 Item :12 (Unchanged)
id :13 Item :13 (Unchanged)
id :14 Item :14 (Unchanged)
id :30 Item :0 (Added)