SqlDataAdapter 批量更新 DataTable
/// <summary> /// 统计接口调用次数 /// </summary> public void UpdateInterfaceClickCount() { DataRow[] arrRows = updateTable.Select(string.Format("name='{0}'", InterfaceName)); if (arrRows.Length > 0) { DataRow row = arrRows[0]; row.BeginEdit(); row["click_count"] = (int)row["click_count"] + 1; row.EndEdit(); } else { DataRow dr = updateTable.NewRow(); dr["name"] = InterfaceName; dr["click_count"] = 1; dr["version"] = appversion; updateTable.Rows.Add(dr); } if (updateTable.Rows.Count < 2) return; new InterfaceCallDal().BatchUpdate(updateTable, updateTable.Rows.Count); updateTable.Clear(); }
/// <summary> /// 批量更新 /// </summary> /// <param name="dataTable"></param> /// <param name="batchSize">获取或设置一个值,该值启用或禁用批处理支持,并指定可以批处理执行的命令的数目。</param> public void BatchUpdate(DataTable dataTable, Int32 batchSize) { try { dataTable.AcceptChanges(); foreach (DataRow row in dataTable.Rows) { row.SetModified(); } // Assumes GetConnectionString() returns a valid connection string. string connectionString = ConfigHelper.SQLConnection; // Connect to the AdventureWorks database. using (var connection = new SqlConnection(connectionString)) { // Create a SqlDataAdapter. var adapter = new SqlDataAdapter { UpdateCommand = new SqlCommand("update test_table SET click_count=click_count+@click_count WHERE name=@name and version=@version and app_type='app' ", connection) }; // Set the UPDATE command and parameters. adapter.UpdateCommand.Parameters.Add("@click_count", SqlDbType.Int, 4, "click_count"); adapter.UpdateCommand.Parameters.Add("@name", SqlDbType.VarChar, 128, "name"); adapter.UpdateCommand.Parameters.Add("@version", SqlDbType.VarChar, 10, "version"); //adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None; // Set the batch size. //adapter.UpdateBatchSize = 0; // Execute the update. adapter.Update(dataTable); } } catch { //ingore } }
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】AI 的力量,开发者的翅膀:欢迎使用 AI 原生开发工具 TRAE
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
· InnoDB为什么不用跳表,Redis为什么不用B+树?
· 记一次 C# 平台调用中因非托管 union 类型导致的内存访问越界
· [EF Core]聊聊“复合”属性
· 那些被推迟的 C# 14 特性及其背后的故事
· 我最喜欢的 C# 14 新特性
· 博客园出海记-开篇:扬帆启航
· 微软开源的 MCP 教程「GitHub 热点速览」
· 记一次 .NET 某汽车控制焊接软件 卡死分析
· InnoDB为什么不用跳表,Redis为什么不用B+树?
· 糟糕,生产环境频繁Full GC,怎么办?