In this article I will explain with an example, how to use Execute method of Dapper library in MySQL with C# and VB.Net while developing ASP.Net, Windows and Console applications.
Note: For beginners in ASP.Net Core (.Net Core 7), please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

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.
Understanding Dapper Execute in MySQL in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 

The Execute method

Execute is basically used for operations where there is nothing returned from the MySQL Query or Stored Procedure. Preferred use will be for INSERT, UPDATE and DELETE operations.
 
 

Namespaces

You will need to import the following namespaces.
C#
using Dapper;
using MySql.Data.MySqlClient;
using System.Configuration;
 
VB.Net
Imports Dapper
Imports MySql.Data.MySqlClient
Imports System.Configuration
 

INSERT

Initially, the connection is read from the App.Config or Web.Config file and object of MySqlConnection class is created using it.
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.
           For more details on how to read connection string from Web.Config file, please refer my article Read (Get) Connection String from Web.Config file in ASP.Net using C# and VB.Net.
 
Then, the SQL query and Name, Country values are passed to the Execute method of the Dapper library which then inserts the record in the Customers Table.
Finally, the number of affected rows is returned.
C#
string name = "Mudassar Khan";
string country = "India";
string sql = "INSERT INTO Customers (Name, Country) VALUES (@Name, @Country)";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
    int rowsAffected = con.Execute(sql, new { name, country });
}
 
VB.Net
Dim name As String = "Mudassar Khan"
Dim country As String = "India"
Dim sql As String = "INSERT INTO Customers (Name, Country) VALUES (@Name, @Country)"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As MySqlConnection = New MySqlConnection(constr)
    Dim rowsAffected As Integer = con.Execute(sql, New With { name, country })
End Using
 
The screenshot below displays the rows affected.
Understanding Dapper Execute in MySQL in C# and VB.Net
 
 

UPDATE

Initially, the connection is read from the App.Config or Web.Config file and object of MySqlConnection class is created using it.
Then, the SQL query and Name, Country values are passed to the Execute method of the Dapper library which then updates the record in the Customers Table.
Finally, the number of affected rows is returned.
C#
string name = "Mudassar Khan";
string country = "India";
string sql = "UPDATE Customers SET Country = @Country WHERE Name = @Name";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
    int rowsAffected = con.Execute(sql, new { name, country });
}
 
VB.Net
Dim name As String = "Mudassar Khan"
Dim country As String = "India"
Dim sql As String = "UPDATE Customers SET Country = @Country WHERE Name = @Name"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As MySqlConnection = New MySqlConnection(constr)
    Dim rowsAffected As Integer = con.Execute(sql, New With { name, country })
End Using
 
The screenshot below displays the rows affected.
Understanding Dapper Execute in MySQL in C# and VB.Net
 
 

DELETE

Initially, the connection is read from the App.Config or Web.Config file and object of MySqlConnection class is created using it.
Then, the SQL query and Name, Country values are passed to the Execute method of the Dapper library which then deletes the record from the Customers Table.
Finally, the number of affected rows is returned.
C#
string name = "Mudassar Khan";
string sql = "DELETE FROM Customers WHERE Name = @Name";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
    int rowsAffected = con.Execute(sql, new { name });
}
 
VB.Net
Dim name As String = "Mudassar Khan"
Dim sql As String = "DELETE FROM Customers WHERE Name = @Name"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As MySqlConnection = New MySqlConnection(constr)
    Dim rowsAffected As Integer = con.Execute(sql, New With { name })
End Using
 
The screenshot below displays the rows affected.
Understanding Dapper Execute in MySQL in C# and VB.Net
 
 

What happens when I use Execute for SELECT statement?

Execute will work flawlessly for SELECT MySQL Query or Stored Procedure but that will simply execute the query and do nothing. Even if you use it, you will not throw any error but the Rows Affected will be negative i.e. -1.
C#
string sql = "SELECT * FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
    int rowsAffected = con.Execute(sql);
}
 
VB.Net
Dim sql As String = "SELECT * FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As MySqlConnection = New MySqlConnection(constr)
    Dim rowsAffected As Integer = con.Execute(sql)
End Using
 
The screenshot below displays the rows affected returned -1.
Understanding Dapper Execute in MySQL in C# and VB.Net
 
Thus, concluding it, we must use Execute for INSERT, UPDATE and DELETE operations only.