MS SQL Server
  Home arrow MS SQL Server arrow Page 3 - 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 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 / 19
    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 stored procedure with a single parameter using ADO.NET from ASP.NET


    (Page 3 of 4 )

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

    CREATE PROCEDURE dbo.sp_IncrementSalariesBy
          (
                @increment int
          )
    AS
          UPDATE emp SET sal = sal + @increment
          RETURN

    The above stored procedure shall simply increase all salaries of the employees by the value (or parameter) provided by the calling program.  The stored procedure is named "sp_IncrementSalariesBy" and the parameter is named "@increment."  Now, we need to have ADO.NET access the same 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 the "SQLConnection" object).
    • Create a SQL Server command (using the "SQLCommand" object).
    • Specify the properties to the "SQLCommand" object.
    • Create a new parameter to handle our communication (using the "SQLParameter" object).
    • Add the parameter 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.  Add a new web form (call it  "StoredProcedureWithSingleParam") to the already created project in the previous section.  Add a button named "Execute SP with Single Parameter."

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

    Imports System.Data.SqlClient

    Add the following code to your "Execute SP with Single Parameter" 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_IncrementSalariesBy"
                Dim paramIncrement As New SqlParameter
                With paramIncrement
                    .ParameterName = "@increment"
                    .SqlDbType = SqlDbType.Int
                    .Value = 300
                End With
                .Parameters.Add(paramIncrement)
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                .Connection.Close()
                .Dispose()
            End With
    End Sub

    Set the start page, execute your application (by pressing F5), and click on the button "Execute SP with Single Parameter."  Once it executes successfully, check your table "emp," which should get updated with new salaries (incremented by 300). 

    In the above code, I simply hard coded the value 300.  You can get the user specified value using a text box and assign it directly, to make it really dynamic.

    You can proceed to the next section with tips on simplifying the above code.

    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 1 hosted by Hostway