In this article I will explain with an example, how to use Dapper library with Stored Procedure in ASP.Net using C# and VB.Net.
 
 

What is Dapper?

Dapper is a simple micro-ORM used to simplify working with ADO.Net.
Dapper is an open-source object-relational mapping (ORM) library for .NET and .NET Core applications. This library allows developers quickly and easily access data from databases.
Dapper allows you to execute raw SQL queries, map the results to objects and execute Stored Procedure.
Dapper is lightweight and fast, making it an ideal choice for applications that require low latency and high performance.
It has support for both asynchronous and synchronous database queries and batching multiple queries together into a single call.
Additionally, dapper supports parameterized queries to help protect against SQL injection attacks.
 
 

Installing Dapper package using Nuget

In order to install Dapper library using Nuget, please refer my article Install Dapper from Nuget in Visual Studio.
 
 

Database

I have made use of the following table Customers with the schema as follows.
How to use Dapper with Stored Procedure in ASP.Net
 
I have already inserted few records in the table.
How to use Dapper with Stored Procedure in ASP.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure

The following Stored Procedure is used to display the records from SQL Server database.
CREATE PROCEDURE [Customers_GetCustomers]
AS
BEGIN
    SET NOCOUNT ON;
    SELECT [CustomerId]
          ,[Name]
          ,[Country]
    FROM [Customers]
END
 
 

HTML Markup

The following form consists of:
GridView – For displaying data.
The GridView consists of three BoundFIeld columns.
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 

Namespaces

You will need to import the following namespaces.
C#
using Dapper;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports Dapper
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
 
 

Using Dapper with Stored Procedure in ASP.Net

Inside the Page_Load event handler, the records are fetched from the Customers Table of SQL Server database using ExecuteReader method of Dapper library with Stored Procedure and copied to DataTable object using Load method.
Note: For more details on how to use ExecuteReader method, please refer my article Understanding Dapper ExecuteReader in C# and VB.Net.
 
Finally, the DataTable is assigned to the DataSource property of GridView and DataBind method is called.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string spName = "Customers_GetCustomers";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (IDataReader sdr = con.ExecuteReader(spName, commandType: CommandType.StoredProcedure))
            {
                using (DataTable dtCustomers = new DataTable())
                {
                    dtCustomers.Load(sdr);
                    gvCustomers.DataSource = dtCustomers;
                    gvCustomers.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim spName As String = "Customers_GetCustomers"
        Using con As SqlConnection = New SqlConnection(constr)
            Using sdr As IDataReader = con.ExecuteReader(spName, commandType:=CommandType.StoredProcedure)
                Using dtCustomers As DataTable = New DataTable()
                    dtCustomers.Load(sdr)
                    gvCustomers.DataSource = dtCustomers
                    gvCustomers.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub
 
 

Screenshot

How to use Dapper with Stored Procedure in ASP.Net
 
 

Downloads