Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dapper能否批处理一组存储的过程调用?_C#_.net_Dapper - Fatal编程技术网

C# Dapper能否批处理一组存储的过程调用?

C# Dapper能否批处理一组存储的过程调用?,c#,.net,dapper,C#,.net,Dapper,是否可以批处理一组存储的过程调用?我在文档中看到它支持多个结果,但我不确定是否可以使用Dapper执行多个存储过程调用。Dapper支持存储过程的批处理命令: connection.Execute("create table #t (i int)"); connection.Execute("create proc #spInsert @i int as insert #t values (@i)"); connection.Execute("#spInsert", new[] { new {

是否可以批处理一组存储的过程调用?我在文档中看到它支持多个结果,但我不确定是否可以使用Dapper执行多个存储过程调用。

Dapper支持存储过程的批处理命令:

connection.Execute("create table #t (i int)");
connection.Execute("create proc #spInsert @i int as insert #t values (@i)");
connection.Execute("#spInsert", new[] { new { i = 1 }, new {i = 2}, new {i = 3} }, 
    commandType: CommandType.StoredProcedure);

var nums = connection.Query<int>("select * from #t order by i").ToList();

nums[0].IsEqualTo(1);
nums[1].IsEqualTo(2);
nums[2].IsEqualTo(3);
这将导致插入三行


此外,如果
#spInsert
返回一个结果集,您可以使用
QueryMultiple
执行批处理,这将给您3个记录集进行迭代

使用类似的方法,将19个过程调用作为一个批处理发送,而不是单独执行,我的性能提高了约3倍(141s vs 421)。使用StringBuilder从列表中准备查询和@param_i名称,其中i是索引。
connection.Execute(@"
    exec #spInsert @i = @one 
    exec #spInsert @i = @two 
    exec #spInsert @i = @three",
    new { one = 1, two = 2, three = 3 });