技术开发 频道

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

    【IT168 技术文档】今天看《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");
0
相关文章