从经典 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
行产生Itemcannot befind in the collection returned by the requested name或序数。
错误。
将其替换为 Response.Write Count.Status 时,我得到了 对象关闭时不允许操作。 错误。
添加 Count.Open 会导致该连接无法用于执行此操作。它在此上下文中已关闭或无效。 错误。
在 Mark B 的回答后编辑:
我已经查看了断开连接的记录集,但我不知道如何在我的示例中使用它们:每个教程都使用 Recordset.Open< 将查询直接输入到记录集中/code>,但我正在使用参数化查询,甚至尝试了很多方法,当有
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
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
The Response.Write Count.Fields(0).Value
line yields the Item cannot be found in the collection corresponding to the requested name or ordinal.
error.
Replacing it with Response.Write Count.Status
I get the Operation is not allowed when the object is closed.
error.
Adding Count.Open
gives the The connection cannot be used to perform this operation. It is either closed or invalid in this context.
error.
Edit after Mark B's answer:
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.
What should I do?
Thanks in advance.
Here's the solution based on Eduardo Molteni's answer:
The function which interacts with the database:
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
The code which calls the 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个返回断开连接的记录集的函数
Here's a function that returns a disconnected recordset
好吧,您在设置函数的返回变量后立即关闭记录集和连接,这解释了错误消息。
我不是 VB 开发人员,但我认为您需要查看断开连接的记录集。看看这篇文章< /a>,它所做的几乎正是您想要的。
Well, you are closing the recordset and connection immediately after setting the function's return variable, so that explains the error messages.
I am not a VB developer, but I think what you need to look at is Disconnected Recordsets. Take a look at this article, it's doing pretty much exactly what you want.