日期:2014-05-18 浏览次数:20951 次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace EmployeeDetails
{
public partial class BatchUpdate : Form
{
//初始化成员变量
public StringBuilder sb = new StringBuilder();
public BatchUpdate()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter();
//更新事件
da.RowUpdated += new SqlRowUpdatedEventHandler(rowUpdated);
string connectionString = "Data source=.;Initial Catalog=Hr;User id=sa;Password=123456";
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = (SqlCommand)conn.CreateCommand();//创建命令对象
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Employee";
da.SelectCommand = cmd;
DataSet ds = new DataSet();
//********创建SqlCommandBuilder对象
SqlCommandBuilder bldr = new SqlCommandBuilder(da);
//将记录添充到记录集
da.Fill(ds, "employee");
//==========================================================
foreach (DataRow dr in ds.Tables["Employee"].Rows)
{
dr["gender"] = 0;
}
//执行批量更新
da.UpdateBatchSize = 1;
da.Update(ds, "Employee");
//获得受更新影响的行.
da.RowUpdated -= new SqlRowUpdatedEventHandler(rowUpdated);
MessageBox.Show(sb.ToString());
}
//------------------------
private void rowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
sb.Append("行:" + e.RecordsAffected.ToString() + "\r\n");
}
}
}