ASP 3.0高級編程(四十二)_ASP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:用ASP實(shí)現(xiàn)網(wǎng)上考試系統(tǒng)隨著互連網(wǎng)技術(shù)的發(fā)展網(wǎng)上教學(xué)將成為人們接受再教育和終身教育的主要形式。在網(wǎng)上學(xué)校中,人們可以不受時間和空間的限制,隨時隨地選學(xué)任何地方的任何課程。網(wǎng)上學(xué)校的發(fā)展對網(wǎng)上考試的發(fā)展提出
5. 返回值對函數(shù)返回值的處理不同于存儲過程返回值的處理,這常常導(dǎo)致混淆。在函數(shù)中,經(jīng)常是返回一個布爾值來表明函數(shù)運(yùn)行的成功與否。
If SomeFunctionName() = True Then
' Function succeeded
但在調(diào)用一個存儲過程時,卻不能使用同樣的方法,因?yàn)榇鎯κ怯肊xecute方法運(yùn)行的,同時返回一個記錄集。
Set rsAuthors = cmdAuthors.Execute
如果得不到一個返回值,如何確定是否已正確執(zhí)行存儲過程?當(dāng)發(fā)生錯誤時,會報(bào)告錯誤,這樣就可使用前一章提供的錯誤處理代碼來處理錯誤。但對于一些非致命的邏輯錯誤怎么辦?
例如,考慮向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
該過程首先檢查是否有同名的職員存在,并據(jù)此設(shè)定相應(yīng)的變量Exists,若存在同名,就設(shè)為1,否則為0。然后將該職員加到表中,同時把Exists的值作為返回值返回。
注意盡管返回了一個值,但并未將其聲明為存儲過程的參數(shù)。
調(diào)用該過程的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
%>
需要重點(diǎn)注意,返回值應(yīng)當(dāng)作為集合中第一個參數(shù)被創(chuàng)建。即使返回值并不作為一個參數(shù)出現(xiàn)在存儲過程中,總是Parameters集合中的第一個Parameters。
分享:ASP 3.0高級編程(三十六)第8章 ADO基礎(chǔ) 在本書前7章中,已經(jīng)講述了ASP的有關(guān)內(nèi)容,以及ASP如何為Web站點(diǎn)帶來動態(tài)的內(nèi)容。已經(jīng)見到其腳本程序允許自定義Web頁面,使我們能夠構(gòu)建功能更為強(qiáng)大的ASP頁面。 現(xiàn)在,將研究ASP
相關(guān)ASP教程:
- asp FSO 讀寫文件本文件實(shí)現(xiàn)代碼
- asp中isNull、isEmpty和空字符串的區(qū)別
- asp獲取用戶真實(shí)IP地址的方法
- asp連接sqlserver數(shù)據(jù)庫實(shí)現(xiàn)代碼
- asp中正則表達(dá)式過濾html代碼函數(shù)
- asp中g(shù)et post提交表單區(qū)別
- 網(wǎng)頁模板:ASP內(nèi)建對象Request
- xmlhttp的open方法使用詳解
- ASP的常用的自定義函數(shù)大全
- asp中用for循環(huán)的一個小技巧
- eWebEditor v3.8 列目錄
- ASP無組件分頁實(shí)現(xiàn)思路及代碼
- 相關(guān)鏈接:
- 教程說明:
ASP教程-ASP 3.0高級編程(四十二)
。