Saturday, September 18, 2010

Executing Store Procedure by ADO Command Object, Passing Input paramer and store the result in Recordset object


This is a simple example in which i demonstrated how to pass input parameters using command object and save the results in record set object.As mention in my previous post dated  : August 18, 2010,passing input parameters with record set is convenient but it has certain serious drawbacks
  • With command object Output parameters of Store procedures are retrieval while with connection object it’s not possible. 
  • Executing Store procedure using connection object is not very efficient as it take more time to parse the Sql Statements.





Table

CREATE TABLE [dbo].[Employee](
    [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
    [EmployeeFirstName] [varchar](50) ,
    [Salary] [varchar](50) ,
 CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
    [EmployeeID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

Store procedure

Create Procedure Sp_Employee
    @EmployeeID Int
AS
    Select * From Employee Where EmployeeID = @EmployeeID


ASP Code for passing input parameter 

<%@LANGUAGE="VBSCRIPT" %>

<%
    Dim con
    set con =server.createobject("ADODB.Connection")
    Con.Open "Provider=SQLOLEDB; Data Source =localhost; Initial Catalog = test; User Id  =sa;pwd=sa"
    Set ObjCom = server.CreateObject("ADODB.Command")
    Set ObjCom.ActiveConnection = Con
    ObjCom.CommandType = adCmdStoredProc 'also you can use 4
    ObjCom.CommandText = "Sp_Employee"
    ObjCom.parameters.Append  ObjCom.CreateParameter("@EmployeeID", adInteger, adParamInput,,1)
   Set Rs = Server.CreateObject("ADODB.RecordSet")
        RS = ObjCom.Execute
    Response.Write(RS(1))
%>

No comments:

Post a Comment