In this article I will explain with an example, how to select data from MySQL database using Stored Procedure in Windows Forms (WinForms) Application with C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Select from MySQL Database using Stored Procedure in C# and VB.Net
 
I have already inserted few records in the table.
Select from MySQL Database using Stored Procedure in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

Stored Procedure

The following Stored Procedure is used to display the records from MySQL database.
DELIMITER //
CREATE PROCEDURE Customers_GetCustomers()
BEGIN
    SELECT CustomerId, Name,Country
    FROM Customers
END//
DELIMITER ;
 
 

Form Design

The Form consists of following control:
DataGridView – For displaying data.
Select from MySQL Database using Stored Procedure in C# and VB.Net
 
 

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
 
 

Selecting from MySQL Database using Stored Procedure 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.
 
Then, a connection to the database is established using MySqlConnection class.
After that, records are fetched from the Customers Table of MySQL database using Stored Procedure and copied to DataTable using Fill method.
Finally, the DataTable is assigned to the DataSource property of DataGridView.
C#
private void Form1_Load(object sender, EventArgs e)
{
    string spName = "Customers_GetCustomers";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand(spName, con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            using (MySqlDataAdapter sdr = new MySqlDataAdapter(cmd))
            {
                using (DataTable dt = new DataTable())
                {
                    sdr.Fill(dt);
                    dgvCustomers.DataSource = dt;
                }
            }
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim spName As String = "Customers_GetCustomers"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As MySqlConnection = New MySqlConnection(constr)
        Using cmd As MySqlCommand = New MySqlCommand(spName, con)
            cmd.CommandType = CommandType.StoredProcedure
            Using sdr As MySqlDataAdapter = New MySqlDataAdapter(cmd)
                Using dt As DataTable = New DataTable()
                    sdr.Fill(dt)
                    dgvCustomers.DataSource = dt
                End Using
            End Using
        End Using
    End Using
End Sub
 
 

Screenshot

Select from MySQL Database using Stored Procedure in C# and VB.Net
 
 

Downloads