惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
The Register - Security
The Register - Security
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
P
Proofpoint News Feed
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
The Last Watchdog
The Last Watchdog
F
Fortinet All Blogs
S
Schneier on Security
Help Net Security
Help Net Security
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
I
InfoQ
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Stack Overflow Blog
Stack Overflow Blog
T
Troy Hunt's Blog
人人都是产品经理
人人都是产品经理
T
Threatpost
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
Forbes - Security
Forbes - Security
Vercel News
Vercel News
S
Security Affairs
美团技术团队
P
Privacy & Cybersecurity Law Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Jina AI
Jina AI
Spread Privacy
Spread Privacy
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
U
Unit 42
Recorded Future
Recorded Future
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
P
Palo Alto Networks Blog
H
Hacker News: Front Page
S
Security @ Cisco Blogs
博客园 - 【当耐特】

博客园 - 大天使泰瑞尔

通过反射机制控制前台的数据项的显示和隐藏 由partition看窗口函数 IE 6下CSS浮动样式的问题 转载一篇拼SQL字符串的语句 ASP.NET 2.0 中控件的简单异步回调 新手基础知识专用 今天终于学会了从客户端调用Web Service 数据结构学习(6):队列 数据结构学习(5):链表 数据结构学习(4):栈 数据结构学习(3):堆化优先队列 数据结构学习(2):汉诺塔问题 数据结构学习(1):搜索二叉树 冒泡算法的三种JavaScript表示 YUI学习(1):ToolTip的用法 XML学习失误系列(2):分清节点性质,使用nodeValue JavaScript&DHTML特效学习(1):MSN提示框 YUI的Drap&Drop对IE7不支持 Gmail邮箱可以直接注册了
学会了ASP.NET 2.0中的数据批量更新 - 大天使泰瑞尔 - 博客园
大天使泰瑞尔 · 2007-02-22 · via 博客园 - 大天使泰瑞尔

    今天看《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

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DemoBulkUpdate : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void btnUpdateAddress_Click(object sender, EventArgs e)
    
{
        SqlDataAdapter EmpAdapter 
= new SqlDataAdapter();
        DataTable EmpDT 
= new DataTable();
        SqlConnection DBConSelect 
= new SqlConnection();
        SqlConnection DBConUpdate 
= new SqlConnection();
        SqlCommand SelectCommand 
= new SqlCommand();
        SqlCommand UpdateCommand 
= new SqlCommand();
        
//采用两个不同的数据源供查询和更新使用
        DBConSelect.ConnectionString = ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
        DBConUpdate.ConnectionString 
= ConfigurationManager.ConnectionStrings["DSN_Northwind"].ConnectionString;
        
//获取所有的雇员表中的记录
        SelectCommand.CommandText = "SELECT TOP 500 * FROM EMPLOYEES";
        SelectCommand.CommandType 
= CommandType.Text;
        SelectCommand.Connection 
= DBConSelect;

        UpdateCommand.CommandText 
= "UPDATE EMPLOYEES SET Address=@Address,City=@City,Region=@Region,Country=@Country";
        UpdateCommand.CommandType 
= CommandType.Text;
        UpdateCommand.Connection 
= DBConUpdate;

        SqlParameter AddressParam 
= new SqlParameter("@Address", SqlDbType.VarChar, 15"Address");
        SqlParameter CityParam 
= new SqlParameter("@City", SqlDbType.VarChar, 15"City");
        SqlParameter RegionParam 
= new SqlParameter("@Region", SqlDbType.VarChar, 15"Region");
        SqlParameter CountryParam 
= new SqlParameter("@Country", SqlDbType.VarChar, 15"Region");

        UpdateCommand.Parameters.Add(AddressParam);
        UpdateCommand.Parameters.Add(CityParam);
        UpdateCommand.Parameters.Add(RegionParam);
        UpdateCommand.Parameters.Add(CountryParam);
        
//设置适配器,查询命令用于检索所有的雇员表中的记录,更新命令用于修改地址信息然后
        
//更新回数据库
        EmpAdapter.SelectCommand = SelectCommand;
        EmpAdapter.UpdateCommand 
= UpdateCommand;

        EmpAdapter.Fill(EmpDT);

        DBConSelect.Close();
        
//遍历所有的雇员表记录然后更新地址信息
        foreach (DataRow DR in EmpDT.Rows)
        
{
            DR[
"Address"= "4445 W 77th Street, Suite 140";
            DR[
"City"= "Edina";
            DR[
"Region"= "Minnesota";
            DR[
"Country"= "USA";
        }


        EmpAdapter.RowUpdated
+=new SqlRowUpdatedEventHandler(OnRowUpdated);
        
this.lblCounter.Text = "";
        EmpAdapter.UpdateBatchSize 
= 100;

        UpdateCommand.UpdatedRowSource 
= UpdateRowSource.None;

        
try
        
{
            DBConUpdate.Open();
            EmpAdapter.Update(EmpDT);
        }

        
catch (Exception ex)
        
{
            
this.lblCounter.Text += ex.Message + "<br />";
        }

        
finally
        
{
            
if (DBConUpdate.State == ConnectionState.Open)
            
{
                DBConUpdate.Close();
            }

        }

    }

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

    }

}

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