如何使用参数运行存储过程,并将结果存储为经典 asp 中的记录集



我找不到完全涵盖这个问题的问题/答案,所以我为什么要问。我需要做的是运行一个采用 1 个参数的存储过程。它将返回一组我需要存储在记录集中的结果。我计划稍后循环访问此记录集。当涉及到较旧的asp时,我非常缺乏经验,但这是我必须做的:

dim myConn
Set myConn = Server.CreateObject("ADODB.Connection")
myConn.Open = ("DSN=example-dsn;SERVER=example-server;DATABASE=example-db;UID=user;PWD=pass;")
dim oStoredProc : Set oStoredProc = Server.CreateObject("ADODB.Command")
With oStoredProc
    .ActiveConnection = myConn
    .CommandType = adCmdStoredProc
    .CommandText = "myStoredProcedure"
    .Parameters.Append(.CreateParameter("@PARAM1", ADODB.adInteger, ADODB.adParamInput, 10, 2012))
    Dim rs : Set rs = .Execute() 
End With
// Will loop through it here.

我的猜测是我没有正确设置记录集,但就像我说的,我不太确定。如果有人能指出我正确的方向,我将不胜感激!

您需要确保结果集是正确的对象

set rs = Server.CreateObject("ADODB.Recordset")

然后您将使用我认为它的工作原理如下的开放方法:

   rs.Open oStoredProc

然后使用记录集对象的其他成员循环访问结果。

好吧,我做错了一些事情,但这是最终对我有用的东西。首先,事实证明我不需要传入参数,但无论如何这不是问题。"adCmdStoredProc"没有被识别的主要问题之一,这很奇怪,因为我看到它在其他地方使用,但用它的相应值 4 替换它确实有效。

dim myConn, cmd
Set myConn = Server.CreateObject("ADODB.Connection")
myConn.Open = ("DSN=[BLAH];SERVER=[SERVER];DATABASE=[BLAH];UID=[User];PWD=[Pass];")
dim oStoredProc : Set oStoredProc = Server.CreateObject("ADODB.Command")
oStoredProc.CommandType = 4 
oStoredProc.CommandText = "StoredProcedureName"
oStoredProc.ActiveConnection = myConn
// Add parameters here if needed.
Dim rs 
Set rs = oStoredProc.Execute()
// I Loop through here
rs.Close
myConn.Close
Set rs = Nothing
Set oStoredProc = Nothing
Set myConn = Nothing

如果其他人需要它,我希望这会有所帮助。

Dim rsStk As New ADODB.Recordset
Set rsStk = cnnPck.Execute("SP_JOB_ALL '" & Trim(te_Item) & "'")
Set Recordset= CONNECTION .Execute()

这是做这件事的简单方法

最新更新