MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - Working with T-SQL Stored Procedures using...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

Working with T-SQL Stored Procedures using ADO.NET
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2006-04-11

    Table of Contents:
  • Working with T-SQL Stored Procedures using ADO.NET
  • How to execute a simple stored procedure using ADO.NET from ASP.NET
  • How to execute a stored procedure with a single parameter using ADO.NET from ASP.NET
  • How to execute a stored procedure with multiple parameters using ADO.NET from ASP.NET

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with T-SQL Stored Procedures using ADO.NET - How to execute a simple stored procedure using ADO.NET from ASP.NET


    (Page 2 of 4 )

    Before going into the ADO.NET code, we need to create a simple stored procedure in SQL Server.  Using the "Query Analyzer", execute the following script in the "Northwind" database.

    CREATE PROCEDURE dbo.sp_IncrementSalaries
    AS
          UPDATE emp SET sal = sal + 500
          RETURN

    The above stored procedure shall simply increase all the salaries of the employees by 500.  The stored procedure is named "sp_IncrementSalaries."  Now, we need to have ADO.NET access the same thing in ASP.NET.

    The following are the steps you need to take to execute a simple stored procedure with ADO.NET:

    • Create and open a SQL Server connection (using a "SQLConnection" object).
    • Create a SQL Server command (using a "SQLCommand" object).
    • Specify the properties to the "SQLCommand" object.
    • Assign the "SQLConnection" object to the "SQLCommand" object.
    • Execute the stored procedure using the "ExecuteNonQuery" method.
    • Close the "SQLConnection" and release all memory resources.

    We will just follow the above steps.  Start by creating your own web application using Visual Studio.NET (I named mine "SP" for this demonstration).  Design your web form (call it "SimpleStoredProcedure") with a single button named "Execute Simple SP."

    After designing the form, switch to the code and add the following line at the top.

    Imports System.Data.SqlClient

    I am importing the namespace "System.Data.SqlClient," as I would like to connect to the Microsoft SQL Server database natively.  You can also use other .NET providers based on your requirements.  Add the following code to your "Execute Simple SP" button:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles Button1.Click
            Dim cn As New SqlConnection("Data Source=.;initial
    catalog=Northwind;user id=sa")
            Dim cmd As New SqlCommand
            With cmd
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_IncrementSalaries"
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                .Connection.Close()
                .Dispose()
            End With
        End Sub

    Once you complete the above, you execute your application (by pressing F5) and click on the button "Execute Simple SP."  Once it executes successfully, check your table "emp," which should get updated with new salaries (incremented by 500).

    You can simplify the above code as follows:

        Private Sub Button1_Click(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles Button1.Click
            Dim cmd As New SqlCommand("sp_IncrementSalaries", New
    SqlConnection("Data Source=.;initial catalog=Northwind;user
    id=sa"))
            With cmd
                .CommandType = CommandType.StoredProcedure
                .Connection.Open()
                .ExecuteNonQuery()
                .Connection.Close()
                .Dispose()
            End With
        End Sub

    In this article, I shall give preference to clarity (rather than simplicity).  So, I shall proceed with the first method from now on.

    More MS SQL Server Articles
    More By Jagadish Chaterjee


       · Hello guys. Enjoy the first in series reg. SQL Server Stored Procedures and...
     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT