Sam*_*ron 10
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);
Run Code Online (Sandbox Code Playgroud)
上面的代码重复使用IDbCommand和文本#spInsert3次.这使得批处理插入更有效.
通常,如果您担心此级别的perf,您可以在事务中包装批处理调用.
此外,Dapper支持您决定发送的任何批次:
connection.Execute(@"
exec #spInsert @i = @one
exec #spInsert @i = @two
exec #spInsert @i = @three",
new { one = 1, two = 2, three = 3 });
Run Code Online (Sandbox Code Playgroud)
这会导致插入三行.
此外,如果#spInsert返回结果集,您可以使用它QueryMultiple来执行批处理,这将为您提供3个记录集以进行迭代.
| 归档时间: |
|
| 查看次数: |
2924 次 |
| 最近记录: |