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 %> 如果有不止一個輸出參數,可用相同的方法訪問。可以使用參數名或索引號取出集合中的值。