Set recTypes = Server.CreateObject("ADODB.Recordset")
recTypes.Open "usp_BookTypes", strConn
While Not recTypes.EOF Response.Write "<OPTION VALUE=" & sDQ & recTypes("type") & sDQ & _ ">" & recTypes("type") recTypes.MoveNext Wend
recTypes.Close Set recTypes = Nothing
%> 显示书的类型后,接着可以构建窗体的其他部分,包括一个允许用户输入书价变化百分数的文本框。 </SELECT> </TD> </TR> <TR> <TD>Percent Value</TD> <TD><INPUT NAME="txtPercent" TYPE="TEXT"></TD> </TR> </TABLE> <P> <INPUT TYPE="submit" VALUE="Run Query"> </FORM> 现在看一下Run Query按钮调用的ASP文件StoredProcedure.asp。首先,声明变量并从调用窗体取出书的类型和百分数。 Dim cmdUpdate Dim lRecs Dim sType Dim cPercent
' Get the form values sType = Request.Form("lstTypes") cPercent = Request.Form("txtPercent") 现在可以向用户显示一些确认信息,告诉他们将发生什么。 ' Tell the user what's being done Response.Write "Updating all books" If sType <> "all" Then Response.Write " of type <B>" & sType & "</B>" End If Response.Write " by " & cPercent & "%<P>" 现在重新回到代码内部,在此创建Command对象和参数。 Set cmdUpdate = Server.CreateObject("ADODB.Command")
With cmdUpdate .ActiveConnection = strConn .CommandText = "usp_UpdatePrices" .CommandType = adCmdStoredProc 利用从前面网页的窗体中提取的数据值,使用快捷方法创建和增加参数。 ' Add the parameters .Parameters.Append .CreateParameter ("@Type", adVarWChar, adParamInput, _ 12, sType) .Parameters.Append .CreateParameter ("@Percent", adCurrency, _ adParamInput, , cPercent) 现在,运行存储过程。 ' Execute the command .Execute lRecs, , adExecuteNoRecords End With 为了确认,可以告诉用户已经更新多少条记录。 ' And finally tell the user what's happened Response.Write "Procedure complete. " & lRecs & " were updated."
Set cmdUpdate = Nothing %> 这样就有了两个简单界面。前者创建了一个供选择的项目列表,后者使用其中某个项目值更新数据。这是许多需要显示和更新数据的ASP页面的基础。 3. 传递数组参数 Parameters参数集合一般来说比较好用,但有时稍有麻烦(尤其对于新手)。好在有一种快捷方法,使用Execute方法的Parameters参数。例如,调用存储过程usp_UpdatePrices,但不使用Parameters集合。 创建一个Command对象,并同前面一样设置其属性。 ' Set cmdUpdate = Server.CreateObject("ADODB.Command")
' Set the properties of the command With cmdUpdate .ActiveConnection = strConn .commandText = "usp_UpdatePrices" .commandType = adCmdStroreProc 但这里正是差异所在。我们仅是通过Execute方法传递参数给存储过程,而不是创建参数并添加到集合中。 ' Execute the command .Execute lngRecs, Array(strType, curPercent), adExecuteNoRecords End With 这里使用了Array函数,将单个变量转换为数组,以适于方法调用。这种方法当然也有缺点: · 只能使用输入参数。因为不能指定参数的类型和传递方向,而缺省为输入参数。 · 如果要多次调用存储过程,这种方法速度就比较慢,因为ADO将向数据存储询问参数的内容及数据类型。 集合方法和数组方法之间在速度上的差异非常之小,几乎可以忽略。所以,如果只有输入参数,可随便使用哪一种。实际上,人们更喜欢使用Parameters集合的方法,尽管它稍为繁琐,但是使参数的属性更加明确。 4. 输出参数 我们已经知道如何获得受命令影响的记录数,如果需要更多信息,却又不想返回一个记录集,怎么办?也许想从存储过程中返回两个或三个值,但又不想费心创建一个记录集。在这时,可以定义一个输出参数,其值由存储过程提供。 例如,对于更新书价的程序,如果想在更新之后找出最高价格,可将存储过程改成: CREATE PROCEDURE usp_UpdatePricesMax @Type Char(12), @Percent Money, @Max Money OUTPUT AS BEGIN UPDATE Titles SET Price = Price * (1 + @Percent / 100) WHERE Type = @Type
SELECT @Max = MAX(Price) FROM Titles END 这只是在执行更新后运行了一个简单的SELECT语句,并将值赋给输出参数。 现在可以改写StroreProcedure.asp的代码从而获取变量@MAX的值。 <% Dim cmdUpdate Dim lngRecs Dim strType Dim curPercent Dim curMax
' Get the form values strType = Request.Form("lstTypes") curPercent = Request.Form("txtPercent")
' Tell the user what's being done Response.Write "Updating all books" & " of type <B>" & strType & "</B>" & _ " by " & curPercent & "%<P>"
Set cmdUpdate = Server.CreateObject("ADODB.Command")
' Set the properties of the command With cmdUpdate .ActiveConnection = strConn .CommandText = "usp_UpdatePricesMax" .CommandType = adCmdStoredProc 我们只是在集合中加入了另一个参数,但这次指定为输出参数。注意它并没有赋值,因为其值将由存储过程提供,记住这是一个输出参数。 ' Add the parameters .Parameters.Append .CreateParameter("@Type", adVarWChar, adParamInput, _ 12, strType) .Parameters.Append .CreateParameter("@Percent", adCurrency, _ adParamInput, , curPercent) .Parameters.Append.CreateParameter("@Max", adCurrency, adParamOutput)
' Execute the command .Execute lngRecs, , adExecuteNoRecords 一旦执行这个过程,就可从集合中取得该值。 ' Extract the output parameter, which the stored ' procedure has supplied to the parameters collection curMax = .Parameters("@Max") End With
' And finally tell the user what's happened Response.Write "Procedure complete. " & lngRecs & _ " records were updated.<P>" Response.Write "The highest price book is now " & _ FormatCurrency(curMax)
Set cmdUpdate = Nothing %> 如果有不止一个输出参数,可用相同的方法访问。可以使用参数名或索引号取出集合中的值。