'**通过Connection对象调用存储过程**
DIM MyConn,MyRst
Set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.open MyConStr 'MyConStr是数据库连接字串
Set MyRst = MyConn.Execute("getUserList",0,4) '最后一个参断含义同CommandType
Set MyConn = Nothing
'**通过Recordset对象调用存储过程**
DIM MyRst
Set MyRst = Server.CreateObject("ADODB.Recordset")
MyRst.open "getUserList",MyConStr,0,1,4
'MyConStr是数据库连接字串,最后一个参断含义与CommandType相同
2. 没有输入输出的存储过程
请看以下存储过程:
/*SP2*/
CREATE PROCEDURE dbo.delUserAll
as
set nocount on
begin
delete from dbo.[userinfo]
end
go
/*SP3*/
CREATE PROCEDURE dbo.delUserAll
as
set nocount on
begin
BEGIN TRANSACTION
delete from dbo.[userinfo]
IF @@error=0
begin
COMMIT TRANSACTION
return 1
end
ELSE
begin
ROLLBACK TRANSACTION
return 0
end
return
end
go
/*SP4*/
CREATE PROCEDURE dbo.getUserName
@UserID int,
@UserName varchar(40) output
as
set nocount on
begin
if @UserID is null return
select @UserName=username
from dbo.[userinfo]
where userid=@UserID
return
end
go
/*SP5*/
CREATE PROCEDURE dbo.getUserName
--为了加深对"顺序"的印象,将以下两参数的定义顺序颠倒一下
@UserName varchar(40) output,
@UserID int
as
set nocount on
begin
if @UserID is null return
select @UserName=username
from dbo.[userinfo]
where userid=@UserID
if @@rowcount>0
return 1
else
return 0
return
end
go
'**调用同时具有返回值、输入参数、输出参数的存储过程**
DIM MyComm,UserID,UserName
UserID = 1
Set MyComm = Server.CreateObject("ADODB.Command")
with MyComm
.ActiveConnection = MyConStr 'MyConStr是数据库连接字串
.CommandText = "getUserName" '指定存储过程名
.CommandType = 4 '表明这是一个存储过程
.Prepared = true '要求将SQL命令先行编译
'返回值要最先被声明
.Parameters.Append .CreateParameter("RETURN",2,4)
'以下两参数的声明顺序也做相应颠倒
.Parameters.append .CreateParameter("@UserName",200,2,40)
.Parameters.append .CreateParameter("@UserID",3,1,4,UserID)
.Execute
end with
if MyComm(0) = 1 then
UserName = MyComm(1)
else
UserName = "该用户不存在"
end if
Set MyComm = Nothing
/*SP6*/
CREATE PROCEDURE dbo.getUserList
@iPageCount int OUTPUT, --总页数
@iPage int, --当前页号
@iPageSize int --每页记录数
as
set nocount on
begin
--创建临时表
create table #t (ID int IDENTITY, --自增字段
userid int,
username varchar(40))
--向临时表中写入数据
insert into #t
select userid,username from dbo.[UserInfo]
order by userid
--取得记录总数
declare @iRecordCount int
set @iRecordCount = @@rowcount
--确定总页数
IF @iRecordCount%@iPageSize=0
SET @iPageCount=CEILING(@iRecordCount/@iPageSize)
ELSE
SET @iPageCount=CEILING(@iRecordCount/@iPageSize)+1
--若请求的页号大于总页数,则显示最后一页
IF @iPage > @iPageCount
SELECT @iPage = @iPageCount
--确定当前页的始末记录
DECLARE @iStart int --start record
DECLARE @iEnd int --end record
SELECT @iStart = (@iPage - 1) * @iPageSize
SELECT @iEnd = @iStart + @iPageSize + 1
--取当前页记录
select * from #t where ID>@iStart and ID<@iEnd
'**调用分页存储过程**
DIM pagenow,pagesize,pagecount,recordcount
DIM MyComm,MyRst
pagenow = Request("pn")
'自定义函数用于验证自然数
if CheckNar(pagenow) = false then pagenow = 1
pagesize = 20
Set MyComm = Server.CreateObject("ADODB.Command")
with MyComm
.ActiveConnection = MyConStr 'MyConStr是数据库连接字串
.CommandText = "getUserList" '指定存储过程名
.CommandType = 4 '表明这是一个存储过程
.Prepared = true '要求将SQL命令先行编译
'返回值(记录总量)
.Parameters.Append .CreateParameter("RETURN",2,4)
'出参(总页数)
.Parameters.Append .CreateParameter("@iPageCount",3,2)
'入参(当前页号)
.Parameters.append .CreateParameter("@iPage",3,1,4,pagenow)
'入参(每页记录数)
.Parameters.append .CreateParameter("@iPageSize",3,1,4,pagesize)
Set MyRst = .Execute
end with
if MyRst.state = 0 then '未取到数据,MyRst关闭
recordcount = -1
else
MyRst.close '注意:若要取得参数值,需先关闭记录集对象
recordcount = MyComm(0)
pagecount = MyComm(1)
if cint(pagenow)>=cint(pagecount) then pagenow=pagecount
end if
Set MyComm = Nothing
'以下显示记录
if recordcount = 0 then
Response.Write "无记录"
elseif recordcount > 0 then
MyRst.open
do until MyRst.EOF
......
loop
'以下显示分页信息
......
else 'recordcount=-1
Response.Write "参数错误"
end if
/*SP7*/
CREATE PROCEDURE dbo.getUserInfo
@userid int,
@checklogin bit
as
set nocount on
begin
if @userid is null or @checklogin is null return
select username
from dbo.[usrinfo]
where userid=@userid
--若为登录用户,取usertel及usermail
if @checklogin=1
select usertel,usermail
from dbo.[userinfo]
where userid=@userid
return
end
go
以下是ASP代码:
'**调用返回多个记录集的存储过程**
DIM checklg,UserID,UserName,UserTel,UserMail
DIM MyComm,MyRst
UserID = 1
'checklogin()为自定义函数,判断访问者是否登录
checklg = checklogin()
Set MyComm = Server.CreateObject("ADODB.Command")
with MyComm
.ActiveConnection = MyConStr 'MyConStr是数据库连接字串
.CommandText = "getUserInfo" '指定存储过程名
.CommandType = 4 '表明这是一个存储过程
.Prepared = true '要求将SQL命令先行编译
.Parameters.append .CreateParameter("@userid",3,1,4,UserID)
.Parameters.append .CreateParameter("@checklogin",11,1,1,checklg)
Set MyRst = .Execute
end with
Set MyComm = Nothing
'从第一个记录集中取值
UserName = MyRst(0)
'从第二个记录集中取值
if not MyRst is Nothing then
Set MyRst = MyRst.NextRecordset()
UserTel = MyRst(0)
UserMail = MyRst(1)
end if
Set MyRst = Nothing
DIM MyComm
Set MyComm = Server.CreateObject("ADODB.Command")
'调用存储过程一
......
Set MyComm = Nothing
Set MyComm = Server.CreateObject("ADODB.Command")
'调用存储过程二
......
Set MyComm = Nothing
......
2. 只创建一个Command对象,结束一次调用时,清除其参数
DIM MyComm
Set MyComm = Server.CreateObject("ADODB.Command")
'调用存储过程一
.....
'清除参数(假设有三个参数)
MyComm.Parameters.delete 2
MyComm.Parameters.delete 1
MyComm.Parameters.delete 0
'调用存储过程二并清除参数
......
Set MyComm = Nothing
此时要注意:清除参数的顺序与参数声明的顺序相反,原因嘛,我也不知道。
3. 利用Parameters数据集合的Refresh方法重置Parameter对象
DIM MyComm
Set MyComm = Server.CreateObject("ADODB.Command")
'调用存储过程一
.....
'重置Parameters数据集合中包含的所有Parameter对象
MyComm.Parameters.Refresh
'调用存储过程二
.....
Set MyComm = Nothing
一般认为,重复创建对象是效率较低的一种方法,但是经测试(测试工具为Microsoft Application Center Test),结果出人意料:
方法2 >= 方法1 >> 方法3
方法2的运行速度大于等于方法1(最多可高4%左右),这两种方法的运行速度远大于方法3(最多竟高达130%),所以建议在参数多时,采用方法1,在参数较少时,采用方法2。