1. You need to obtain multiple records from the page to the datatable --> Delete existing records in the database from the datatable -- Insert existing records into the database.
2. Implementation idea: Get all records on the page, use the foreach loop datatable to delete existing records in the database, and finally update the database. (It is better to judge whether the data exists in the database before putting it into the datatable. Here, we only use the datarow method)
3. Problem: Can the row. Delete () loop be implemented in foreach sometimes, but sometimes the following error cannot be reported:
The set has been modified; the enumeration operation may not be executed.
After debugging, the problem is found: loop row in foreach. during the delete () operation, the row status must be unchanged. The status of the newly added row is added, and then the exception is thrown.
Therefore, you must use the acceptchanges () method of the datatable to set the status of the newly added row to unchanged to confirm the changes in the datatable. Then, when row. elete () is deleted cyclically, the row status is set to deleted. Rows that do not need to be deleted must be set back to the original added state through the setadded () method. In this way, when updating the data source in dataadapter, you can update the new records to the database.
Alas, it's really troublesome.
// The table has been added dynamically. The following changes the status of the added row to unchanged so that it can be deleted during the loop.
DS. Table name. acceptchanges ();
Foreach (DS table name. Table Name row In DS. Table name)
{
If (Br. getsome ( String . Format ( " And Title = '{0 }' " , Row. Title). Table name. Count > 0 )
{
Row. Delete ();
}
Else
{
Set it back to the original added status. Otherwise, the database will not be updated during database update.
Row. setadded ();
}
}
4. Sort the information as follows:
(1) For (INT I = 0...) {Delete} when the first row is deleted cyclically, the original second row becomes the first row. Pay attention to this problem.
(2) differences between Delete () and remove (ROW:
"When using dataset or datatable with dataadapter and relational data sources, use the delete method of datarow to remove rows. The delete method only marks the row as deleted in dataset or able without removing it. When the dataadapter encounters a row marked as deleted, it executes its deletecommand method to delete the row from the data source. Then, you can use the acceptchanges method to permanently remove the row. If you use remove to delete a row, the row is completely removed from the table, but the dataadapter does not delete the row from the data source. ", This is the original statement on msdn. (You can see that the data source is modified when the dataadapter is executed, and the modification in the able still needs to be done by acceptchanges .) Add:
* Remove is removed from the datatable, but not from the data source. Even if the data source is updated using dataadapter.
* After A Delete row is called, it cannot be accessed again. When dataadapter. Update is called, it will be deleted from the data source. It still exists in the able before there is acceptchanges.
(3) acceptchanges
"When acceptchanges is called, The endedit method is called implicitly to terminate any edits. If the rowstate of a row is "added" or "modified", the rowstate is changed to "unchanged "."I don't quite understand what the purpose of this method is. After modifying the able, I have to confirm it before I can actually modify the datatable.
(4) setadd: Change the rowstate of datarow to added. If the current row is not in the unchanged or added state, invalidoperationexception is thrown and a message is displayed, indicating that setadded can be called only for the datarow instance whose rowstate is unchanged.