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

时间:2010-10-01 09:23:07

标签: function asp-classic adodb recordset

我对如何从经典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

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:0)

嗯,您在设置函数的返回变量后立即关闭记录集和连接,以便解释错误消息。

我不是VB开发人员,但我认为您需要关注的是Disconnected Recordsets。看看this article,它正在做你想要的。