如何使用asp.net2.0的UpdateBatchSize高效上传文件

使用UpdateBatchSize效率真的很高,我做了一个小demo,希望大家在项目中灵活的使用它,代码如下提供DEMO下载

 

Code
protected void Button1_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();

            SqlConnection conn 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["demoConnectionString"].ToString());
            SqlCommand selectComm 
= new SqlCommand("select * from product", conn);
            SqlCommand updateComm 
= new SqlCommand("update product set remark=@remark", conn);
            updateComm.Parameters.Add(
"@remark", SqlDbType.VarChar, 200"remark");
            updateComm.UpdatedRowSource 
= UpdateRowSource.None;

            SqlDataAdapter sda 
= new SqlDataAdapter();
            sda.SelectCommand 
= selectComm;
            sda.UpdateCommand 
= updateComm;
            DataSet ds 
= new DataSet();
            sda.Fill(ds);
            
//for (int i = 1; i < 10; i++)
            
//{
            
//    UpdateData(sda, ds, i);
            
//}
            UpdateData(sda, ds, 300);
        }
        
public void UpdateData(SqlDataAdapter sda, DataSet ds, int recordCount)
        {
            
foreach (DataRow row in ds.Tables[0].Rows)
            {
                row[
"remark"= "当前时间:" + DateTime.Now.ToString();
            }
            
try
            {
                sda.UpdateBatchSize 
= recordCount;
                
long oldTicks = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
                sda.Update(ds);
                
long ticksSpan = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - oldTicks) / 1000;
                ListBox1.Items.Add(
"UpdateBatchSize=" + recordCount.ToString() + "时,用时:" + ticksSpan.ToString() + "");
            }
            
catch (Exception ex)
            {
                ListBox1.Items.Add(ex.Message);
            }
            
finally
            {

            }
        }

 

 源码下载:点击这里

 

 

 

posted @ 2009-10-14 15:11  Jason.Bird  阅读(399)  评论(0编辑  收藏  举报