In this article I will explain with an example, how to insert data into
MySQL database using
Stored Procedure in Windows Forms (WinForms) Application with C# and VB.Net.
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.
Stored Procedure
This
Stored Procedure accepts
Name and
Country parameters, which are used to
Insert the records in
Customers Table.
DELIMITER //
CREATE PROCEDURE Customers_InsertCustomer(
IN Name VARCHAR(100),
IN Country VARCHAR(50)
)
BEGIN
INSERT INTO Customers(Name, Country)
VALUES(Name, Country);
SELECT LAST_INSERT_ID()
END//
DELIMITER ;
Form Design
The Form consists of following controls:
Label – For labeling the controls.
TextBox – For capturing Name to be inserted.
ComboBox – For selecting Country name to be inserted.
Button – For inserting 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
Inserting into MySQL database using Stored Procedure in C# and VB.Net
Inside the Form Load event handler, the ComboBox items are added.
When Insert button is clicked, first the connection is read from App.Config file.
Then, the
Name value is fetched from the
TextBox, while the
Country value is fetched from the
ComboBox are passed as parameter and inserted into the
MySQL database using
Stored Procedure using
ExecuteScalar method of
MySqlCommand class.
Finally, the TextBox and ComboBox values are set to empty and CustomerId of the inserted record is displayed in MessageBox class.
C#
private void Form1_Load(object sender, EventArgs e)
{
cbCountries.Items.Add("Please Select");
cbCountries.Items.Add("United States");
cbCountries.Items.Add("India");
cbCountries.Items.Add("France");
cbCountries.Items.Add("Russia");
}
private void OnInsert(object sender, EventArgs e)
{
int customerId;
string spName = "Customers_InsertCustomer";
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("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem);
con.Open();
customerId = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
txtName.Text = string.Empty;
cbCountries.Text = string.Empty;
MessageBox.Show("Inserted Customer ID: " + customerId);
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cbCountries.Items.Add("Please Select")
cbCountries.Items.Add("United States")
cbCountries.Items.Add("India")
cbCountries.Items.Add("France")
cbCountries.Items.Add("Russia")
End Sub
Private Sub OnInsert(sender As Object, e As EventArgs) Handles btnInsert.Click
Dim customerId As Integer
Dim spName As String = "Customers_InsertCustomer"
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("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", cbCountries.SelectedItem)
con.Open()
customerId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
txtName.Text = String.Empty
cbCountries.Text = String.Empty
MessageBox.Show("Inserted Customer ID: " & customerId)
End Sub
Screenshots
The Form
Record after Insert in database
Downloads