Article ID: 200190
Article Last Modified on 6/29/2004
SELECT * FROM Products WHERE ProductID = [@productid]The following sample code will call the Access Query called SampleQuery using the ADO Command object and the Parameters collection.
<%@ Language=VBScript %>
<HTML>
<BODY>
<!--#include file=adovbs.inc -->
<%
Set objConn = Server.CreateObject("ADODB.Connection")
Set objCmd = Server.CreateObject("ADODB.Command")
Set objRS = Server.CreateObject("ADODB.Recordset")
objConn.Open "dsn=northwind;"
objRS.CursorType = adOpenForwardOnly
objRS.LockType = adLockOptimistic
Set objCmd.ActiveConnection = objConn
'If a SQL statement with question marks is specified, then the
'CommandType is adCmdText. If a query name is specified, then
'the CommandType is adCmdStoredProc.
objCmd.CommandText = "SampleQuery"
objCmd.CommandType = adCmdStoredProc
'Create the parameter and populate it.
Set objParam = objCmd.CreateParameter("@productid" , adInteger, adParamInput, 0, 0)
objCmd.Parameters.Append objParam
objCmd.Parameters("@productid") = 15 'Return the product with ProductID = 15
'Open and display the Recordset.
objRS.Open objCmd
%>
<table border=1 cellpadding=2 cellspacing=2>
<tr>
<%
For I = 0 To objRS.Fields.Count - 1
Response.Write "<td><b>" & objRS(I).Name & "</b></td>"
Next
%>
</tr>
<%
Do While Not objRS.EOF
Response.Write "<tr>"
For I = 0 To objRS.Fields.Count - 1
Response.Write "<td>" & objRS(I) & "</td>"
Next
Response.Write "</tr>"
objRS.MoveNext
Loop
%>
</table>
<%
objRS.Close
objConn.Close
Set objRS = Nothing
Set objCmd = Nothing
Set objConn = Nothing
%>
</BODY>
</HTML>
To use a SQL statement with question marks as parameter placeholders, use the same sample code but update the CommandText and CommandType properties as in the following example:
objCmd.CommandText = "SELECT * FROM Products WHERE ProductID = ?" objCmd.CommandType = adCmdText
Keywords: kbhowto KB200190