从经典 ASP 中的函数返回记录集 [英] Return recordset from function in classic ASP

查看:19
本文介绍了从经典 ASP 中的函数返回记录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm at a loss on how I can return a readable recordset from a function in classic ASP.

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

This is what I came up with, but it's not working:

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 在与请求的名称或序号相对应的集合中. 错误.

将其替换为 Response.Write Count.Status 我得到 Operation is not allowed when object is closed. 错误.

Replacing it with Response.Write Count.Status I get the Operation is not allowed when the object is closed. error.

添加 Count.Open 使 连接不能用于执行此操作.它在此上下文中已关闭或无效. 错误.

在 Mark B 回答后

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

I already looked at disconnected recordsets but I don't know how to use them in my example: every tutorial feeds the query directly into the recordset with Recordset.Open, but I'm using parametrized queries, and even trying many ways I couldn't obtain the same result when there's an ADODB.Command in the way.

我该怎么办?

提前致谢.


这是基于 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

推荐答案

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

Here's a function that returns a disconnected recordset

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

这篇关于从经典 ASP 中的函数返回记录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆