In this article I will explain with an example, how to delete data from MySQL database using Stored Procedure in Windows Forms (WinForms) Application with C# and VB.Net.
 
 

Download and Install the MySQL Connector

You will need to download and install the MySQL Connector in order to connect to the MySQL database in Windows Forms Application.
Note: For details on how to download and install the MySQL Connector, please refer my article Download, install and reference MySQL Connector in C# and VB.Net.
 
 

Database

I have made use of the following table Customers with the schema as follows.
Delete from MySQL Database using Stored Procedure in C# and VB.Net
 
I have already inserted few records in the table.
Delete from MySQL Database using Stored Procedure 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 MySQL database table.
This Stored Procedure accepts CustomerId parameter, which is used to DELETE the records in Customers Table.
DELIMITER //
CREATE PROCEDURE Customers_DeleteCustomer(
    IN CustomerId INT
)
BEGIN
    DELETE FROM Customers
    WHERE Customers.CustomerId = CustomerId;
END//
DELIMITER ;
 
 

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 from MySQL Database using Stored Procedure in C# and VB.Net
 
 

Namespaces

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

Deleting from MySQL Database using Stored Procedure 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.
 
The CustomerId field is fetched from TextBox as passed parameter.
Then, using ExecuteNonQuery method record is deleted from the MySQL database using Stored Procedure.
Note: For more details on ExecuteNonQuery method, please refer my article Understanding MySqlCommand ExecuteNonQuery in C# and VB.Net.
 
Finally, based on whether record is deleted or not, an appropriate message is displayed using MessageBox class.
C#
private void OnDelete(object sender, EventArgs e)
{
    string spName = "Customers_DeleteCustomer";
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (MySqlConnection con = new MySqlConnection(constr))
    {
        using (MySqlCommand cmd = new MySqlCommand(spName, con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text);
            con.Open();
            int i = cmd.ExecuteNonQuery();
            con.Close();
 
            if (i > 0)
            {
                MessageBox.Show("Customer record deleted.");
            }
            else
            {
                MessageBox.Show("Customer not found.");
            }
        }
    }
}
 
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 MySqlConnection = New MySqlConnection(constr)
        Using cmd As MySqlCommand = New MySqlCommand(spName, con)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@CustomerId", txtId.Text)
            con.Open()
            Dim i As Integer = cmd.ExecuteNonQuery()
            con.Close()
 
            If i > 0 Then
                MessageBox.Show("Customer record deleted.")
            Else
                MessageBox.Show("Customer not found.")
            End If
        End Using
    End Using
End Sub
 
 

Screenshot

Delete from MySQL Database using Stored Procedure in C# and VB.Net
 
 

Downloads