In this article I will explain with an example, how to select data from MySQL database using Dapper in Windows Forms (WinForms) Application with 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.
Select using Dapper from MySql Database in C# and VB.Net
 
I have already inserted few records in the table.
Select using Dapper from MySql Database 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 Form consists of following control:
DataGridView – For displaying data.
Select using Dapper from MySql Database in C# and VB.Net
 
 

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
 
 

Selecting record from MySql database using Dapper in C# and VB.Net

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.
 
The connection to the database is established using the MySqlConnection class.
Then, using ExecuteReader method of Dapper library, the records are fetched from the Customers Table and copied to DataTable object using Load method.
Note: For more details on how to use ExecuteReader, please refer my article Using Dapper ExecuteReader in MySQL in C# and VB.Net.
 
Finally, DataTable is assigned to the DataSource property of DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
    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);
                dgvCustomers.DataSource = dtCustomers;
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    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)
                dgvCustomers.DataSource = dtCustomers
            End Using
        End Using
    End Using
End Sub
 
 

Screenshot

Select using Dapper from MySql Database in C# and VB.Net
 
 

Downloads