Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » SQL Server 2005 » Stored Procedure

Stored Procedure

This article shows that what are the stored procedure. How we will declare and use stored procedure?

Author Rank :
Page Views : 12672
Downloads : 217
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
AStoredProcedureApplication.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

In this article I am going to explain about stored procedure. What is stored procedure?  How we will define and use a stored procedure in our programme?

A stored procedure is a named group of SQL statements that have been previously created and stored in the server database.
They accept data in the form of input parameters that are specified at execution time.

Benefits of Stored procedure

  • Precompiled Execution
  • Reduced Client/Server Traffic
  • Efficient reuse of code and programming abstraction
  • Enhanced Security control 

Let see the stored procedure with this programme. I have stored the database inside the zip folder. Just attach the database and change the connection string to run the application

Firstly open the Microsoft SQL Server -> Enterprise Manager and right click on that database which you are going to use like this:



Figure 1.

After clicking here a window will open to write our stored procedure.



Figure 2: Write stored procedure here.

The AStoredProcedureAppliacation.aspx code:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>A Stored Procedure</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <table cellpadding="0" cellspacing="0" width="62%" align="center">

    <tr><td>

         <asp:Label ID="lblName" runat="server" Text="Name" Font-Bold="true" Width="100px"> </asp:Label>

         <asp:TextBox ID="txtName" runat="server"></asp:TextBox>   

    </td></tr>

    <tr><td>

         <asp:Label ID="Label1" runat="server" Text="Address" Font-Bold="true" Width="100px"> </asp:Label>

         <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>   

    </td></tr>

    <tr><td>

         <asp:Label ID="Label2" runat="server" Text="City" Font-Bold="true" Width="100px"> </asp:Label>

         <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>   

    </td></tr>

    <tr><td>

         <asp:Label ID="Label3" runat="server" Text="Country" Font-Bold="true" Width="100px"> </asp:Label>

         <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>   

    </td></tr>

    <tr><td height="10px"></td></tr>

    <tr><td>

         <asp:Button ID="btnSubmit" runat="server" OnClick="SubmitRecord" Text="Submit" />

    </td></tr>

    </table>

   </div>

  </form>

 </body>

</html>

The .cs code is:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    SqlConnection con;

    SqlCommand cmd ;

    SqlParameter sp1 = new SqlParameter();

    SqlParameter sp2 = new SqlParameter();

    SqlParameter sp3 = new SqlParameter();

    SqlParameter sp4 = new SqlParameter();

 

    protected void SubmitRecord(object sender, EventArgs e)

    {

        con = new SqlConnection("Data source=MCN101; Initial Catalog=databaseForProgramme; Uid=sa; pwd=");

 

        sp1.ParameterName = "@Name";

        sp1.Direction = ParameterDirection.Input;

        sp1.Value = txtName.Text;

 

        sp2.ParameterName = "@Address";

        sp2.Direction = ParameterDirection.Input;

        sp2.Value = txtAddress.Text;

 

        sp3.ParameterName = "@City";

        sp3.Direction = ParameterDirection.Input;

        sp3.Value = txtCity.Text;

 

        sp4.ParameterName = "@Country";

        sp4.Direction = ParameterDirection.Input;

        sp4.Value = txtCountry.Text;

 

        cmd = new SqlCommand("InsertRecord", con);

        cmd.CommandType = CommandType.StoredProcedure;

 

        //Instead Of doing above code like as sp1.Nmae,sp1.Address,sp1.city,sp1.Country we can do it like as:

 

        //cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value=txtName.Text;

        //cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value=txtAddress.Text;

        //cmd.Parameters.Add("@City", SqlDbType.NVarChar).Value=txtCity.Text;

        //cmd.Parameters.Add("@Country", SqlDbType.NVarChar).Value=txtCountry.Text;

 

        cmd.Parameters.Add(sp1);

        cmd.Parameters.Add(sp2);

        cmd.Parameters.Add(sp3);

        cmd.Parameters.Add(sp4);

 

        con.Open();

        cmd.ExecuteNonQuery();

        con.Close();

    }

}

When user run the application the window will look like this:




Figure 3.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Rahul Kumar Saxena
Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books.
(Talabpur*)
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
tankxxx by tari On November 9, 2010
hi,thats very good to implementaion the very useful & simple concept
continue this act.
tanxxxxx
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.