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.
Installing Dapper package using Nuget
Database
I have made use of the following table Customers with the schema as follows.
Note: You can download the database table SQL by clicking the download link below.
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.
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.
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.
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.
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.
Thus, concluding it, we must use Execute for INSERT, UPDATE and DELETE operations only.