Data Access Application Block Gets Batch Update Functionality in Enterprise Library 3.0

Data Access Application Block Gets Batch Update Functionality in Enterprise Library 3.0

by David Hayden ( Microsoft MVP C# ), Filed: Enterprise Library 3.0 Tutorials

 

I remember writing a tutorial showing one how to implement batch updates using the DAAB in Enterprise Library 2.0 because it didn't directly support it:

That was some “ugly“ code, because we had to essentially go around the DAAB and use the DataAdapter directly.

In Enterprise Library 3.0, the DAAB now supports batch updates by providing an overload to the Database.UpdateDataSet method that allows you to specify a batch update size as a Nullable<int>. Here is one of the overloads that I will show today:

 

Database.UpdateDataSet(DataSet dataSet, string tableName,
DbCommand insertCommand, DbCommand updateCommand,
DbCommand deleteCommand, UpdateBehavior updateBehavior,
int? updateBatchSize);

 

If we take a very simple Customers Table as follows:

 

Batch Updates in Enterprise Library 3.0

 

We can write code that updates the following table using Batch Updates and Enterprise Library 3.0 Data Access Application Block as follows:

 

// Get default database
Database database = DatabaseFactory.CreateDatabase();

// Read all the customers in a dataset
string selectSql = "SELECT * FROM Customers";
DataSet customerDataSet = database.
ExecuteDataSet(CommandType.Text, selectSql);
// Modify Records foreach (DataRow dr in customerDataSet.Tables[0].Rows) dr["Name"] += "2"; // Create UpdateCommand string updateSql = "UPDATE Customers SET Name = @Name
WHERE CustomerId = @CustomerId
"; DbCommand updateCommand = database.
GetSqlStringCommand(updateSql); database.AddInParameter(updateCommand,
"CustomerId",
DbType.Int32,
"CustomerId", DataRowVersion.Current); database.AddInParameter(updateCommand, "Name",
DbType.String,
"Name", DataRowVersion.Current); // Execute Batch Update // Use zero to specify database // maximum batch update size database.UpdateDataSet(customerDataSet, "Table", null,
updateCommand,
null, UpdateBehavior.Transactional, 0);

 

Conclusion

Batch update support in the Enterprise Library 3.0 DAAB is a useful addition.

 

Source: David Hayden ( Microsoft MVP C# )

Filed: Enterprise Library 3.0 Tutorials

posted on Sunday, December 31, 2006 7:52 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices