In this article I will explain with an example, how to use ExecuteReader method of Dapper library in Windows Forms (WinForms) Application using C# and VB.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 C# and VB.Net
 
I have already inserted few records in the table.
Understanding Dapper ExecuteReader 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 SQL 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 SqlCommand class.
The operation opening and closing connection is handled internally in Dapper library.
 
 

Form Design

The following Form consists of:
DataGridView – For displaying data.
Understanding Dapper ExecuteReader in C# and VB.Net
 
 

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
 
 

Fetching Data using ExecuteReader method of Dapper

Inside the Form Load event handler, first the connection is read from App.Config file.
Note: For more details on how to read connection string from App.Config file, please refer my article .Net 4.5: Read (Get) Connection String from App.Config file using C# and VB.Net.
 
Then, a connection to the database is established using the SqlConnection class.
The records are fetched from the Customers Table using ExecuteReader method of Dapper library and copied to DataTable object using Load method.
Finally, DataTable is assigned to the DataSource property of DataGridView and the DataGridView is populated.
C#
private void Form1_Load(object sender, EventArgs e)
{
    string sql = "SELECT CustomerId, Name, Country FROM Customers";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (IDataReader sdr = con.ExecuteReader(sql))
        {
            using (DataTable dtCustomers = new DataTable())
            {
                dtCustomers.Load(sdr);
                dgvCustomers.DataSource = dtCustomers;
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim sql As String = "SELECT CustomerId, Name, Country FROM Customers"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Using sdr As IDataReader = con.ExecuteReader(sql)
            Using dtCustomers As DataTable = New DataTable()
                dtCustomers.Load(sdr)
                dgvCustomers.DataSource = dtCustomers
            End Using
        End Using
    End Using
End Sub
 
 

Screenshot

Understanding Dapper ExecuteReader in C# and VB.Net
 
 

Downloads