2010-10-01 80 views
5

我对如何从经典ASP中的函数返回可读记录集感到迷茫。从经典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.错误。

编辑马克·B的应答后:

我已经看了断开连接的记录,但我不知道怎么在我的例子中使用它们:每个教程喂查询直接与Recordset.Open记录,但我使用参数化查询,甚至尝试很多方法,我无法获得相同的结果,当有一个ADODB.Command的方式。

我该怎么办?

在此先感谢。


下面是根据爱德华的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 
+0

你的代码最终块丢失'Response.CodePage = 65001'功能。 – AnthonyWJones 2010-10-01 15:28:59

回答

4

下面是返回一个断开连接的记录

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 
+0

您的代码完美无缺,谢谢。 – Albireo 2010-10-01 12:49:16

+0

我知道这是一个旧帖子,但我怎样才能在JScript中实现相同?我无法通过Connection的空参数执行“rs.Open cmd,adOpenForwardOnly,adLockReadOnly”。我收到错误“无法更改具有Command对象作为其源的Recordset对象的ActiveConnection属性。” – jpmorin 2013-04-12 20:45:43

+0

@jpmorin:对不起,但我不知道JScript。为什么不只是保持这个功能是VBScript?我相信你可以有混合脚本... – 2013-04-15 14:22:19

0

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

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

+0

嗨,我已经看过断开连接的记录集 - 我应该提到它 - 但我不知道如何在我的例子中使用它们:每个教程使用'Recordset.Open'直接将查询输入到记录集中,但是我使用参数化查询,甚至尝试很多方法,当有ADODB.Command方法时,我无法获得相同的结果。 – Albireo 2010-10-01 09:52:17

+0

我认为你需要编辑你的问题并包含所有的信息,那么比我更多的VB知识的人可能会有所帮助。 – 2010-10-01 10:57:32