当前位置: 首页 > 图文教程 > 网络编程 > ASP > ASP 3.0高级编程(四十二)

ASP
ASP语法高亮类代码
ASP实现长文章分页
上一篇,下一篇过程代码
插件下载┊垃圾引用防御补丁(每小时自动换KEY,支持静态页面)
完美解决PJ的Cookies保存时限问题!可选择记录登陆时长!
ASP,PHP与.NET伪造HTTP-REFERER方法及防止伪造REFERER方法探讨
用VB生成DLL封装ASP连接数据库的代码
利用ActiveX控件InetCtls.Inet在ASP中实现新闻小偷
验证码识别技术
另类扩展名同样执行ASP
用JAVASCRIPT帮我写个计数器
用ASP读取XML文件的具体方法与示例
很有用的学习ASP常用到的代码
VBScript中变量作用域
可以查询百度排名的asp源码放送了
可以查询google排名的asp源码
用asp实现文件浏览、上传、下载的程序
Ajax+ASP和Flash+ASP数据读取取方法有些相似的实现方法
ASP与数据库,有用的代码(转贴,摘贴)
将首页转成静态html页的asp文件

ASP 3.0高级编程(四十二)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 201 ::
收藏到网摘: n/a

5. 返回值
对函数返回值的处理不同于存储过程返回值的处理,这常常导致混淆。在函数中,经常是返回一个布尔值来表明函数运行的成功与否。
If SomeFunctionName() = True Then
' Function succeeded
但在调用一个存储过程时,却不能使用同样的方法,因为存储是用Execute方法运行的,同时返回一个记录集。
Set rsAuthors = cmdAuthors.Execute
如果得不到一个返回值,如何确定是否已正确执行存储过程?当发生错误时,会报告错误,这样就可使用前一章提供的错误处理代码来处理错误。但对于一些非致命的逻辑错误怎么办?
例如,考虑向employee表添加一个新职员的情形。你可能不想防止两个职员同名的情况,但想注明这个情况。那么,可以使用一个返回值以表明是否已有同名的职员存在。存储过程如下:
CREATE PROCEDURE usp_AddEmployee
@Emp_ID Char(9),
@FName Varchar(20),
@Minit Char(1),
@LName Varchar(30),
@Job_ID SmallInt,
@Job_Lvl TinyInt,
@Pub_ID Char(4),
@Hire_Date Datetime
AS
BEGIN
DECLARE @Exists Int -- Return value

-- See if an employee with the same name exists
IF EXISTS(SELECT *
FROM Employee
WHERE FName = @FName
AND MInit = @MInit
AND LName = @LName)
SELECT @Exists = 1
ELSE
SELECT @Exists = 0

INSERT INTO Employee (emp_id, fname, minit, lname,
job_id, job_lvl, pub_id, hire_date)
VALUES (@Emp_Id, @FName, @MInit, @LName, @Job_ID,
@Job_Lvl, @Pub_ID, @Hire_Date)
RETURN @Exists
END
该过程首先检查是否有同名的职员存在,并据此设定相应的变量Exists,若存在同名,就设为1,否则为0。然后将该职员加到表中,同时把Exists的值作为返回值返回。
注意尽管返回了一个值,但并未将其声明为存储过程的参数。
调用该过程的ASP代码如下:
<!-- #INCLUDE FILE="../include/Connection.asp" -->
<%
Dim cmdEmployee
Dim lngRecs
Dim lngAdded

Set cmdEmployee = Server.CreateObject("ADODB.Command")

' Set the properties of the command
With cmdEmployee
.ActiveConnection = strConn
.CommandText = "usp_AddEmployee"
.CommandType = adCmdStoredProc

' Create the parameters
' Notice that the return value is the first parameter
.Parameters.Append .CreateParameter ("RETURN_VALUE", adInteger, _
adParamReturnValue)
.Parameters.Append .CreateParameter ("@Emp_id", adChar, adParamInput, 9)
.Parameters.Append .CreateParameter ("@fname", adVarWChar, adParamInput, 20)
.Parameters.Append .CreateParameter ("@minit", adChar, adParamInput, 1)
.Parameters.Append .CreateParameter ("@lname", adVarWChar, adParamInput, 30)
.Parameters.Append .CreateParameter ("@job_id", adSmallInt, adParamInput)
.Parameters.Append .CreateParameter ("@job_lvl", adUnsignedTinyInt, adParamInput)
.Parameters.Append .CreateParameter ("@pub_id", adChar, adParamInput, 4)
.Parameters.Append .CreateParameter ("@hire_date", adDBTimeStamp, _
adParamInput, 8)

' Set the parameter values
.Parameters("@Emp_id") = Request.Form("txtEmpID")
.Parameters("@fname") = Request.Form("txtFirstName")
.Parameters("@minit") = Request.Form("txtInitial")
.Parameters("@lname") = Request.Form("txtLastName")
.Parameters("@job_id") = Request.Form("lstJobs")
.Parameters("@job_lvl") = Request.Form("txtJobLevel")
.Parameters("@pub_id") = Request.Form("lstPublisher")
.Parameters("@hire_date") = Request.Form("txtHireDate")

' Run the stored procedure
.Execute lngRecs, , adExecuteNoRecords

' Extract the return value
lngAdded = .Parameters("RETURN_VALUE")
End With

Response.Write "New employee added.<P>"
If lngAdded = 1 Then
Response.Write "An employee with the same name already exists."
End If

Set cmdEmployee = Nothing
%>
需要重点注意,返回值应当作为集合中第一个参数被创建。即使返回值并不作为一个参数出现在存储过程中,总是Parameters集合中的第一个Parameters。