Condividi tramite


Handling Batch Update-Related Events and Errors 

The DataAdapter has two update-related events: RowUpdating and RowUpdated. In previous versions of ADO.NET, when batch processing is disabled, each of these events is generated once for each row processed. RowUpdating is generated before the update occurs, and RowUpdated is generated after the database update has been completed.

Event Behavior Changes with Batch Updates

When batch processing is enabled, multiple rows are updated in a single database operation. Therefore, only one RowUpdated event occurs for each batch, whereas the RowUpdating event occurs for each row processed. When batch processing is disabled, the two events are fired with one-to-one interleaving, where one RowUpdating event and one RowUpdated event fire for a row, and then one RowUpdating and one RowUpdated event fire for the next row, until all of the rows are processed.

Accessing Updated Rows

When batch processing is disabled, the row being updated can be accessed using the Row property of the RowUpdatedEventArgs class.

When batch processing is enabled, a single RowUpdated event is generated for multiple rows. Therefore, the value of the Row property for each row is null. RowUpdating events are still generated for each row. The CopyToRows method of the RowUpdatedEventArgs class allows you to access the processed rows by copying references to the rows into an array. If no rows are being processed, CopyToRows throws an ArgumentNullException. Use the RowCount property to return the number of rows processed before calling the CopyToRows method.

Handling Data Errors

Batch execution has the same effect as the execution of each individual statement. Statements are executed in the order that the statements were added to the batch. Errors are handled the same way in batch mode as they are when batch mode is disabled. Each row is processed separately. Only rows that have been successfully processed in the database will be updated in the corresponding DataRow within the DataTable.

The data provider and the back-end database server determine which SQL constructs are supported for batch execution. An exception may be thrown if a non-supported statement is submitted for execution.

See Also

Concepts

Performing Batch Updates with a DataAdapter
Working with DataAdapter Events

Other Resources

Performing Batch Operations Using DataAdapters