In this article I will explain with an example, how to insert data into MySQL database with Stored Procedure using 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.
Insert with Stored Procedure 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
 
 

Stored Procedure

The following Stored Procedure will be used to Insert data into the MySQL database table.
This Stored Procedure accepts Name and Country parameters, which are used to Insert the records in Customers Table.
DELIMITER //
CREATE PROCEDURE Customers_InsertCustomer(
    IN Name VARCHAR(100),
    IN Country VARCHAR(50)
)
BEGIN
    INSERT INTO Customers(Name, Country)
    VALUES(Name, Country);
    SELECT LAST_INSERT_ID()
END//
DELIMITER ;
 
 

Form Design

The Form consists of following controls:
Label – For labeling the controls.
TextBox – For capturing Name to be inserted.
ComboBox – For selecting Country name to be inserted.
Button – For inserting record.
The Button has been assigned with a Click event handler.
Insert with Stored Procedure 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
 
 

Inserting with Stored Procedure into MySql database using Dapper in C# and VB.Net

Inside the Form Load event handler, the ComboBox items are added.
When Insert button is clicked, 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, the name of the Stored Procedure and an object with Name and Country values are passed to the ExecuteScalar method of the Dapper library which then inserts the record in the Customers Table.
Note: For more details on ExecuteScalar method, please refer my article Understanding Dapper ExecuteScalar with MySQL in C# and VB.Net.
 
Finally, the TextBox and ComboBox value is set to empty and CustomerId of the inserted record is displayed in MessageBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
    cbCountries.Items.Add("United States");
    cbCountries.Items.Add("India");
    cbCountries.Items.Add("France");
    cbCountries.Items.Add("Russia");
}
 
private void OnInsert(object sender, EventArgs e)
{
    int customerId;
    string spName = "Customers_InsertCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        string name = txtName.Text;
        string country = cbCountries.SelectedItem.ToString();
        customerId = Convert.ToInt32(con.ExecuteScalar(spName, new { name, country }, commandType: CommandType.StoredProcedure));
    }
    txtName.Text = string.Empty;
    cbCountries.Text = string.Empty;
    MessageBox.Show("Inserted Customer ID: " + customerId);
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    cbCountries.Items.Add("United States")
    cbCountries.Items.Add("India")
    cbCountries.Items.Add("France")
    cbCountries.Items.Add("Russia")
End Sub
 
Private Sub OnInsert(sender As Object, e As EventArgs) Handles btnInsert.Click
    Dim customerId As Integer
    Dim spName As String = "Customers_InsertCustomer"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As MySqlConnection = New MySqlConnection(constr)
        Dim name As String = txtName.Text
        Dim country As String = cbCountries.SelectedItem.ToString()
        customerId = Convert.ToInt32(con.ExecuteScalar(spName, New With {name, country}, commandType:=CommandType.StoredProcedure))
    End Using
    txtName.Text = String.Empty
    cbCountries.Text = String.Empty
    MessageBox.Show("Inserted Customer ID: " & customerId)
End Sub
 
 

Screenshots

The Form

Insert with Stored Procedure using Dapper from MySql Database in C# and VB.Net
 

Record after Insert in database

Insert with Stored Procedure using Dapper from MySql Database in C# and VB.Net
 
 

Downloads