RowState.Added 和 DataRowVersion.Original 有什么区别

What is the difference between RowState.Added and DataRowVersion.Original

我有一个包含 3 个条件的函数:

  1. 如果DataRow rowstate == Added
  2. 如果!DataRow HasVersion(DataRowVersion.Original)
  3. 如果DataRow rowstate == modified

我的问题是第二个,谁能解释一下? 它与第一个条件有何不同?

说明

  1. DataRow 在新添加到 Table 调用 AcceptChanges() 后,RowState 将设置为 “不变
  2. DataRow 具有 DataRowVersion "Original",当它包含它时 原始值。在 DataRow 上调用 AcceptChanges() 或 DataTable 时,DataRowVersion 将设置为 "Original"。您可以说 original 意味着所有更改都已被接受。
  3. DataRow 在编辑后具有 RowState "Modified"。

示例程序

我创建了一个小示例程序,它显示了动作的变化,以阐明差异。

class Program {
    static void Main(string[] args) {
        var table = new DataTable("MyTable");
        table.Columns.Add(new DataColumn("MyColumn"));
        var row = table.NewRow();
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Detached

        table.Rows.Add(row);
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Added

        table.AcceptChanges();
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Unchanged

        row.BeginEdit();
        row[0] = "NewValue";
        row.EndEdit();
        Console.WriteLine($"Rowstate: {row.RowState}"); //Prints Modified

        if (row.HasVersion(DataRowVersion.Current)) { // Does the row contain uncommited values?
            Console.WriteLine($"DataRowVersion: {DataRowVersion.Current}"); //Prints Current
        }

        table.AcceptChanges(); //Commit all DataRowChanges
        if (row.HasVersion(DataRowVersion.Original)) {
            Console.WriteLine($"DataRowVersion: {DataRowVersion.Original}"); //Prints Current
        }

        Console.ReadLine();
    }
}

进一步阅读

有关 DataRowStates is actually pretty well explained. It provides a short explanation about every single state aswell as some example code. Thats the same for DataRowVersions 的 msdn 文档。您一定要看看这两篇文章。

DataRowVersion

引自链接的 MSDN 文章:

After calling the DataRow object's BeginEdit method, if you change the value, the Current and Proposed values become available.

After calling the DataRow object's CancelEdit method, the Proposed value is deleted.

After calling the DataRow object's EndEdit method, the Proposed value becomes the Current value.

After calling the DataRow object's AcceptChanges method, the Original value becomes identical to the Current value.

After calling the DataTable object's AcceptChanges method, the Original value becomes identical to the Current value.

After calling the DataRow object's RejectChanges method, the Proposed value is discarded, and the version becomes Current.

行状态(已添加、已删除..) 行版本(原始、当前、建议)

更新方式使用Row Version来决定 对数据库应用哪些更改。 如果我们在 TableAdapter 上调用 AcceptChanges 方法 在调用更新方法之前: 当前版本将是原始版本 所以你不应该在调用 Update 之前调用 ACCEPTCHANGES 方法 方法