用Ado.net可以对数据进行批量添加或更新

 你可以在客户端改添加许多条记录,然后在服务器用一句Update来一次更新  
  首先取得数据,放到DataGrid里  
   
  System.Data.SqlClient.SqlConnection   conn   =   new   System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=110");  
  conn.Open();  
  System.Data.SqlClient.SqlDataAdapter   da   =   new   System.Data.SqlClient.SqlDataAdapter("select   *   from   student",conn);  
  dt   =   new   System.Data.DataSet();  
  da.Fill(dt,"student");  
   
  然后绑定数据集和DataGrid  
  DataGrid.SetDataBinding(dt,"student");  
  如果需要,可以绑定TextBox来作录入,而用DataGrid显示  
  this.textBox16.DataBindings.Add("Text",dt,"student.stuno");  
  然后进行数据的操作如:  
  增加:  
  this.BindingContext[dt,"student"].AddNew();  
  删除:  
  this.BindingContext[dt,"student"].RemoveAt(this.BindingContext[dt,"student"].Position);  
  最后把结果写回数据库:  
   
   
  //   sqlInsertCommand1  
  //    
  this.sqlInsertCommand1.CommandText   =   "INSERT   INTO   student(stuno,   name)   VALUES   (@stuno,   @name)";    
  this.sqlInsertCommand1.Connection   =   this.conn;  
  this.sqlInsertCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter("@stuno",   System.Data.SqlDbType.VarChar,   4,   "stuno"));  
  this.sqlInsertCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter("@name",   System.Data.SqlDbType.VarChar,   50,   "name"));  
  //    
  //   sqlUpdateCommand1  
  //    
  this.sqlUpdateCommand1.CommandText   =   "UPDATE   student   SET   stuno   =   @stuno,   name   =   @name   WHERE   (stuno   =   @Original_stuno)";  
  this.sqlUpdateCommand1.Connection   =   this.conn;  
  this.sqlUpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter("@stuno",   System.Data.SqlDbType.VarChar,   4,   "stuno"));  
  this.sqlUpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter("@name",   System.Data.SqlDbType.VarChar,   50,   "name"));  
  this.sqlUpdateCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter("@Original_stuno",   System.Data.SqlDbType.VarChar,   4,   System.Data.ParameterDirection.Input,   false,   ((System.Byte)(0)),   ((System.Byte)(0)),   "stuno",   System.Data.DataRowVersion.Original,   null));  
     
  //   sqlDeleteCommand1  
  //    
  this.sqlDeleteCommand1.CommandText   =   "DELETE   FROM   student   WHERE   (stuno   =   @Original_stuno)";  
  this.sqlDeleteCommand1.Connection   =   this.conn;  
  this.sqlDeleteCommand1.Parameters.Add(new   System.Data.SqlClient.SqlParameter("@Original_stuno",   System.Data.SqlDbType.VarChar,   4,   System.Data.ParameterDirection.Input,   false,   ((System.Byte)(0)),   ((System.Byte)(0)),   "stuno",   System.Data.DataRowVersion.Original,   null));  
   
  this.sqlDa.DeleteCommand   =   this.sqlDeleteCommand1;  
  this.sqlDa.InsertCommand   =   this.sqlInsertCommand1;  
  this.sqlDa.UpdateCommand   =   this.sqlUpdateCommand1;  
  try  
  {  
  sqlDa.Update(dt.GetChanges,"student");    
  return   true;  
  }  
  catch(System.Data.SqlClient.SqlException   ex)  
  {  
   
  return   false;  
  }    
  finally  
  {  
  conn.Close();  
  }

给你一个更新例子:数据库表只有一个字段iD,表名是test,其它的类推:  
  conn   =   new   System.Data.SqlClient.SqlConnection("server=localhost;database=hos;uid=sa;pwd=");  
  conn.Open();  
  da   =   new   System.Data.SqlClient.SqlDataAdapter("select   *   from   test",conn);  
  dt1   =   new   System.Data.DataSet();  
  da.Fill(dt1,"test");  
  DataRow   myRow;  
  for(int   i=0;i<10;i++)  
  {  
  myRow   =   dt1.Tables[0].NewRow();  
  //   Then   add   the   new   row   to   the   collection.  
  myRow["ID"]   =   i.ToString();  
  dt1.Tables[0].Rows.Add(myRow);  
  }  
   
  System.Data.SqlClient.SqlDataAdapter   daa   =   new   System.Data.SqlClient.SqlDataAdapter("Select   ID   From   test",   conn);  
  System.Data.SqlClient.SqlCommandBuilder   MyCB   =   new   System.Data.SqlClient.SqlCommandBuilder(daa);  
  daa.MissingSchemaAction   =   MissingSchemaAction.AddWithKey;  
  daa.Update(dt1,   "test");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值