In this article I will explain how to use and MySQL Connector to connect to MySQL database in Windows Forms (WinForms) application using C# and VB.Net.
For illustration purposes, the DataGridView control with records from MySQL Database using C# and VB.Net.
 
Database
I have made use of the following table Customers with the schema as follows.
How to use MySQL Connector in C# and VB.Net
 
I have already inserted few records in the table.
How to use MySQL Connector in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
         Download SQL file
 
 
Download and Install the MySQL Connector
You will need to download and install the MySQLConnector in order to connect to the MySQL database in Windows Forms Application.
After installation is complete you need to open Windows Explorer and look for the MySql installation in the Program Files folder of your Windows drive.
There you will find a folder for MySQL Connector and inside that you will find the MySql.Data.dll which you need to copy inside the BIN folder of your project.
How to use MySQL Connector in C# and VB.Net
 
 
MySql Connection String
Below is the connection string to the MySql Database.
<connectionStrings>
    <add name="constr" connectionString="Data Source=localhost;port=3306;Initial Catalog=SampleDB;User Id=mudassar;password=pass@123" />
</connectionStrings>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
 
 
Binding the DataGridView with records from MySQL Database Table
Inside the Form Load event handler, the BindGrid method is called.
Inside the BindGrid method, first the Connection String value is set and then the Connection String is used to initialize the MySqlCommand object.
Then, an object of MySqlDataAdapter is created using the MySqlCommand object and the DataTable is filled using the Fill method of the MySqlDataAdapter class.
Finally, the DataTable is assigned as DataSource to the DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
    this.BindGrid();
}
 
private void BindGrid()
{
    string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(conString))
    {
        using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Customers", con))
        {
            cmd.CommandType = CommandType.Text;
            using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgsHandles MyBase.Load
    Me.BindGrid()
End Sub
 
Private Sub BindGrid()
    Dim constring As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New MySqlConnection(conString)
        Using cmd As New MySqlCommand("SELECT * FROM Customers", con)
            cmd.CommandType = CommandType.Text
            Using sda As New MySqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    dataGridView1.DataSource = dt
                End Using
            End Using
        End Using
    End Using
End Sub
 
 
Screenshot
How to use MySQL Connector in C# and VB.Net
 
 
Downloads