In this article I will explain with an example, how to use
Execute method of
Dapper library in 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 SQL 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 System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports Dapper
Imports System.Data.SqlClient
Imports System.Configuration
INSERT
Initially, the connection string is fetched from the App.Config or Web.Config file and object of SqlConnection class is created using it.
Then, the INSERT query is passed to Execute method as parameter.
The values of the name and country are also added as parameter to
Execute method of
Dapper library.
Finally, the Execute function is executed and 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 (SqlConnection con = new SqlConnection(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 SqlConnection = New SqlConnection(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 string is fetched from the App.Config or Web.Config file and object of SqlConnection class is created using it.
Then, the UPDATE query is passed to Execute method as parameter.
The values of the name and country are also added as parameter to
Execute method of
Dapper library.
Finally, the Execute function is executed and 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 (SqlConnection con = new SqlConnection(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 SqlConnection = New SqlConnection(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 string is fetched from the App.Config or Web.Config file and object of SqlConnection class is created using it.
Then, the DELETE query is passed to Execute method as parameter.
The value of the name is also added as parameter to
Execute method of
Dapper library.
Finally, the Execute function is executed and 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 (SqlConnection con = new SqlConnection(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 SqlConnection = New SqlConnection(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 SQL 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 (SqlConnection con = new SqlConnection(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 SqlConnection = New SqlConnection(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.