In this article I will explain with an example, how to insert data into database with
Stored Procedure using
Dapper library in Windows Forms (WinForms) Application using C# and VB.Net.
Installing Dapper package using Nuget
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.
CREATE PROCEDURE [Customers_InsertCustomer]
@Name VARCHAR(100),
@Country VARCHAR(50)
AS
BEGIN
INSERT INTO [Customers]
([Name]
,[Country])
VALUES
(@Name
,@Country)
SELECT SCOPE_IDENTITY()
END
Form Design
The following Form consists of:
Label – For labelling the controls.
TextBox – For capturing Name to be inserted.
ComboBox – For selecting Country name to be inserted.
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
Inserting with Stored Procedure using Dapper in Windows Forms
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.
Finally, the TextBox and ComboBox values are set to empty and CustomerId of the inserted record is displayed in MessageBox.
C#
private void Form1_Load(object sender, EventArgs e)
{
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 (SqlConnection con = new SqlConnection(constr))
{
string name = txtName.Text;
string country = cbCountries.SelectedItem.ToString();
customerId = Convert.ToInt32(con.ExecuteScalar(spName, new { name, country }, commandType: CommandType.StoredProcedure));
}
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("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 SqlConnection = New SqlConnection(constr)
Dim name As String = txtName.Text
Dim country As String = cbCountries.SelectedItem.ToString()
customerId = Convert.ToInt32(con.ExecuteScalar(spName, New With { name, country }, commandType:=CommandType.StoredProcedure))
End Using
txtName.Text = String.Empty
cbCountries.Text = String.Empty
MessageBox.Show("Inserted Customer ID: " & customerId)
End Sub
Screenshot
Record after Insert in database
Downloads