【问题标题】:How to run a stored procedure with param and store result as a record set in classic asp如何使用参数运行存储过程并将结果存储为经典asp中的记录集
【发布时间】:2023-03-18 06:37:02
【问题描述】:

我无法找到完全涵盖此问题的问题/答案,因此我要问这个问题。我需要做的是运行一个带有 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.

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

【问题讨论】:

标签: asp-classic parameters recordset procedure


【解决方案1】:
Dim rsStk As New ADODB.Recordset

Set rsStk = cnnPck.Execute("SP_JOB_ALL '" & Trim(te_Item) & "'")

Set Recordset= CONNECTION .Execute()

这是做这件事的简单方法

【讨论】:

    【解决方案2】:

    好吧,我做错了一些事情,但最终对我有用。首先,事实证明我不需要传入参数,但这无论如何都不是问题。主要问题之一是“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
    

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

    【讨论】:

      【解决方案3】:

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

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

      然后你将使用open method 我认为它的工作原理是这样的:

         rs.Open oStoredProc
      

      然后使用Record Set object 的其他成员循环遍历结果。

      【讨论】:

      • 这与从 Command 的 execute 方法中获取记录集有何不同?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多