如何从 ASP 调用 SQL Server 存储过程

来源:http://support.microsoft.com/kb/q164485

有朋问,asp中如何调用存储过程。整理如下

概要
有关本文的 Microsoft Visual Basic .NET 版本,请参阅 306574 (http://support.microsoft.com/kb/306574/)。

下面的示例使用 Command 对象调用示例存储过程 sp_test。此存储过程接受整数,同时返回一个整数值:

<%@ LANGUAGE="VBSCRIPT" %>
   <!--#include virtual="/ASPSAMP/SAMPLES/ADOVBS.INC"-->
   <HTML>
   <HEAD><TITLE>Place Document Title Here</TITLE></HEAD>
   <BODY>
   This first method queries the data source about the parameters
   of the stored procedure. This is the least efficient method of calling
   a stored procedure.<BR>
   <%
   Set cn = Server.CreateObject("ADODB.Connection")
   Set cmd = Server.CreateObject("ADODB.Command")
   cn.Open "data source name", "userid", "password"
   Set cmd.ActiveConnection = cn
   cmd.CommandText = "sp_test"
   cmd.CommandType = adCmdStoredProc
   ' Ask the server about the parameters for the stored proc
   cmd.Parameters.Refresh
   ' Assign a value to the 2nd parameter.
   ' Index of 0 represents first parameter.
   cmd.Parameters(1) = 11
   cmd.Execute
   %>
   Calling via method 1<BR>
   ReturnValue = <% Response.Write cmd.Parameters(0) %><P>

   <!-- ************************************************************ -->

   Method 2 declares the stored procedure, and then explicitly declares
   the parameters.<BR>
   <%
   Set cn = Server.CreateObject("ADODB.Connection")
   cn.Open "data source name", "userid", "password"
   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = cn
   cmd.CommandText = "sp_test"
   cmd.CommandType = adCmdStoredProc
   cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _
      adParamReturnValue)
   cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _
      adParamInput)
   ' Set value of Param1 of the default collection to 22
   cmd("Param1") = 22
   cmd.Execute
   %>
   Calling via method 2<BR>
   ReturnValue = <% Response.Write cmd(0) %><P>

   <!-- ************************************************************ -->

   Method 3 is probably the most formal way of calling a stored procedure.
   It uses the canocial
   <%
   Set cn = Server.CreateObject("ADODB.Connection")
   cn.Open "data source name", "userid", "password"
   Set cmd = Server.CreateObject("ADODB.Command")
   Set cmd.ActiveConnection = cn
   ' Define the stored procedure's inputs and outputs
   ' Question marks act as placeholders for each parameter for the
   ' stored procedure
   cmd.CommandText = "{?=call sp_test(?)}"
   ' specify parameter info 1 by 1 in the order of the question marks
   ' specified when we defined the stored procedure
   cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _
   adParamReturnValue)
   cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _
     adParamInput)
   cmd.Parameters("Param1") = 33
   cmd.Execute
   %>
   Calling via method 3<BR>
   ReturnValue = <% Response.Write cmd("RetVal") %><P>
   </BODY>
   </HTML>
请注意,上面的示例使用了访问 Command 对象的 Parameters 集合的各种方法。有些方法使用 Command 对象的默认集合,而其他方法指定了要访问的特定集合的属性。

 

参考

<script type="text/javascript">loadTOCNode(1, 'references');</script>
有关 Visual InterDev 和 Active Server Pages 的最新知识库文章及其他支持信息,请参见 Microsoft 技术支持网站上的以下网页:
http://support.microsoft.com/search/default.aspx?qu=vinterdev (http://support.microsoft.com/search/default.aspx?qu=vinterdev)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值