In this article I will explain with an example, how to use ExecuteReader method from MySQL database using Dapper library in ASP.Net using C# and VB.Net.
Note: For more details on how to use MySQL database, please refer my article How to use MySQL Connector in ASP.Net.
 
 

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.
Understanding Dapper ExecuteReader in MySQL in C# and VB.Net
 
I have already inserted few records in the table.
Understanding Dapper ExecuteReader in MySQL in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

ExecuteReader

The ExecuteReader method of Dapper library is used for retrieving data from database.
The Dapper ExecuteReader method is an extension method provided by the IDbConnection interface.
The MySQL query or Stored Procedure and an array of parameters that will be used in the query are passed as parameter to this method.
While using Dapper ExecuteReader method, there is no need to open the connection manually that is necessary in the case of ExecuteReader method of MySqlCommand class.
The operation opening and closing connection is handled internally in Dapper library.
 
 

HTML Markup

The HTML Markup consists of following control:
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.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports Dapper
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 

Fetching Data using ExecuteReader from MySQL Database

Inside the Page_Load event handler, first the connection is read from Web.Config file.
Note: For more details on how to read connection string from Web.Config file, please refer my article Read (Get) Connection String from Web.Config file in ASP.Net using C# and VB.Net.
 
Then, the records are fetched from Customers Table of MySQL database using ExecuteReader method of Dapper library and copied to DataTable object using Load method.
Finally, DataTable is assigned to the DataSource property of GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string sql = "SELECT CustomerId, Name, Country FROM Customers";
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (MySqlConnection con = new MySqlConnection(constr))
        {
            using (IDataReader sdr = con.ExecuteReader(sql))
            {
                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 sql As String = "SELECT CustomerId, Name, Country FROM Customers"
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As MySqlConnection = New MySqlConnection(constr)
            Using sdr As IDataReader = con.ExecuteReader(sql)
                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

Understanding Dapper ExecuteReader in MySQL in C# and VB.Net
 
 

Downloads