网络编程
位置:首页>> 网络编程>> Asp编程>> ASP中使用存储过程介绍(2)

ASP中使用存储过程介绍(2)

 来源:CSDN 发布时间:2008-10-10 12:10:00 

标签:存储过程,sql,数据库,asp

本文介绍存储过程如何在ASP中运用。

简单的一个SQL语句:

select ID,Name,Picture,Time,Duty from employ 

我们可以创建一个存储过程:

CREATE PROCEDURE sp_employ
AS
select ID,Name,Picture,Time,Duty from employ 
Go

而SQL语句:

select ID,Name,Picture,Time,Duty from employ where ID=10230

对应的存储过程是:(用Alter替换我们已有的存储过程)

ALTER PROCEDURE sp_employ
@inID  int
AS
select ID,Name,Picture,Time,Duty from employ  where ID=@inID
Go

下面对比一下SQL和存储过程在ASP中的情况。首先看看直接执行SQL的情况:

<%
dim Conn, strSQL, rs
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password" 
strSQL = "select ID,Name,Picture,Time,Duty from employ "
Set rs = Conn.Execute(strSQL) 
%> 

再看看如何执行Stored Procedure:

<%
dim Conn, strSQL, rs
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password" ’make connection
strSQL = "sp_employ"
Set rs = Conn.Execute(strSQL) 
%> 

而执行带参数的Stored Procedure也是相当类似的:

<%
dim Conn, strSQL, rs, myInt
myInt = 1 
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password"
strSQL = "sp_myStoredProcedure " & myInt
Set rs = Conn.Execute(strSQL) 
%> 

在SQL Server中执行存储过程

在SQL Server的查询分析器中,输入以下代码:

declare @tot_amt int
execute order_tot_amt 1,@tot_amt output
select @tot_amt

以上代码是执行order_tot_amt这一存储过程,以计算出定单编号为1的定单销售金额,我们定义@tot_amt为输出参数,用来承接我们所要的结果

在ASP中调用存储过程

<!--必须加载adovbs.inc文件,否则将出错-->
<!--#include file="adovbs.inc"-->
<%
dim objCnn
dim objCmd
dim Rs
const o_id=112
'-----建立Connection对象----------
set objCnn=Server.CreateObject("Adodb.connection")
objCnn.Open "driver={sql server};server=localhost;uid=sa;pwd=cncanet;database=check;"
'-----建立Command对象-----------
set objCmd=Server.CreateObject("Adodb.Command")
objCmd.ActiveConnection=objCnn
objCmd.CommandText="order_tot_amt" '指定存储过程名称
objCmd.CommandType=adCmdStoredProc '其为Stored Procedure
'-----准备stored procedure 的参数-------
objCmd.Parameters.Append _
 objCmd.CreateParameter("o_id",adInteger,adParamInput,,o_id)
objCmd.Parameters.Append _
 objCmd.CreateParameter("p_tot",adBigInt,adParamOutput,,0)
'-----执行存储过程----------------------
objCmd.Execute
'-----输出参数以及处理结果--------------
for each parm in objCmd.Parameters
 Response.Write parm.name &"="& trim(parm) &"<br>"
next
%>
0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com