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

ASP
IIS访问ASP页面时报错The requested resource is in use.的解决办法
错误类型:Provider (0x80004005)未指定的错误 的一个处理方法
关于asp+access的安全问题分析
把RS.GetRows看得更清楚
asp下UTF-8页面乱码的解决方法
解决使用良精企业建站7.0未注册问题
在ASP中连接MySQL数据库的方法,最好的通过ODBC方法
flash与js通讯方法
ASP操作Excel相关技术总结
解决用Access数据库建站维护不便的问题的方法
ASP实现GB2312字符与区位码的相互转换的代码
对于ASP编码问题的深入研究与最终解决方案
ASP生成UTF-8编码的代码
删除A表中在B表中不存在的数据
动网防恶意广告比较有效的办法附asp代码
asp磁盘缓存技术使用的代码
网页语言编码及asp乱码问题解决方案
FSO遍历目录实现全站插马的代码
关于无限分级(ASP+数据库+JS)的实现代码
如何写ASP入库小偷程序

ASP 3.0高级编程(四十二)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 176 ::
收藏到网摘: 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。