【问题标题】:Return recordset from function in classic ASP从经典 ASP 中的函数返回记录集
【发布时间】:2011-04-19 18:28:28
【问题描述】:

我不知道如何从经典 ASP 中的函数返回可读记录集。

这是我想出的,但它不起作用:

Response.Clear
Response.CharSet = "utf-8"
Response.ContentType = "text/plain"

Dim Count

Set Count = Test

Response.Write Count.Fields(0).Value


Function Test

    Dim Query, Connection, Command, Recordset

    Query = " blah blah blah "

    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Command = Server.CreateObject("ADODB.Command")
    Set Recordset = Server.CreateObject("ADODB.Recordset")

    Connection.ConnectionString = "blah blah blah"
    Connection.Open

    Set Command.ActiveConnection = Connection
    Command.CommandText = Query

    Set Recordset = Command.Execute

    Set Test = Recordset

    Recordset.Close
    Connection.Close

    Set Recordset = Nothing
    Set Command = Nothing
    Set Connection = Nothing

End Function

Response.Write Count.Fields(0).Value 行产生Item cannot be found in the collection corresponding to the requested name or ordinal. 错误。

Response.Write Count.Status 替换它我得到Operation is not allowed when the object is closed. 错误。

添加Count.Open 会产生The connection cannot be used to perform this operation. It is either closed or invalid in this context. 错误。

在 Mark B 回答后编辑:

我已经查看了断开连接的记录集,但我不知道如何在我的示例中使用它们:每个教程都使用 Recordset.Open 将查询直接输入到记录集中,但我使用的是参数化查询,甚至尝试了很多方法当有ADODB.Command 时,我无法获得相同的结果。

我该怎么办?

提前致谢。


这是基于 Eduardo Molteni 回答的解决方案:

与数据库交互的函数:

Function Test

    Dim Connection, Command, Recordset

    Set Connection = Server.CreateObject("ADODB.Connection")
    Set Command = Server.CreateObject("ADODB.Command")
    Set Recordset = Server.CreateObject("ADODB.Recordset")

    Connection.ConnectionString = "blah blah blah"
    Connection.Open

    Command.ActiveConnection = Connection
    Command.CommandText = "blah blah blah"

    Recordset.CursorLocation = adUseClient
    Recordset.Open Command, , adOpenForwardOnly, adLockReadOnly

    Set Recordset.ActiveConnection = Nothing

    Set Test = Recordset

    Connection.Close

    Set Recordset = Nothing
    Set Command = Nothing
    Set Connection = Nothing

End Function

调用函数的代码:

Response.Clear
Response.CharSet = "utf-8"
Response.ContentType = "text/plain"

Dim Recordset

Set Recordset = Test

Response.Write Recordset.Fields(0).Value

Recordset.Close

Set Recordset = Nothing

【问题讨论】:

  • 您的最后一段代码丢失了Response.CodePage = 65001

标签: function asp-classic adodb recordset


【解决方案1】:

这是一个返回断开连接的记录集的函数

Function RunSQLReturnRS(sqlstmt, params())
    On Error Resume next

    ''//Create the ADO objects
    Dim rs , cmd
    Set rs = server.createobject("ADODB.Recordset")
    Set cmd = server.createobject("ADODB.Command")

    ''//Init the ADO objects  & the stored proc parameters
    cmd.ActiveConnection = GetConnectionString()
    cmd.CommandText = sqlstmt
    cmd.CommandType = adCmdText
    cmd.CommandTimeout = 900 

    ''// propietary function that put params in the cmd
    collectParams cmd, params

    ''//Execute the query for readonly
    rs.CursorLocation = adUseClient
    rs.Open cmd, , adOpenForwardOnly, adLockReadOnly
    If err.number > 0 then
        BuildErrorMessage()
        exit function
    end if

    ''// Disconnect the recordset
    Set cmd.ActiveConnection = Nothing
    Set cmd = Nothing
    Set rs.ActiveConnection = Nothing

    ''// Return the resultant recordset
    Set RunSQLReturnRS = rs

End Function

【讨论】:

  • 我知道这是一篇旧文章,但如何在 JScript 中实现相同的效果?我不能对 Connection 使用空参数执行“rs.Open cmd, , adOpenForwardOnly, adLockReadOnly”。我收到错误消息“无法更改以 Command 对象为源的 Recordset 对象的 ActiveConnection 属性。”
  • @jpmorin:抱歉,我不知道 JScript。你为什么不把这个函数保留为VBScript?我相信你可以有混合脚本......
  • @EduardoMolteni:我已经能够手动设置记录集对象中的参数并仅使用命令参数调用 open 方法。我还必须写 SET NOCOUNT ON;在我的存储过程中使其工作。
【解决方案2】:

好吧,您在设置函数的返回变量后立即关闭记录集和连接,这样就可以解释错误消息。

我不是 VB 开发人员,但我认为您需要查看的是断开连接的记录集。看看this article,它几乎完全符合您的要求。

【讨论】:

  • 嗨,我已经看过断开连接的记录集 - 我应该提到它 - 但我不知道如何在我的示例中使用它们:每个教程都使用 Recordset.Open 将查询直接馈送到记录集中,但我正在使用参数化查询,甚至尝试了很多方法,但当有一个 ADODB.Command 时,我无法获得相同的结果。
  • 我认为您需要编辑您的问题并包含所有这些信息,然后比我有更多 VB 知识的人可能会提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多