批量更新操作
在上一个版本的ADONET当中SqlDataAdapterde的Update方法将会为 DataSet当中的每一行调用一次更新操作
在ADONET中您可以设置UpdateBatchSize属性在单步中执行多个更新
这样可以提高数据更新的效率
UpdataBatchSize的默认值为使得默认的更新行为与以前版本的ADONET一致
代码经验
public Form()
{
conn = new SqlConnection(ConfigurationManagerConnectionStrings[AWConnectionString]ConnectionString)
dAdapt = new SqlDataAdapter(SELECT ProductID Name ListPrice FROM ProductionProduct conn)
InitializeComponent()
}
SqlConnection conn;
SqlDataAdapter dAdapt;
DataSet dSet = new DataSet()
StringBuilder logString = new StringBuilder()
private void batchUpdateForm_Load(SystemObject sender SystemEventArgs e)
{
dAdaptRowUpdating += new SystemDataSqlClientSqlRowUpdatingEventHandler(OnRowUpdating)
dAdaptRowUpdated += new SystemDataSqlClientSqlRowUpdatedEventHandler(OnRowUpdated)
}
private void getDataButton_Click(SystemObject sender SystemEventArgs e)
{
dAdaptFill(dSet Product)
productGridDataSource = dSetTables[Product];
}
private void updateDataButton_Click(SystemObject sender SystemEventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(dAdapt)
logStringRemove( logStringLength)
// Enable batching by setting batch size !=
dAdaptUpdateBatchSize = intParse(batchSizeTextBoxText)
// Execute the update
dAdaptUpdate(dSetTables[Product])
MessageBoxShow(logStringToString())
}
//handler for the RowUpdating event
public void OnRowUpdating(object sender SqlRowUpdatingEventArgs e)
{
logStringAppendLine(Starting row update)
}
// handler for RowUpdated event
public void OnRowUpdated(object sender SqlRowUpdatedEventArgs e)
{
logStringAppendLine(Completed row update)
}