asp(Parameters) 参数化实现方法

本文演示了使用 ActiveX 数据对象 (ADO) 从 Active Server Pages 调用存储过程的三种方法

下面的示例使用 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 对象的默认集合,而其他方法指定了要访问的特定集合的属性。
// 以上来源于https://support.microsoft.com/zh-cn/kb/164485#rtDisclaimer

Creating and Executing a Simple Command

https://msdn.microsoft.com/zh-cn/ms675065
Using a Command object
Using a Connection object
Using a Recordset object
https://msdn.microsoft.com/zh-cn/ms675101

MSDN ADO API

https://msdn.microsoft.com/zh-cn/kb/ms678086(v=vs.71).aspx


关于Parameters.Append(ado中任何一个集合都有Append方法)

以下来源:https://msdn.microsoft.com/zh-cn/kb/ms681564(v=vs.71)
Parameters Collection
You must set the Type property of a Parameter object before appending it to the Parameters collection. If you select a variable-length data type, you must also set the Size property to a value greater than zero.

Describing parameters yourself minimizes calls to the provider and therefore improves performance when you use stored procedures or parameterized queries. However, you must know the properties of the parameters associated with the stored procedure or parameterized query that you want to call.

Use the CreateParameter method to create Parameter objects with the appropriate property settings and use the Append method to add them to the Parameters collection. This lets you set and return parameter values without having to call the provider for the parameter information. If you are writing to a provider that does not supply parameter information, you must use this method to manually populate the Parameters collection in order to use parameters at all.

Fields Collection
The FieldValue parameter is only valid when adding a Field object to a Record object, not to a Recordset object. With a Record object, you can append fields and provide values at the same time. With a Recordset object, you must create fields while the Recordset is closed, and then open the Recordset and assign values to the fields.

NoteNote
For new Field objects that have been appended to the Fields collection of a Record object, the Value property must be set before any other Field properties can be specified. First, a specific value for the Value property must have been assigned and Update on the Fields collection called. Then, other properties such as Type or Attributes can be accessed. Field objects of the following data types (DataTypeEnum) cannot be appended to the Fields collection and will cause an error to occur: adArray, adChapter, adEmpty, adPropVariant, and adUserDefined. Also, the following data types are not supported by ADO: adIDispatch, adIUnknown, and adIVariant. For these types, no error will occur when appended, but usage can produce unpredictable results including memory leaks.

Recordset
If you do not set the CursorLocation property before calling the Append method, CursorLocation will be set to adUseClient (a CursorLocationEnum value) automatically when the Open method of the Recordset object is called.

A run-time error will occur if the Append method is called on the Fields collection of an open Recordset, or on a Recordset where the ActiveConnection property has been set. You can only append fields to a Recordset that is not open and has not yet been connected to a data source. This is typically the case when a Recordset object is fabricated with the CreateRecordset method or assigned to an object variable.

Record
A run-time error will not occur if the Append method is called on the Fields collection of an open Record. The new field will be added to the Fields collection of the Record object. If the Record was derived from a Recordset, the new field will not appear in the Fields collection of the Recordset object.

A non-existent field can be created and appended to the Fields collection by assigning a value to the field object as if it already existed in the collection. The assignment will trigger the automatic creation and appending of the Field object, and then the assignment will be completed.

After appending a Field to the Fields collection of a Record object, call the Update method of the Fields collection to save the change.
适用于

Fields Collection (ADO)

Parameters Collection (ADO)

Parameters Collection (ADO)

A Command object has a Parameters collection made up of Parameter objects.

Using the Refresh method on a Command object's Parameters collection retrieves provider parameter information for the stored procedure or parameterized query specified in the Command object. Some providers do not support stored procedure calls or parameterized queries; calling the Refresh method on the Parameters collection when using such a provider will return an error.

If you have not defined your own Parameter objects and you access the Parameters collection before calling the Refresh method, ADO will automatically call the method and populate the collection for you.

You can minimize calls to the provider to improve performance if you know the properties of the parameters associated with the stored procedure or parameterized query you wish to call. Use the CreateParameter method to create Parameter objects with the appropriate property settings and use the Append method to add them to the Parameters collection. This lets you set and return parameter values without having to call the provider for the parameter information. If you are writing to a provider that does not supply parameter information, you must manually populate the Parameters collection using this method to be able to use parameters at all. Use the Delete method to remove Parameter objects from the Parameters collection if necessary.

The objects in the Parameters collection of a Recordset go out of scope (therefore becoming unavailable) when the Recordset is closed.

When calling a stored procedure with Command, the return value/output parameter of a stored procedure is retrieved as follows:

When calling a stored procedure that has no parameters, the Refresh method on the Parameters collection should be called before calling the Execute method on the Command object.

When calling a stored procedure with parameters and explicitly appending a parameter to the Parameters collection with Append, the return value/output parameter should be appended to the Parameters collection. The return value must first be appended to the Parameters collection. Use Append to add the other parameters into the Parameters collection in the order of definition. For example, the stored procedure SPWithParam has two parameters. The first parameter, InParam, is an input parameter defined as adVarChar (20), and the second parameter, OutParam, is an output parameter defined as adVarChar (20). You can retrieve the return value/output parameter with the following code.

' Open Connection Conn
set ccmd = CreateObject("ADODB.Command")
ccmd.Activeconnection= Conn

ccmd.CommandText="SPWithParam"
ccmd.commandType = 4 'adCmdStoredProc

ccmd.parameters.Append ccmd.CreateParameter(, adInteger, adParamReturnValue, , NULL)   ' return value
ccmd.parameters.Append ccmd.CreateParameter("InParam", adVarChar, adParamInput, 20, "hello world")   ' input parameter
ccmd.parameters.Append ccmd.CreateParameter("OutParam", adVarChar, adParamOuput, 20, NULL)   ' output parameter

ccmd.execute()

' Access ccmd.parameters(0) as return value of this stored procedure
' Access ccmd.parameters("OutParam") as the output parameter of this stored procedure.
When calling a stored procedure with parameters and configuring the parameters by calling the Item method on the Parameters collection, the return value/output parameter of the stored procedure can be retrieved from the Parameters collection. For example, the stored procedure SPWithParam has two parameters. The first parameter, InParam, is an input parameter defined as adVarChar (20), and the second parameter, OutParam, is an output parameter defined as adVarChar (20). You can retrieve the return value/output parameter with the following code.

' Open Connection Conn
set ccmd = CreateObject("ADODB.Command")
ccmd.Activeconnection= Conn

ccmd.CommandText="SPWithParam"
ccmd.commandType = 4 'adCmdStoredProc

ccmd.parameters.Item("InParam").value = "hello world" ' input parameter
ccmd.execute()

' Access ccmd.parameters(0) as return value of stored procedure
' Access ccmd.parameters(2) or ccmd.parameters("OutParam") as the output parameter.


猜你喜欢

转载自blog.csdn.net/ozhangsangong/article/details/46398581