c# - 对 DataSet.Merge 和 RowState 感到困惑

标签 c# .net dataset

考虑以下过于简单的 DataTable对象:

enter image description here

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();
}

该代码产生以下结果:

enter image description here

看最后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/

相关文章:

c# - 我如何标记我的某些代码行,因为它不会在发布时运行?

c# - 用于商业目的的 iText/iTextSharp : not recommended?

c# - 序列化任何自定义 WCF SecurityToken 的好方法?

c# - .NET 日期时间到字符串

javascript - 未捕获的类型错误 : Cannot read property 'cat' of undefined (this. dataset.cat)

opencv - 用于情感识别的分类数据集

c# - 将现有数据集添加到 Report .rdlc

c# - 将 iostream 输入代码从 C++ 移植到 C#

c# - 指定的转换无效 C# Entity Framework

c# - 当我处理多种数据类型时,如何处理多个 foreach 循环