欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【DataAdapter执行批量更新的实例代码】,下面是详细的讲解!
DataAdapter执行批量更新的实例代码
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();
// Using different connection objects for select and updates from the
// Northwind database.
DBConSelect.ConnectionString=
ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
DBConUpdate.ConnectionString=
ConfigurationManager.ConnectionStrings["DSN_NorthWind"].ConnectionString;
// Reading all records from the Employees table
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;
AddressParam=new SqlParameter("@Address",
SqlDbType.VarChar, 15, "Address");
SqlParameter CityParam;
CityParam=new SqlParameter("@City", SqlDbType.VarChar, 15, "City");
SqlParameter RegionParam;
RegionParam=new SqlParameter("@Region", SqlDbType.VarChar, 15, "Region");
SqlParameter CountryParam;
CountryParam=new SqlParameter("@Country",
SqlDbType.VarChar, 15, "Country");
UpdateCommand.Parameters.Add(AddressParam);
UpdateCommand.Parameters.Add(CityParam);
UpdateCommand.Parameters.Add(RegionParam);
UpdateCommand.Parameters.Add(CountryParam);
// Setting up Data Adapter with the Select and Update Commands
// The Select command will be used to retrieve all employee
// information from the Northwind database and the Update command
// will be used to save changes back to the database
EmpAdapter.SelectCommand=SelectCommand;
EmpAdapter.UpdateCommand=UpdateCommand;
EmpAdapter.Fill(EmpDT);
DBConSelect.Close();
// Looping through all employee records and assigning them the new
// address
foreach (DataRow DR in EmpDT.Rows)
{
DR["Address"]="4445 W 77th Street, Suite 140";
DR["City"]="Edina";
DR["Region"]="Minnesota";
DR["Country"]="USA";
}
// Adding an event handler to listen to the RowUpdated event.
// This event will will fire after each batch is executed
EmpAdapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
lblCounter.Text="";
EmpAdapter.UpdateBatchSize=100;
// It is important to set this property for batch processing of
// updated records since batch updates are incapable of
// updating the source with changes from the database
UpdateCommand.UpdatedRowSource=UpdateRowSource.None;
try
{
DBConUpdate.Open();
EmpAdapter.Update(EmpDT);
}
catch (Exception ex)
{
lblCounter.Text +=ex.Message + "<Br>";
}
finally
{
if (DBConUpdate.State==ConnectionState.Open)
{
DBConUpdate.Close();
}
}
}
private void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
lblCounter.Text +="Batch is processed till row number=" +
args.RowCount.ToString() + "<br>";
}
关于DataAdapter执行批量更新的实例代码的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 【asp】asp.net开发中常见公共捕获异常方式总结(
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
