在一个往返使用Dapper.NET多个SQL语句 [英] Multiple SQL statements in one roundtrip using Dapper.NET

查看:1395
本文介绍了在一个往返使用Dapper.NET多个SQL语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有在ADO.NET一个很好的功能,可以让您将多个SQL语句发送到数据库在一个往返并获得结果的所有语句:

There is a nice feature in ADO.NET that allows you to send multiple SQL statements to database in one roundtrip and receive results for all statements:

var command = new SqlCommand("SELECT count(*) FROM TableA; SELECT count(*) FROM TableB;", connection);

using(var reader = command.ExecuteReader())
{
    reader.Read();
    resultA = reader.GetInt32(0);
    reader.NextResult();
    reader.Read();
    resultB = reader.GetInt32(0);
}



有没有Dapper.NET类似的功能?

Is there a similar feature in Dapper.NET?

推荐答案

是的,小巧玲珑的 QueryMultiple 扩展可以做到这一点:

Yes, the Dapper QueryMultiple extension can do that:

string query = @"SELECT COUNT(*) FROM TABLEA
                 SELECT COUNT(*) FROM TABLEB";
using (var multi = connection.QueryMultiple(query, null))
{
    int countA = multi.Read<int>().Single();
    int countB = multi.Read<int>().Single();
}     



据马克Gravell这是一个执行多个查询的理想方法。批

According to Marc Gravell this is the ideal way to execute multiple queries in a single batch.

请注意:小巧玲珑的创造者萨姆藏红花具有贴有代码示例一个详细说明如何使用 QueryMultiple 来做到这一点。

Note: Dapper creator Sam Saffron has posted a detailed explanation with code sample on using QueryMultiple to accomplish this.

这篇关于在一个往返使用Dapper.NET多个SQL语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆