Function 从经典ASP中的函数返回记录集

Function 从经典ASP中的函数返回记录集,function,asp-classic,adodb,recordset,Function,Asp Classic,Adodb,Recordset,我不知道如何从classic 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, Records

我不知道如何从classic 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行生成的
项在与请求的名称或序号对应的集合中找不到。
错误

将其替换为
Response.Write Count.Status
对象关闭时不允许执行
操作。
错误

添加
Count.Open
会导致
连接无法用于执行此操作。在此上下文中,它已关闭或无效。
错误

在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

在设置函数的返回变量后,您将立即关闭记录集和连接,这就解释了错误消息


我不是一个VB开发人员,但我认为您需要了解的是断开连接的记录集。看一看,它做的几乎正是您想要的。

这里有一个函数,它返回一个断开连接的记录集

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

您好,我已经看过了断开连接的记录集-我应该提到它-但我不知道如何在我的示例中使用它们:每个教程都使用
记录集将查询直接输入到记录集中。打开
,但我使用的是参数化查询,即使尝试了很多方法,当有
ADODB.Command
的时候,我也无法获得相同的结果。我想你需要编辑你的问题并包含所有的信息,那么,比我有更多VB知识的人可能会提供帮助。您的最后一段代码缺少
Response.CodePage=65001
。我知道这是一篇老文章,但如何在JScript中实现同样的效果?我无法使用连接的空参数执行“rs.Open cmd、adOpenForwardOnly、adlockdreadonly”。我收到错误“无法更改源为命令对象的记录集对象的ActiveConnection属性”。@jpmorin:抱歉,我不知道JScript。为什么不保留这个函数是VBScript?我相信您可以使用混合脚本…@EduardoMolteni:我已经能够手动设置recordset对象中的参数,并仅使用命令参数调用open方法。我还得写上“不计数”;在我的存储过程中使其工作。