In this article I will explain with an example, how to update data into
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.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Stored Procedure
This
Stored Procedure accepts
CustomerId,
Name and
Country parameters, which are used to UPDATE the records in
Customers Table.
DELIMITER //
CREATE PROCEDURE Customers_UpdateCustomer(
IN CustomerId INT,
IN Name VARCHAR(100) ,
IN Country VARCHAR(50)
)
BEGIN
UPDATE Customers
SET Name = Name,
Country = Country
WHERE Customers.CustomerId = CustomerId;
END//
DELIMITER ;
Form Design
The Form consists of following controls:
Label – For labeling the controls.
TextBox – For capturing the values of CustomerId, Name and Country.
Button – For updating record.
The Button has been assigned with a Click event handler.
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
Updating the record into MySQL Database using Stored Procedure in C# and VB.Net
When Update button is clicked, first the connection is read from App.Config file.
The CustomerId, Name and Country fields are fetched from their respective TextBoxes.
Finally, based on whether record is updated or not, an appropriate message is displayed using MessageBox class.
C#
private void OnUpdate(object sender, EventArgs e)
{
string spName = "Customers_UpdateCustomer";
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);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
MessageBox.Show("Customer record updated.");
}
else
{
MessageBox.Show("Customer not found.");
}
}
}
}
VB.Net
Private Sub OnUpdate(sender As Object, e As EventArgs) Handles btnUpdate.Click
Dim spName As String = "Customers_UpdateCustomer"
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)
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
con.Open()
Dim i As Integer = cmd.ExecuteNonQuery()
con.Close()
If i > 0 Then
MessageBox.Show("Customer record updated.")
Else
MessageBox.Show("Customer not found.")
End If
End Using
End Using
End Sub
Screenshot
Downloads