在以前版本的 ADO
在 ADO
執行非常大的批次可能會降低性能
使用 UpdateBatchSize 屬性
啟用了批量更新後
下面的過程演示如何使用 UpdateBatchSize 屬性
protected void btnUpdateAddress_Click(object sender
{
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
ConfigurationManager
DBConUpdate
ConfigurationManager
// Reading all records from the Employees table
SelectCommand
SelectCommand
SelectCommand
"City=@City
UpdateCommand
UpdateCommand
SqlParameter AddressParam;
AddressParam = new SqlParameter("@Address"
SqlDbType
SqlParameter CityParam;
CityParam = new SqlParameter("@City"
SqlParameter RegionParam;
RegionParam = new SqlParameter("@Region"
SqlParameter CountryParam;
CountryParam = new SqlParameter("@Country"
SqlDbType
UpdateCommand
UpdateCommand
UpdateCommand
UpdateCommand
// 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
EmpAdapter
EmpAdapter
DBConSelect
// Looping through all employee records and assigning them the new
// address
foreach (DataRow DR in EmpDT
{
DR["Address"] = "
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
lblCounter
EmpAdapter
// 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
try
{
DBConUpdate
EmpAdapter
}
catch (Exception ex)
{
lblCounter
}
finally
{
if (DBConUpdate
{
DBConUpdate
}
}
}
private void OnRowUpdated(object sender
{
lblCounter
args
}
From:http://tw.wingwit.com/Article/program/net/201311/14475.html