web analytics

Row States and Row Versions

Options

codeling 1602 - 6666
@2017-02-26 21:44:03

ADO.NET manages rows in tables using row states and versions. A row state indicates the status of a row; row versions maintain the values stored in a row as it is modified, including current, original, and default values. For example, after you have made a modification to a column in a row, the row will have a row state of Modified, and two row versions: Current, which contains the current row values, and Original, which contains the row values before the column was modified.

When AcceptChanges is called on a DataSet, DataTable, or DataRow, all rows with a row state of Deleted are removed. The remaining rows are given a row state of Unchanged, and the values in the Original row version are overwritten with the Current row version values. When RejectChanges is called, all rows with a row state of Added are removed. The remaining rows are given a row state of Unchanged, and the values in the Current row version are overwritten with the Original row version values.

@2017-02-26 21:46:02

Each DataRow object has a RowState property that you can examine to determine the current state of the row. The following table gives a brief description of each RowState enumeration value.

RowState value

Description

Unchanged

No changes have been made since the last call to AcceptChanges or since the row was created by DataAdapter.Fill.

Added

The row has been added to the table, but AcceptChanges has not been called.

Modified

Some element of the row has been changed.

Deleted

The row has been deleted from a table, and AcceptChanges has not been called.

Detached

The row is not part of any DataRowCollection. The RowState of a newly created row is set to Detached. After the new DataRow is added to the DataRowCollection by calling the Add method, the value of the RowState property is set to Added.

Detached is also set for a row that has been removed from a DataRowCollection using the Remove method, or by the Delete method followed by the AcceptChanges method.

@2017-02-26 21:57:52

The following table gives a brief description of each DataRowVersion enumeration value.

 

DataRowVersion value

Description

Current

The current values for the row. This row version does not exist for rows with a RowState of Deleted.

Default

The default row version for a particular row. The default row version for an Added, Modified, or Unchanged row is Current. The default row version for a Deleted row is Original. The default row version for a Detached row is Proposed.

Original

The original values for the row. This row version does not exist for rows with a RowState of Added.

Proposed

The proposed values for the row. This row version exists during an edit operation on a row, or for a row that is not part of a DataRowCollection.

@2017-02-26 22:00:07

The following code example displays the values in all the deleted rows of a table. Deleted rows do not have a Current row version, so you must pass DataRowVersion.Original when accessing the column values.

DataTable catTable = catDS.Tables["Categories"];

DataRow[] delRows = catTable.Select(null, null, DataViewRowState.Deleted);

Console.WriteLine("Deleted rows:\n");

foreach (DataColumn catCol in catTable.Columns)
  Console.Write(catCol.ColumnName + "\t");
Console.WriteLine();

foreach (DataRow delRow in delRows)
{
  foreach (DataColumn catCol in catTable.Columns)
    Console.Write(delRow[catCol, DataRowVersion.Original] + "\t");
  Console.WriteLine();
}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com