![]() When batch processing is enabled, a single RowUpdated event is generated for multiple 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 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. Therefore, only one RowUpdated event occurs for each batch, whereas the RowUpdating event occurs for each row processed. When batch processing is enabled, multiple rows are updated in a single database operation. Event Behavior Changes with Batch Updates RowUpdating is generated before the update occurs, and RowUpdated is generated after the database update has been completed. In previous versions of ADO.NET, when batch processing is disabled, each of these events is generated once for each row processed. The DataAdapter has two update-related events: RowUpdating and RowUpdated. Handling Batch Update-Related Events and Errors "DELETE FROM Production.ProductCategory "Ī = UpdateRowSource.None "INSERT INTO Production.ProductCategory (Name) VALUES 50, "Name") Ī = UpdateRowSource.None Set the UPDATE command and parameters.Ī = UpdateRowSource.None SqlDataAdapter adapter = new SqlDataAdapter() Connect to the AdventureWorks database. String connectionString = GetConnectionString() Assumes GetConnectionString() returns a valid connection string. Public static void BatchUpdate(DataTable dataTable,Int32 batchSize) "DELETE FROM Production.ProductCategory " _Ī = UpdateRowSource.None "INSERT INTO Production.ProductCategory (Name) VALUES _Ī = _Īdapter.DeleteCommand = New SqlCommand( _ "UPDATE Production.ProductCategory SET " _Ī = _Īdapter.InsertCommand = New SqlCommand( _ Using connection As New SqlConnection(connectionString)Īdapter.UpdateCommand = New SqlCommand( _ ' Connect to the AdventureWorks database. ' Assumes GetConnectionString() returns a valid connection string.ĭim connectionString As String = GetConnectionString() Public Sub BatchUpdate( _īyVal dataTable As DataTable, ByVal batchSize As Int32) It sets the UpdateBatchSize property and executes the update. The code assumes that the DataSet object has modified rows. ![]() The code creates a new SqlDataAdapter object, setting its UpdateCommand, InsertCommand, and DeleteCommand properties. The procedure takes two arguments, a DataSet object that has columns representing the ProductCategoryID and Name fields in the Production.ProductCategory table, and an integer representing the batch size (the number of rows in the batch). The following procedure demonstrates the use of the UpdateBatchSize property. When performing a batch update, the command's UpdatedRowSource property value of FirstReturnedRecord or Both is invalid. When batch updates are enabled, the UpdatedRowSource property value of the DataAdapter's UpdateCommand, InsertCommand, and DeleteCommand should be set to None or OutputParameters. Therefore, you should test for the optimum batch size setting before implementing your application. Setting it to 1 disables batch updates, as rows are sent one at a time.Įxecuting an extremely large batch could decrease performance. Setting the UpdateBatchSize to 0 will cause the DataAdapter to use the largest batch size that the server can handle. For example, setting the UpdateBatchSize to 10 will group 10 separate statements and submit them as single batch. Setting the UpdateBatchSize to a positive integer value causes updates to the database to be sent as batches of the specified size. ![]() Starting with ADO.NET 2.0, the DbDataAdapter exposes an UpdateBatchSize property. Every row update involved a network round-trip to the database. If the row had been modified, it called the appropriate UpdateCommand, InsertCommand, or DeleteCommand, depending on the value of the RowState property for that row. ![]() As it iterated through the rows in the specified DataTable, it examined each DataRow to see if it had been modified. When updating a database with changes from a DataSet in previous versions of ADO.NET, the Update method of a DataAdapter performed updates to the database one row at a time. NET data providers for SQL Server ( ) and Oracle ( ). The reduction in the number of round trips to the server typically results in significant performance gains. Batch support in ADO.NET allows a DataAdapter to group INSERT, UPDATE, and DELETE operations from a DataSet or DataTable to the server, instead of sending one operation at a time. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |