使用 DataAdapter 执行批量更新
2006-04-29 22:06 Clingingboy 阅读(3520) 评论(1) 编辑 收藏 举报在以前版本的 ADO.NET 中,使用
在 ADO.NET 2.0 中,
执行非常大的批次可能会降低性能。因此,在实现应用程序之前,应测试最佳的批次大小设置。
使用 UpdateBatchSize 属性
启用了批量更新后,DataAdapter 的 UpdateCommand、InsertCommand 和 DeleteCommand 的
下面的过程演示如何使用
{
SqlDataAdapter EmpAdapter = new SqlDataAdapter();
DataTable EmpDT = new DataTable();
SqlConnection DBConSelect = new SqlConnection();
SqlConnection DBConUpdate = new SqlConnection();
SqlCommand SelectCommand = new SqlCommand();
SqlCommand UpdateCommand = new SqlCommand();
// Using different connection objects for select and updates from the
// Northwind database.
DBConSelect.ConnectionString =
ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
DBConUpdate.ConnectionString =
ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
// Reading all records from the Employees table
SelectCommand.CommandText = "SELECT top 500 * FROM EMPLOYEES";
SelectCommand.CommandType = CommandType.Text;
SelectCommand.Connection = DBConSelect;
UpdateCommand.CommandText = " UPDATE EMPLOYEES SET Address=@Address, " +
"City=@City, Region=@Region, Country=@Country";
UpdateCommand.CommandType = CommandType.Text;
UpdateCommand.Connection = DBConUpdate;
SqlParameter AddressParam;
AddressParam = new SqlParameter("@Address",
SqlDbType.VarChar, 15, "Address");
SqlParameter CityParam;
CityParam = new SqlParameter("@City", SqlDbType.VarChar, 15, "City");
SqlParameter RegionParam;
RegionParam = new SqlParameter("@Region", SqlDbType.VarChar, 15, "Region");
SqlParameter CountryParam;
CountryParam = new SqlParameter("@Country",
SqlDbType.VarChar, 15, "Country");
UpdateCommand.Parameters.Add(AddressParam);
UpdateCommand.Parameters.Add(CityParam);
UpdateCommand.Parameters.Add(RegionParam);
UpdateCommand.Parameters.Add(CountryParam);
// Setting up Data Adapter with the Select and Update Commands
// The Select command will be used to retrieve all employee
// information from the Northwind database and the Update command
// will be used to save changes back to the database
EmpAdapter.SelectCommand = SelectCommand;
EmpAdapter.UpdateCommand = UpdateCommand;
EmpAdapter.Fill(EmpDT);
DBConSelect.Close();
// Looping through all employee records and assigning them the new
// address
foreach (DataRow DR in EmpDT.Rows)
{
DR["Address"] = "4445 W 77th Street, Suite 140";
DR["City"] = "Edina";
DR["Region"] = "Minnesota";
DR["Country"] = "USA";
}
// Adding an event handler to listen to the RowUpdated event.
// This event will will fire after each batch is executed
EmpAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
lblCounter.Text = "";
EmpAdapter.UpdateBatchSize = 100;
// It is important to set this property for batch processing of
// updated records since batch updates are incapable of
// updating the source with changes from the database
UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
try
{
DBConUpdate.Open();
EmpAdapter.Update(EmpDT);
}
catch (Exception ex)
{
lblCounter.Text += ex.Message + "<Br>";
}
finally
{
if (DBConUpdate.State == ConnectionState.Open)
{
DBConUpdate.Close();
}
}
}
private void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
lblCounter.Text += "Batch is processed till row number = " +
args.RowCount.ToString() + "<br>";
}