In this article I will explain with an example, how to delete data from 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.
Delete with Stored Procedure using Dapper in C# and VB.Net
 
I have already inserted few records in the table.
Delete with Stored Procedure using Dapper 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 Delete data from the SQL Server database table.
This Stored Procedure accepts CustomerId parameter, which is used to DELETE the records in Customers Table.
CREATE PROCEDURE [Customers_DeleteCustomer]
    @CustomerId INT
AS
BEGIN
    DELETE FROM [Customers]
    WHERE CustomerId = @CustomerId
END
 
 

Form Design

The following Form consists of:
TextBox – For capturing CustomerId of the Customer record to be deleted.
Button – For deleting the record.
Delete with Stored Procedure using Dapper in C# and VB.Net
 
 

Namespaces

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

Deleting with Stored Procedure using Dapper

When Delete button is clicked, first the connection string 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, using Execute method of Dapper library with Stored Procedure record is deleted from the SQL Server.
Note: For more details on Execute method, please refer my article Understanding Dapper Execute in C# and VB.Net.
 
Finally, based on whether record is deleted or not, an appropriate message will be displayed using MessageBox class.
C#
private void OnDelete(object sender, EventArgs e)
{
    string spName = "Customers_DeleteCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        int customerId = int.Parse(txtId.Text);
        int i = con.Execute(spName, new { customerId }, commandType: CommandType.StoredProcedure);
        if (i > 0)
        {
            MessageBox.Show("Customer record deleted.");
        }
        else
        {
            MessageBox.Show("Customer not found.");
        }
    }
 
    txtId.Text = string.Empty;
}
 
VB.Net
Private Sub OnDelete(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim spName As String = "Customers_DeleteCustomer"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(constr)
        Dim customerId As Integer = Integer.Parse(txtId.Text)
        Dim i As Integer = con.Execute(spName, New With {customerId}, commandType:=CommandType.StoredProcedure)
        If i > 0 Then
            MessageBox.Show("Customer record deleted.")
        Else
            MessageBox.Show("Customer not found.")
        End If
    End Using
 
    txtId.Text = String.Empty
End Sub
 
 

Screenshot

Delete with Stored Procedure using Dapper in C# and VB.Net
 
 

Downloads