MS SQL Server
  Home arrow MS SQL Server arrow Page 4 - 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 stored procedure with multiple parameters using ADO.NET from ASP.NET


    (Page 4 of 4 )

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

    CREATE PROCEDURE dbo.IncrementSalaryFor
          (
                @empno int,
                @increment float
          )
    AS
          UPDATE emp SET sal = sal + @increment WHERE empno = @empno
          RETURN
     
    The above stored procedure will simply increase the salary of a particular employee based on the values sent through parameters.  The stored procedure is named "sp_IncrementSalariesFor" and the parameters are named "@empno" and "@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 a "SQLConnection" object).
    • Create a SQL Server command (using a "SQLCommand" object).
    • Specify the properties to the "SQLCommand" object.
    • Create two parameters to handle our communication (using the "SQLParameter" object).
    • Add the two parameters to the "SQLCommand" object.
    • Assign the "SQLConnection" object to "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  "StoredProcedureWithMultipleParam") to the already created project in the previous section.  Add a button named "Execute SP with Multiple 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 Multiple 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_IncrementSalariesFor"
                .Parameters.Add("@empno", 1001) 'specify accordingly
                .Parameters.Add("@increment", 1001) 'specify
    accordingly
                .Connection = cn
                .Connection.Open()
                .ExecuteNonQuery()
                .Connection.Close()
                .Dispose()
            End With
        End Sub

    You can observe the above code for simplifying the "parameter" objects.

    I developed the application using Microsoft Windows Server 2003 Standard Edition with Microsoft Visual Studio.NET 2003 Enterprise Architect and Microsoft SQL Server 2000.  Even though I didn't test on all versions, the code in this article should work with any Windows OS (supporting .NET) and any .NET version with any SQL Server version.  If anything could not work, please drop me line so that I can guide you.

    In the next article, we shall deal with more interesting topics like OUTPUT parameters, returning values from database to ADO.NET, and so on. Watch for it or sign up for a newsletter to get notified.

    Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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