批量更新数据- -(还有五一系列的原代)

由于最近作的项目跟数据库有关系- -特此把分析的资料分享下- -顺便广告下我现在是电脑维修工

None.gif // --思路通过一个SqlDataAdapter先检索数据到DataTable中,修改DataTable中的数据在更新,主要通过设置
None.gif
EmpAdapter.UpdateBatchSize = 100 ; // --该属性默认为,代表次处理条记录,是处理所有记录,不要设得太大
None.gif
CommandUpdater.UpdatedRowSource  =  UpdateRowSource.None;
None.gif
// -----------提高性能
None.gif

None.gif
None.gif
None.gif
None.gif
using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
public  partial  class  Default3 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//数据容器
InBlock.gif
        DataTable BtMm=new DataTable();
InBlock.gif
InBlock.gif        SqlDataAdapter EmpAdapter 
= new SqlDataAdapter();
InBlock.gif        
//-----------选择数据
InBlock.gif
        SqlConnection DbConSelect = new SqlConnection();
InBlock.gif
InBlock.gif        
//-------------复制数据到这个连接中
InBlock.gif
        SqlConnection DbConUpdater = new SqlConnection();
InBlock.gif
InBlock.gif        SqlCommand CommandSelect 
= new SqlCommand();
InBlock.gif        SqlCommand CommandUpdater 
= new SqlCommand();
InBlock.gif
InBlock.gif        
//DoNET
InBlock.gif
        DbConSelect.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
InBlock.gif        
//DoNET
InBlock.gif
        DbConUpdater.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
InBlock.gif
InBlock.gif        
//---------选择更新的数据
InBlock.gif
        CommandSelect.CommandText = "SELECT* FROM b";
InBlock.gif        CommandSelect.CommandType 
= CommandType.Text;
InBlock.gif        CommandSelect.Connection 
= DbConSelect;
InBlock.gif
InBlock.gif        
//-------------------------------------设置更新
InBlock.gif
        CommandUpdater.CommandText = "update b set ID=@ID";
InBlock.gif        CommandUpdater.Connection 
= DbConUpdater;
InBlock.gif
InBlock.gif        
//--添加参数
InBlock.gif
        SqlParameter a = new SqlParameter("@ID", SqlDbType.VarChar, 50"ID");//注意最后一个Address代表从数据源检索的列
InBlock.gif
        CommandUpdater.Parameters.Add(a);
InBlock.gifEmpAdapter.SelectCommand
=CommandSelect;
InBlock.gifEmpAdapter.UpdateCommand
=CommandUpdater;
InBlock.gifEmpAdapter.Fill(BtMm);
//---调用CommandSelect检索数据
InBlock.gif
DbConSelect.Close();
InBlock.gif
InBlock.gif
foreach (DataRow dr in BtMm.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    dr[
"ID"= "- -ooo"+System.DateTime.Now.ToString();
ExpandedSubBlockEnd.gif}

InBlock.gif
//更新触发
InBlock.gif
EmpAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
InBlock.gifLabel1.Text 
= "";
InBlock.gif        EmpAdapter.UpdateBatchSize
=100;//--该属性默认为,代表次处理条记录,是处理所有记录,不要设得太大
InBlock.gif
        CommandUpdater.UpdatedRowSource = UpdateRowSource.None;
InBlock.gif
//--------------------------------------------------------
InBlock.gif
//Both 将输出参数和第一个返回行都映射到DataSet 中的已更改的行。 
InBlock.gif 
//FirstReturnedRecord 将第一个返回行中的数据映射到DataSet 中的已更改的行。 
InBlock.gif 
//None 忽略任何返回的参数或行。 
InBlock.gif 
//OutputParameters 将输出参数映射到DataSet 中的已更改的行。 
InBlock.gif

InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbConUpdater.Open();
InBlock.gif            EmpAdapter.Update(Blog);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif    
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif    
private void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
InBlock.gif        Label1.Text
+=args.RowCount.ToString()+"<br>";
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// ------------web.confing设置
None.gif
< connectionStrings >
None.gif        
< add name = " ConnectionString "  connectionString = " Data Source=.;Initial Catalog=g;User ID=sa;Password=sa " />
None.gif        
< add name = " ConnectionStringInfo "  connectionString = " Data Source=.;Initial Catalog=g;User ID=sa;Password=sa " />
None.gif    
</ connectionStrings >
 

转载于:https://www.cnblogs.com/ajaxren/archive/2007/05/03/734989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值