首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

SqlDataAdapter的批量更新有关问题

2012-01-23 
SqlDataAdapter的批量更新问题.C# codeusing Systemusing System.Collections.Genericusing System.Comp

SqlDataAdapter的批量更新问题.

C# code
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");        }    }}


[解决办法]
没有你的数据库,但要告诉你,SqlCommandBuilder它来更新数据时,会自动生成其它的语句(Update,Insert,Delete),但要注意你的表里面一定要有主键

热点排行