熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ADO.NET批量更新操作

2022-06-13   來源: .NET編程 
    批量更新操作
    在上一個版本的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
    }
From:http://tw.wingwit.com/Article/program/net/201311/13275.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.