In this article I will explain with an example, how to select data form database using Dapper library in ASP.Net MVC.

Installing Dapper package using Nuget

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


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


The Model class consists of following properties.
public class Customer
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }


You will need to import the following namespaces.
using Dapper;
using System.Configuration;
using System.Data.SqlClient;


The Controller consists of following Action method.

Action Method for handling GET operation

Inside this Action method, the records are fetched from the Customers Table of the SQL Server database using Query method of Dapper library and returned to the View.
public class HomeController : Controller
    // GET: Home
    public ActionResult Index()
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        using (SqlConnection con = new SqlConnection(constr))
            return View(con.Query<Customer>(sql));


Inside the View, in the very first line the Customer Model is declared as IEnumerable which specifies that it will be available as a Collection.
For displaying the records, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@model IEnumerable<Customer>
@using Dapper_Select_MVC.Models
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
    <table cellpadding="0" cellspacing="0">
            <th>Customer Id</th>
        @foreach (Customer customer in Model)


Select using Dapper in ASP.Net MVC