In this article I will explain with an example, how to work with
Stored Procedure in
ADO.Net using C# and VB.Net in ASP.Net.
What is Stored Procedure?
A
Stored Procedure is a group of SQL queries that can be saved and reused multiple times.
It is nothing but a group of SQL statements that accepts some input in the form of parameters, performs some task, and may or may not return a value.
It is very useful as it reduces the need for rewriting SQL queries. It enhances efficiency, reusability, and security in database management.
Stored Procedure are created to perform one or more DML operations on the database.
Syntax to Create a Stored Procedure
CREATE PROCEDURE PROCEDURE_NAME
parameter1 data_type
,parameter2 data_type
,…
AS
BEGIN
-- SQL statements to be executed.
END
Syntax to Execute the Stored Procedure
EXEC PROCEDURE_NAME parameter1_value, parameter2_value,..
Benefits of Using Stored Procedures
1. Performance: Stored procedures are precompiled and optimized, which often results in better performance compared to executing individual SQL statements from the application.
2. Reduced Network Traffic: Since stored procedures execute on the server side, only the call to execute the procedure and the parameters need to be sent over the network, reducing the amount of data transferred.
3. Code Reusability: Stored procedures can be reused across multiple applications or different parts of the same application, promoting consistency and reducing code duplication.
4. Security: By using stored procedures, you can encapsulate the database logic and control access more effectively. Users can be granted permission to execute the stored procedure without direct access to the underlying tables.
5. Maintainability: Changes to database logic can be made in one place (the stored procedure) rather than updating multiple application codebases.
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.
Implementing Stored Procedure
CREATE PROCEDURE [Customers_GetCustomers]
AS
BEGIN
SET NOCOUNT ON;
SELECT [CustomerId]
,[Name]
,[Country]
FROM [Customers]
END
HTML Markup
The HTML Markup consists of following controls:
GridView – For displaying data.
Columns
The GridView consists of fouor BoundField columns
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="Customer Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Working with Stored Procedure in C# and VB.Net
Inside the
Page_Load event handler, the records are fetched from the
Customers Table of
SQL Server database using
Stored Procedure and DataTable is filled.
Finally, the DataTable is assigned to the DataSource property of GridView and GridView is populated.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string spName = "Customers_GetCustomers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(spName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim spName As String = "Customers_GetCustomers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(spName, con)
cmd.CommandType = CommandType.StoredProcedure
Using sda As New SqlDataAdapter(cmd)
Using dt As New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
Screenshot
Downloads