Dapper - Execute Multiple Stored Procedures

Dapper provides extension methods to execute multiple queries within the same command. The same can be used to execute multiple stored procedures.

commit 8af30e1 · authored · amended · 2 min read

Dapper - Execute Multiple Stored Procedures

Dapper is my preferred ORM for performance constraint applications. It is one of the best solution in case of a database first approach as well as when there is already a database in place. Dapper works with SQL Server as well as so many other DB’s and I have tried Oracle DB apart from SQL Server.

Dapper have so many features/extension methods, the documentation of these features can be accessed here.

👉 Dapper Dapper Tutorial | Dapper Tutorial and Documentation .

To execute multiple stored procedures or multiple SQL statements, we can use the QueryMultipleAsync extension method. See the sample code below,

using Dapper;
...
public async Task<SomeClass> GetAsync(Guid someId, Guid anotherId)
{
using (var connection = new SqlConnection(_settings.ConnectionString()))
{
connection.Open();
const string sql = @"exec uspTableOneAction @someId;
exec uspTableTwoAnotherAction @anotherId;";
using (var multi = await connection.QueryMultipleAsync(sql, new { someId, anotherId }))
{
var firstTableItem = multi.Read<TableOneItem>().First();
var secondTableItem = multi.Read<TableTwoItem>().FirstOrDefault();
...
}
...
}
}

In the above code, my stored procedure are,

  • uspTableOneAction
  • uspTableTwoAnotherAction

The naming convention which we follows for stored procedures are,

usp{TableName}{Action}

eg: uspUserInsert, uspUserUpdate

And in the above example, first stored procedure expects a parameter @someId. And the second stored procedure expects @anotherId. We passed these args by

new { someId, anotherId }

The same can be written as,

new { someId = someId, anotherId = anotherId }

Why we omitted the right hand side part in the example is because of the arguments name matching the parameter.

Just in case if both stored procedures expecting same parameter, say @someId. Then our args will look like,

new { someId }

That’s it. You have the result of the two queries.

Additional Resources

local graph · open full graph

"Dapper - Execute Multiple Stored Procedures" is connected to the topics Dapper, SQL Server, ORM and related to 8 other entries. Style Guide - SQL Server Auditing Column Names Style Guide - SQL Server Au… SQL Server - Delete Duplicate Rows SQL Server - Delete Duplica… Create SQL Server Database From a Script in Docker-Compose Create SQL Server Database … Microsoft SQL Server Guy Trying Oracle Database Microsoft SQL Server Guy Tr… Docker - SQL Error on ASP.NET Core Alpine Docker - SQL Error on ASP.N… Filtering, Paging and Sorting in SQL Server 2008 Filtering, Paging and Sorti… Create New Database Level User Create New Database Level U… UTC to UAE Time (Arabian Standard Time) UTC to UAE Time (Arabian St… Dapper #dapper SQL Server #sql-server ORM #orm Dapper - Execute Multiple Stored …

comments

loading discussion…