In this article I will explain with an example, how to delete data from MySQL database 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 using Dapper from MySql Database in C# and VB.Net
 
I have already inserted few records in the table.
Delete 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
 
 

Form Design

The Form consists of following controls:
TextBox – For capturing CustomerId of the Customer record to be deleted.
Button – For deleting the record.
The Button has been assigned with a Click event handler.
Delete using Dapper from MySql Database in C# and VB.Net
 
 

Namespaces

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

Deleting records from MySql database using Dapper in C# and VB.Net

When Delete 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 SQL query and an object with CustomerId values are passed to the Execute method of the Dapper library which then deletes the record from the Customers Table.
Note: For more details on Execute method, please refer my article Understanding Dapper Execute with MySQL in C# and VB.Net.
 
Finally, based on whether record is deleted or not, an appropriate message is displayed using MessageBox class and TextBox is set to empty.
C#
private void OnDelete(object sender, EventArgs e)
{
    string sql = "DELETE FROM Customers WHERE CustomerId = @CustomerId";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        int customerId = int.Parse(txtId.Text);
        int i = con.Execute(sql, new { customerId });
        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 sql As String = "DELETE FROM Customers WHERE CustomerId = @CustomerId"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As MySqlConnection = New MySqlConnection(constr)
        Dim customerId As Integer = Integer.Parse(txtId.Text)
        Dim i As Integer = con.Execute(sql, New With {customerId})
        If i > 0 Then
            MessageBox.Show("Customer record deleted.")
        Else
            MessageBox.Show("Customer not found.")
        End If
 
        txtId.Text = String.Empty
    End Using
End Sub
 
 

Screenshot

Delete using Dapper from MySql Database in C# and VB.Net
 
 

Downloads