sql 多线程
2024-09-04 16:13:54
1、codeproject上面一个多线程执行sql的库。
开发中常常要执行一些耗时的数据操作,比如update、insert、bulk insert、index creation,
若顺序执行,则操作总时间是每个单独sql的时间之和 为了加快速度,采用线程池异步执行的做法,比如要要create 10个表以及加数据,则可以开10个线程去分别执
codeproject上库源码地址 http://www.codeproject.com/Articles/29356/Asynchronous-T-SQL-Execution-Without-Service-Broke
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlTypes;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SqlClrLib.Model; namespace ExampleTransaction
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
//开发中常常要执行一些耗时的数据操作,比如update、insert、bulk insert、index creation,
//若顺序执行,则操作总时间是每个单独sql的时间之和,
//为了加快速度,引入异步执行的做法,比如要要create 10个表以及加数据,则可以开10个线程去分别执行
} [Microsoft.SqlServer.Server.SqlProcedure]
public static int SaveWarehouseFinalData(Guid fileDefinitionId)
{
Console.WriteLine("Start");
StoredProcedures.Parallel_Declare(new SqlString("Parallel SaveWarehouseFinalData"));
//Console.WriteLine("Database connection is ... {0}", StoredProcedures.Block.ConnectionString);
//Console.WriteLine("Max threads count is ... {0}", StoredProcedures.Block.MaxThreads);
////ParallelProcedures.Block.IsTransactional = true;
////ParallelProcedures.Parallel_UseTransaction("Serializable");
//依次将存储过程加入到线程池
StoredProcedures.Parallel_AddSql(new SqlString("usp_WarehouseImportSaveToEAV"), new SqlChars(string.Format("usp_WarehouseImportSaveToEAV '{0}'", fileDefinitionId)));
StoredProcedures.Parallel_AddSql(new SqlString("usp_WarehouseImportSaveToAdminHistory"), new SqlChars(string.Format("usp_WarehouseImportSaveToAdminHistory '{0}'", fileDefinitionId)));
StoredProcedures.Parallel_AddSql(new SqlString("usp_WarehouseImportSaveToValResult"), new SqlChars(string.Format("usp_WarehouseImportSaveToValResult '{0}'", fileDefinitionId)));
//执行save操作(此处用的事务,如果不成功会回滚)
int failedCount = StoredProcedures.Parallel_Execute();
//Console.WriteLine("Failed count is ... {0}", failedCount);
ResultItem[] resultItems = (ResultItem[])StoredProcedures.Parallel_GetExecutionResult();
foreach (ResultItem r in resultItems)
{
//Console.WriteLine(r.ToString());
}
//Console.WriteLine("End..."); return failedCount;
} }
}
最新文章
- WinFrom窗体始终置顶
- Eclipse 常用快捷键 For MAC
- java初学者应掌握的30个基本概念
- ::after::before清除浮动原理
- Windows读取文本文件后的显示过程
- 关于Look and Say序列的感想
- Unity3D之MeleeWeaponTrail武器轨迹插件的使用
- Android学习–Android app 语言切换功能
- zabbix 通过smtp 邮件报警
- js alert(“”)弹框 自定义样式
- Redis学习-Set
- 发布一个Python小程序:ManHourCalendar
- 高性能伪事务之Lua in Redis
- 基本的CRUD操作
- angular.identity()
- python2.x 与 python3.x的不同
- 2017百度软研(C++)
- Spring JDBC模版以及三种数据库连接池的使用
- 关于Java 软件工程师应该知道或掌握的技术栈
- tomcat整体架构
热门文章
- python3爬虫爬取煎蛋网妹纸图片(下篇)2018.6.25有效
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
- 第1天-html快速入门
- ASP.net 资源请求漏洞利用工具PadBuster
- English distilled
- [Codeforces 30D] Kings Problem
- 【递归】【栈】先修课 计算概论(A)/函数递归练习(2)5:布尔表达式
- gitk图形界面中文乱码情况
- Zookeeper的功能以及工作原理(转)
- ASP.NET MVC DropdownList的使用