Change of RowState property of DataRow

Source: Internet
Author: User

The RowState attribute (state) of the DataRow has 5 values: Detached, Unchanged, Added, Deleted, Modified.

When we use the DataRow newRow = Datatable.newrow () method to produce a new DataRow, its state is detached. Then we will use this NewRow DataTable.Rows.Add (NewRow); Method is added to the DataTable. At this point the state of NewRow is added, this time to execute the Datatable.acceptchanges () method, commit the changes. At this point the state of NewRow is unchanged, this time, of course, can execute newrow.delete () operation or Datatable.remove (NewRow) or edit this line ah, However, note that the corresponding state after execution is deleted, Detached, Modified, respectively. If the Datatable.acceptchanges () method is executed again, the commit change status is Detached, Detached, unchanged, respectively. The difference between newrow.delete () and Datatable.remove (NewRow) is that newrow.delete () does not remove NewRow directly from the DataTable, but only changes its state to deleted, So you need to call Datatable.acceptchanges () to commit the changes to remove, the concept of removal is to change its state to detached, and it does not really kill NewRow, and Datatable.remove (NewRow) Method changes the NewRow state directly to detached without having to call Datatable.acceptchanges () to commit the change. However, if Newrow.delete () is executed when the NewRow status is added, it is removed and the state is changed to detached. There are commit changes naturally also have a corresponding rollback operation Datatable.rejectchanges (), it is worth noting that the rollback operation will be a state of added, Deleted, Modified, All become the last unchanged. For the state is already detached, the Datatable.rejectchanges () method is powerless.

The following is a more detailed explanation:

1. RowState Introduction

RowState is a very important property of the DataRow, indicating the current state of the DataRow. RowState has Added, Modified, unchanged, Deleted, Detached, respectively, indicating that the DataRow was added, modified, unchanged, deleted, detached from the table. These states can be converted to each other after some methods have been called or some operations have been performed.

DataAdapter can determine how to affect storage locations such as databases based on RowState. If the DataRow has a status of Added, DataAdapter will add the DataRow to the storage location such as the database, and for Modified, Deleted will perform the update and delete operations. In fact, the final operation effect is decided by DataAdapter SelectCommand, UpdateCommand and so on DbCommand. If you write a DELETE statement in UpdateCommand or perform a stored procedure with a delete operation, a DataRow with a status of Modified will eventually be deleted in the database instead of being updated. (Note: This is true of the DataAdapter operation described below.)

2. RowState status after loading a DataRow from a different location

A. The RowState of a DataRow added from an XML file or using the DataTable.Rows.Add (params object[]) method is Added.
With the DataRow added above, if the DataAdapter update is used, an insert operation, for example: added to the SQL Server database, is performed.

B. If the Dataadapter.acceptchangesduringfill property is true, the RowState of the DataRow populated with the DataAdapter.Fill method is unchanged, otherwise, Added, The default AcceptChangesDuringFill is true.
For example: By default, a DataRow that is read from a SQL Server database and then updated directly with DataAdapter will not have any data modified because DataAdapter does not perform a DataRow that has a status of unchanged Any action.

3. Modification, change, datarow.rowstate conversion after deletion

A. For a DataRow with a status of unchanged or Modified, the state after the data has been modified is Modified.
This means that the DataRow needs to update its own data to a storage location such as a database through DataAdapter. Because the data in the DataRow may already be different from its previous version.

B. For a DataRow with a status of Added, the data is still Added after modification.
DataAdapter a DataRow with a status of Added will perform the add operation. It is clear that even if the modified DataRow should remain in the Added state, it cannot be added to the storage location such as the database when the DataAdapter is updated.

C. If the Dataadapter.acceptchangesduringupdate property is true, the state of the DataRow that is updated with dataadapter.update is unchanged, otherwise, the status of the DataRow remains Unchanged, the default acceptchangesduringupdate is true.
By default, the status is unchanged indicates that the current data for the DataRow has not undergone a change, and you can think that it is consistent with the data in the database, but not all.

D. For a DataRow with a status of unchanged, the state is Deleted after the Delete method is called.
DataAdapter deletes the corresponding data for the storage location, such as the database, by performing a delete operation on a DataRow with a status of Deleted.

E. For a DataRow with a status of Added, the state is Detached after the Delete method is called.
A DataRow of the Added state may not exist in a storage location such as a database, so the state is converted to Detached instead of Deleted,dataadapter the DataRow with a status of Detached is not processed.

F. After removing a DataRow using the DataTable.Rows.Remove method, the DataRow status is Detached.

4. Using AcceptChanges, RejectChanges, setadded, setmodified method after datarow.rowstate conversion

A. The DataRow with a status of unchanged, Added, Modified, using the DataRow.AcceptChanges method, the row state will be converted to unchanged.
The DataRow of the above three States is intended to add or modify data, so the DataRow is present in the DataTable and the state is unchanged after accepting the change. If you use the DataAdapter update at this point, you will not have any effect on storage locations such as databases, because the status is already unchanged, which should have been converted after the DataAdapter update.

B. A DataRow with a status of Deleted, using the DataRow.AcceptChanges method, the row state is converted to Detached.
The purpose is to delete the DataRow of the data and accept the change from the DataTable, so the state becomes Detached.

C. A DataRow with a status of Detached, you cannot use the DataRow.AcceptChanges method.

D. A DataRow with a status of unchanged, Modified, Deleted, using the Datarow.rejectchanges method, the row state will be converted to unchanged.
The DataRow of the above three states, whose purpose is equivalent to deleting or modifying data, so the DataRow exists in the DataTable and the state is unchanged after rejecting the change. If you use the DataAdapter update at this point, the situation will be similar to a.

E. DataRow with a status of Added, Detached, using the Datarow.rejectchanges method, the row state will be converted to Detached.
A DataRow with a status of Added is intended to be added, so it is rejected and detached from the DataTable with a state of Detached.

F. For a DataRow with a status of unchanged, you can use Datarow.setadded, datarow.setmodified method to convert the row state to Added or Modified.
Setadded, the SetModified method will throw an exception for a DataRow that is not unchanged to the state.

5. Using ImportRow, Datarow.rowstate conversion after Copy method

A. After importing a DataRow using the Datatable.importrow method, the imported DataRow and the original DataRow have the same row state.
The ImportRow method imports the DataRow in a replicated manner, a DataRow with a status of Detached, and cannot be imported into a DataTable, but does not produce an exception.

B. Using the Datatable.copy or Dataset.copy method, the status of the DataRow remains unchanged.

6. Accessing data from a DataRow in a different RowState

A. For a DataRow with a status of Added, unchanged, Modified, you can conveniently access the data through the datarow[< column name;]

B. A DataRow with a status of Deleted needs to be accessed using the datarow[< column name, DataRowVersion.Original] .
For a DataRow that has already called the Delete method, you need to specify the Original version of the Access data.

C. A DataRow with a status of Detched, there seems to be no way to access the data in it.
The DataRow has been removed from the DataTable, which may make the data inaccessible.

7. Get the DataRow for different RowState in the DataTable

A. A copy of the DataRow of different RowState in the DataTable can be obtained through datatable.getchanges (DataRowState) .
The GetChanges method returns a new DataTable that contains a copy of the DataRow for the specified row state, and modifying these copies does not affect the DataRow in the original DataTable. If you use the GetChanges method without parameters, a DataTable that contains a DataRow copy of the row state of Added, Modified, Deleted is returned. You can use a bitwise OR operator | Combine to get a DataRow of multiple states. In addition, a DataRow with a status of Detached appears to be unable to be obtained through the GetChanges method.

RowState property changes for DataRow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.