考虑以下过于简单的 DataTable
对象:
Id
是一个自动递增的整数,作用于表的主键,而 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
值(value)。它被修改了。怎么会?复制的数据集实例没有变化,我明确设置了 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 .
关于c# - 对 DataSet.Merge 和 RowState 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30060958/