In this post, I'll show you how to handle multiple queries with Dapper ORM using C#. You can excute more than one query statement within the same command and map results.
using (IDbConnection db = new SqlConnection(ConnectionString))
{
if (db.State == ConnectionState.Closed)
db.Open();
var query = db.QueryMultiple("SELECT COUNT(*) FROM Company;SELECT* FROM Company WHERE Id = @Id", new { Id = Id }, commandType: CommandType.Text);
var totalRow = query.Read<int>().First();
var result = query.Read<Company>().SingleOrDefault();
}
Multiple queries in Dapper ORM are useful for executing a batch of SQL commands whose results produce not only one resultset, but also multiple results. This avoids adding roundtrips to the database.