对 DataSet.Merge 和 RowState 感到困惑
Confused on DataSet.Merge and RowState
考虑以下过于简化的 DataTable
对象:
Id
是一个自动递增整数,作用于 table 的主键,而 Text
是一个简单的 String
类型列。
现在考虑以下代码:
private static void Main(string[] args)
{
var dataSet = new TestingDataSet();
var row = dataSet.TestingTable.AddTestingTableRow("First");
dataSet.AcceptChanges();
var copiedDataSet = dataSet.Copy();
Console.WriteLine("Row after initialization:");
Console.WriteLine();
Console.WriteLine("Text : " + row.Text);
Console.WriteLine("State: " + row.RowState.ToString());
Console.WriteLine();
row.Text = "Modifications";
Console.WriteLine("Row after changes:");
Console.WriteLine();
Console.WriteLine("Text : " + row.Text);
Console.WriteLine("State: " + row.RowState.ToString());
Console.WriteLine();
dataSet.Merge(copiedDataSet, false);
Console.WriteLine("Row after merge:");
Console.WriteLine();
Console.WriteLine("Text : " + row.Text);
Console.WriteLine("State: " + row.RowState.ToString());
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
该代码产生以下结果:
查看最终的 RowState
值。它被修改了。怎么会?复制的数据集实例没有变化,我明确地将 preserveChanges
参数设置为 false
。因此,现在不应考虑该行 Unchanged
?
This MSDN article 声明合并行时,它们的状态也会随之而来。这篇文章是错误的,还是我的代码中有什么?
所以我深入研究了文档并发现了这个:
If the incoming row has a RowState of Unchanged and the existing row
has a RowState of Modified, Deleted, or Added, the RowState of the
existing row is set to Modified.
所以这似乎是将未更改的行合并到已修改的行时的预期行为。
可以找到完整的文章 here。
我 运行 遇到了同样的问题,最终通过从主 DataTable 对象中提取所有更改来绕过它,运行 核心对象上的 AcceptChanges,然后对个人进行处理表并将它们合并回主表。这样,核心 DataTable 对象中的所有内容都是 "Unmodified",并且只会吸收合并数据的 RowState,而不会像这样进行任何意外调整。
考虑以下过于简化的 DataTable
对象:
Id
是一个自动递增整数,作用于 table 的主键,而 Text
是一个简单的 String
类型列。
现在考虑以下代码:
private static void Main(string[] args)
{
var dataSet = new TestingDataSet();
var row = dataSet.TestingTable.AddTestingTableRow("First");
dataSet.AcceptChanges();
var copiedDataSet = dataSet.Copy();
Console.WriteLine("Row after initialization:");
Console.WriteLine();
Console.WriteLine("Text : " + row.Text);
Console.WriteLine("State: " + row.RowState.ToString());
Console.WriteLine();
row.Text = "Modifications";
Console.WriteLine("Row after changes:");
Console.WriteLine();
Console.WriteLine("Text : " + row.Text);
Console.WriteLine("State: " + row.RowState.ToString());
Console.WriteLine();
dataSet.Merge(copiedDataSet, false);
Console.WriteLine("Row after merge:");
Console.WriteLine();
Console.WriteLine("Text : " + row.Text);
Console.WriteLine("State: " + row.RowState.ToString());
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
该代码产生以下结果:
查看最终的 RowState
值。它被修改了。怎么会?复制的数据集实例没有变化,我明确地将 preserveChanges
参数设置为 false
。因此,现在不应考虑该行 Unchanged
?
This MSDN article 声明合并行时,它们的状态也会随之而来。这篇文章是错误的,还是我的代码中有什么?
所以我深入研究了文档并发现了这个:
If the incoming row has a RowState of Unchanged and the existing row has a RowState of Modified, Deleted, or Added, the RowState of the existing row is set to Modified.
所以这似乎是将未更改的行合并到已修改的行时的预期行为。
可以找到完整的文章 here。
我 运行 遇到了同样的问题,最终通过从主 DataTable 对象中提取所有更改来绕过它,运行 核心对象上的 AcceptChanges,然后对个人进行处理表并将它们合并回主表。这样,核心 DataTable 对象中的所有内容都是 "Unmodified",并且只会吸收合并数据的 RowState,而不会像这样进行任何意外调整。