没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
转帖|其它|编辑:郝浩|2011-07-26 14:47:29.000|阅读 572 次
概述:在以前版本的 ADO.NET 中,使用 DataSet 中的更改来更新数据库时,DataAdapter 的 Update 方法每次更新数据库的一行。因为该方法循环访问指定 DataTable 中的行,所以,会检查每个 DataRow,确定是否已修改。如果该行已修改,将根据该行的 RowState 属性值调用相应的 UpdateCommand、InsertCommand 或 DeleteCommand。每一次行更新都涉及网络与数据库之间的双向数据传输。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
在以前版本的 ADO.NET 中,使用 DataSet 中的更改来更新数据库时,DataAdapter 的 Update 方法每次更新数据库的一行。因为该方法循环访问指定 DataTable 中的行,所以,会检查每个 DataRow,确定是否已修改。如果该行已修改,将根据该行的 RowState 属性值调用相应的 UpdateCommand、InsertCommand 或 DeleteCommand。每一次行更新都涉及网络与数据库之间的双向数据传输。
在 ADO.NET 2.0 中,DataAdapter 公开了 UpdateBatchSize 属性。将 UpdateBatchSize 设置为正整数值将使对数据库的更新以指定大小的批次进行发送。例如,如果将 UpdateBatchSize 设置为 10,会将 10 个独立的语句组合在一起并作为一批提交。将 UpdateBatchSize 设置为 0 将导致 DataAdapter 使用服务器可以处理的最大批次的大小。如果将其设置为 1,则禁用批量更新,因为此时每次发送一行。
执行非常大的批次可能会降低性能。因此,在实现应用程序之前,应测试最佳的批次大小设置。
使用 UpdateBatchSize 属性
启用了批量更新后,DataAdapter 的 UpdateCommand、InsertCommand 和 DeleteCommand 的 UpdatedRowSource 属性值应设置为 None 或 OutputParameters。在执行批量更新时,命令的 FirstReturnedRecord 或 Both 的 UpdatedRowSource 属性值无效。
下面的过程演示如何使用 UpdateBatchSize 属性。该过程采用两个参数,一个 DataSet 对象,其中包含代表 PRoduction.ProductCategory 表中的 ProductCategoryID 和 Name 字段的列,一个代表批次大小的整数(批次中的行数)。该代码创建一个新的 SqlDataAdapter 对象,设置其 UpdateCommand、InsertCommand 和 DeleteCommand 属性。该代码假定 DataSet 对象已修改了行。它设置 UpdateBatchSize 属性并执行更新。
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>";
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:网络转载面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号