In this article I will explain with an example, how to use Dapper library in Windows Forms (WinForms) Application 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 procedures.
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.


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

Form Design

The following form consists of:
DataGridView – For displaying data.
How to use Dapper in C# and VB.Net


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

Selecting record from database using Dapper in Windows Forms

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 of Northwind database using ExecuteReader method of Dapper library and copied to DataTable object using Load method.
Finally, DataTable is assigned to the DataSource property of DataGridView.
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())
                dgvCustomers.DataSource = dtCustomers;
Private Sub Form1_Load(ByVal 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()
                dgvCustomers.DataSource = dtCustomers
            End Using
        End Using
    End Using
End Sub


How to use Dapper in C# and VB.Net