文章編號: 300488 - 上次校閱: 2006年3月2日 - 版次: 5.3

如何從 ASP 網頁執行 SQL Server 預存程序

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何從動態伺服器網頁 (ASP) 執行 SQL Server 預存程序並使用參數。

本文假設您已熟悉在 ASP 網頁使用 ActiveX Data Objects (ADO) 的程序。

其他相關資訊

逐步範例

  1. 針對 Pubs 資料庫在 SQL Server Query Analyzer 或 SQL Server Management Studio 中執行下列查詢:
    CREATE proc MyProc
    (
    	@price smallint,
    	@out smallint OUTPUT
    )
    AS
    Select @out = count(*) from titles where price < @price
    GO
    						
    預存程序 ("MyProc") 需要一個輸入參數 ("@price"),且會傳回一個輸出參數 ("@out")。

    注意 根據預設,Northwind 範例資料庫和 pubs 範例資料庫不會安裝在 SQL Server 2005 中。這些資料庫可從「Microsoft 下載中心」下載。如需有關如何下載 Northwind 範例資料庫和 pubs 範例資料庫的詳細資訊,請造訪下列 Microsoft 網站:
    http://msdn2.microsoft.com/en-us/library/ms143221.aspx (http://msdn2.microsoft.com/en-us/library/ms143221.aspx)
  2. 下列 ASP 範例程式碼會呼叫新建的預存程序。您可使用此 ASP 程式碼,來設定輸入參數和執行查詢。
    1. 使用 CreateParameter 方法,依照下列方式在 Microsoft ActiveX Data Objects (ADO) 中建立參數:
      Set myParameter = Command.CreateParameter (Name, [Type], [Direction], [Size], [Value])
      						
    2. 依照下列方式將參數附加至 Parameters 集合:
      Command.Parameters.Append myParameter
      							
      注意Parameters 集合中的參數必須符合預存程序中的參數順序。
    3. 依照下列方式,執行命令以在預存程序內外傳送參數值:
      <%
      Dim cmd
      Dim ln
      Dim retCount
      
      Set cmd = Server.CreateObject("ADODB.Command")
      	
      With cmd
          .ActiveConnection = "Paste your connection string here"
          .Commandtext = "MyProc"
          .CommandType = adCmdStoredProc
          .Parameters.Append .CreateParameter("@price", adSmallInt, adParamInput, 10)
          .Parameters("@price") = 22
          .Parameters.Append .CreateParameter("@retValue", adSmallInt, adParamOutput, 10)
          .Execute ln, , adExecuteNoRecords
          retCount = .Parameters("@retValue")
      End with
      
      Response.Write retcount
      
      Set cmd = Nothing
      %>
      						
    注意: 此範例中使用的常數可在 Adovbs.inc 檔案中找到。此檔案是在安裝動態伺服器網頁 (ASP) 時安裝的,並且置於 \Aspsamp\Samples 資料夾,通常位於您的 \Inetpub 資料夾中。建議的程式設計作法,是當您呼叫預存程序時使用常數而不使用數值,如此您的程式碼會較容易讀取和維護。
    <%@ LANGUAGE = VBScript %>
    <!-- #INCLUDE VIRTUAL="/ASPSAMP/SAMPLES/ADOVBS.INC" -->
    					
  3. 根據您的環境,修改 ADO 連線字串。
  4. 儲存 ASP 網頁,並在瀏覽器中檢視。

    注意: 如果您不瞭解預存程序的參數資訊,要判斷如何適當呼叫預存程序是有困難的。沒有正確的資訊,您就不能正確建立 ADO 參數。您可使用 Parameter 物件的 Refresh 方法,以伺服器上預存程序的定義為基礎,自動填入 Parameters 集合。例如:
    Command.Parameters.Refresh
    					

?考

如需詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
306574? (http://support.microsoft.com/kb/306574/ ) How to call SQL Server stored procedures in ASP.NET by using Visual Basic .NET
如需有關 Refresh 方法的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
165156? (http://support.microsoft.com/kb/165156/ ) How To Determine Parameter Requirements for a Stored Procedure in ASP
如需有關如何使用資料連結檔案 (.udl) 以建立連線字串的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
189680? (http://support.microsoft.com/kb/189680/ ) HOWTO:使用 ADO 的資料連結檔案
如需有關從 Oracle 擷取資料錄集的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
229919? (http://support.microsoft.com/kb/229919/ ) How To Retrieve a Recordset from an Oracle Stored Procedure Using ADO on ASP

這篇文章中的資訊適用於:
  • Microsoft Active Server Pages 4.0
  • Microsoft SQL Server 2000 Standard Edition
  • Microsoft SQL Server 7.0 Standard Edition
  • Microsoft SQL Server 2005 Developer Edition
  • Microsoft SQL Server 2005 Enterprise Edition
  • Microsoft SQL Server 2005 Standard Edition
  • Microsoft SQL Server 2005 Workgroup Edition
關鍵字:?
kbfaq kbhowto KB300488
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。