学会了ASP.NET 2.0中的数据批量更新

本文介绍了一个ASP.NET2.0应用程序中实现数据批量更新的方法,通过一个具体的示例展示了如何使用ADO.NET2.0进行数据库批量操作。示例包括前后端代码,演示了从数据库读取数据、更新数据并最终批量提交更改的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    今天看《ASP.NET 2.0高级编程》,学会了ADO.NET 2.0中的数据批量更新,把代码发到这里,以供日后之需。
    DemoBulkUpdate.aspx
   
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoBulkUpdate.aspx.cs" Inherits="DemoBulkUpdate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Button ID="btnUpdateAddress" runat="server" Text="批量更新地址" OnClick="btnUpdateAddress_Click" />
        
<br /><br />
        
<asp:Label ID="lblCounter" runat="server"></asp:Label>&nbsp;<br />
    
</div>
    
</form>
</body>
</html>
   DemoBulkUpdate.aspx.cs
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Data.SqlClient;
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
None.gif
public partial class DemoBulkUpdate : 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
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void btnUpdateAddress_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlDataAdapter EmpAdapter 
= new SqlDataAdapter();
InBlock.gif        DataTable EmpDT 
= new DataTable();
InBlock.gif        SqlConnection DBConSelect 
= new SqlConnection();
InBlock.gif        SqlConnection DBConUpdate 
= new SqlConnection();
InBlock.gif        SqlCommand SelectCommand 
= new SqlCommand();
InBlock.gif        SqlCommand UpdateCommand 
= new SqlCommand();
InBlock.gif        
//采用两个不同的数据源供查询和更新使用
InBlock.gif
        DBConSelect.ConnectionString = ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
InBlock.gif        DBConUpdate.ConnectionString 
= ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
InBlock.gif        
//获取所有的雇员表中的记录
InBlock.gif
        SelectCommand.CommandText = "SELECT TOP 500 * FROM EMPLOYEES";
InBlock.gif        SelectCommand.CommandType 
= CommandType.Text;
InBlock.gif        SelectCommand.Connection 
= DBConSelect;
InBlock.gif
InBlock.gif        UpdateCommand.CommandText 
= "UPDATE EMPLOYEES SET Address=@Address,City=@City,Region=@Region,Country=@Country";
InBlock.gif        UpdateCommand.CommandType 
= CommandType.Text;
InBlock.gif        UpdateCommand.Connection 
= DBConUpdate;
InBlock.gif
InBlock.gif        SqlParameter AddressParam 
= new SqlParameter("@Address", SqlDbType.VarChar, 15"Address");
InBlock.gif        SqlParameter CityParam 
= new SqlParameter("@City", SqlDbType.VarChar, 15"City");
InBlock.gif        SqlParameter RegionParam 
= new SqlParameter("@Region", SqlDbType.VarChar, 15"Region");
InBlock.gif        SqlParameter CountryParam 
= new SqlParameter("@Country", SqlDbType.VarChar, 15"Region");
InBlock.gif
InBlock.gif        UpdateCommand.Parameters.Add(AddressParam);
InBlock.gif        UpdateCommand.Parameters.Add(CityParam);
InBlock.gif        UpdateCommand.Parameters.Add(RegionParam);
InBlock.gif        UpdateCommand.Parameters.Add(CountryParam);
InBlock.gif        
//设置适配器,查询命令用于检索所有的雇员表中的记录,更新命令用于修改地址信息然后
InBlock.gif        
//更新回数据库
InBlock.gif
        EmpAdapter.SelectCommand = SelectCommand;
InBlock.gif        EmpAdapter.UpdateCommand 
= UpdateCommand;
InBlock.gif
InBlock.gif        EmpAdapter.Fill(EmpDT);
InBlock.gif
InBlock.gif        DBConSelect.Close();
InBlock.gif        
//遍历所有的雇员表记录然后更新地址信息
InBlock.gif
        foreach (DataRow DR in EmpDT.Rows)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DR[
"Address"= "4445 W 77th Street, Suite 140";
InBlock.gif            DR[
"City"= "Edina";
InBlock.gif            DR[
"Region"= "Minnesota";
InBlock.gif            DR[
"Country"= "USA";
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        EmpAdapter.RowUpdated
+=new SqlRowUpdatedEventHandler(OnRowUpdated);
InBlock.gif        
this.lblCounter.Text = "";
InBlock.gif        EmpAdapter.UpdateBatchSize 
= 100;
InBlock.gif
InBlock.gif        UpdateCommand.UpdatedRowSource 
= UpdateRowSource.None;
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DBConUpdate.Open();
InBlock.gif            EmpAdapter.Update(EmpDT);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.lblCounter.Text += ex.Message + "<br />";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (DBConUpdate.State == ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                DBConUpdate.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
private void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.lblCounter.Text += "Batch is processed till row number= " + args.RowCount.ToString() + "<br />";
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

    没想到这是2007年第一篇文章,希望给我带来好运气!

转载于:https://www.cnblogs.com/tyrael007/archive/2007/02/22/653585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值