How do you call a stored procedure with Dapper?

May 15, 2020

During one of my latest renditions of Better Object Relational Mapping with Dapper, the question arose about using stored procedures.

How do you call a stored procedure with Dapper?

using (var connection = new SqlConnection(CONNECTION_STRING))
{
    var storedProcedureName = "getAllUsers";

    var results = await connection
        .QueryAsync<ApplicationUser>(storedProcedureName, 
                                commandType: CommandType.StoredProcedure);
}

Looking at the code above, calling a stored procedure is similar to calling any other query with Dapper.

SignalR Mastery: Become a Pro in Real-Time Web Development

Join the thousands of developers who have already taken their first steps into building real-time web applications with SignalR.

The best SignalR Course I've ever watched ever. explaining all the details you need to know and even more about SignalR technology.
Exactly what I am looking for. Really appreciate the real world scenario. Thank you.
The author of the course really shows he knows what he is talking about. Very awesome!

But what if your Stored Procedure doesn't return a result? No problem.

ExecuteAsync
works just as well.

using (var connection = new SqlConnection(CONNECTION_STRING))
{
    var storedProcedureName = "deleteAllUsers";

    await connection.ExecuteAsync(storedProcedureName, 
                            commandType: CommandType.StoredProcedure);
}

And there you go! Using stored procedures with Dapper can be a great way to take advantage of the strengths of the database while reducing complexity of your code.