从传统的ASP函数返回记录集 [英] Return recordset from function in classic ASP
问题描述
我在我如何能在传统的ASP函数返回一个可读的记录丢失。
这是我想出了,但它不工作:
Response.Clear
Response.Charset的=UTF-8
Response.ContentType =text / plain的昏暗的计数设置数=测试回复于Count.Fields(0).value的
功能测试 昏暗的查询,连接,命令,记录 查询=等等等等等等 设置连接=的Server.CreateObject(ADODB.Connection)
设置命令=的Server.CreateObject(ADODB.Command)
设置记录=的Server.CreateObject(ADODB.Recordset) Connection.ConnectionString =等等等等等等
Connection.Open 设置Command.ActiveConnection =连接
Command.CommandText =查询 设置记录= Command.Execute 组测试=记录 Recordset.Close
的Connection.close 设置记录=什么
设置命令=什么
设置连接=无结束功能
的回复于Count.Fields(0).value的
行生成项目无法对应于请求的名称在集合中找到或序号。
错误。
与的Response.Write Count.Status
我得到当对象被关闭操作是不允许更换它。
错误。
添加 Count.Open
给的连接不能用于执行此操作。这是无论是在这种情况下关闭或无效。
错误。
标记B的回答之后编辑:
我已经看了断开连接的记录,但我不知道怎么在我的例子中使用它们:每一个教程喂查询直接与 Recordset.Open
记录,但我使用参数化查询,甚至尝试很多方法我不能获得同样的结果的 ADODB.Command
在路上的时候就有的。
我应该怎么办?
先谢谢了。
下面是根据爱德华MOLTENI的回答解决方案:
这与数据库交互功能:
功能测试 昏暗的连接,命令,记录 设置连接=的Server.CreateObject(ADODB.Connection)
设置命令=的Server.CreateObject(ADODB.Command)
设置记录=的Server.CreateObject(ADODB.Recordset) Connection.ConnectionString =等等等等等等
Connection.Open Command.ActiveConnection =连接
Command.CommandText =等等等等等等 Recordset.CursorLocation =为adUseClient
Recordset.Open命令,adOpenForwardOnly,ADLOCKREADONLY 设置Recordset.ActiveConnection =什么 组测试=记录 的Connection.close 设置记录=什么
设置命令=什么
设置连接=无结束功能
在code这调用该函数:
Response.Clear
Response.Charset的=UTF-8
Response.ContentType =text / plain的昏暗的记录设置记录=测试回复于Recordset.Fields(0).value的Recordset.Close设置记录=什么
下面是一个返回断开连接的记录功能
功能RunSQLReturnRS(sqlstmt,则params())
在错误恢复下一个 ''//创建ADO对象
昏暗的RS,CMD
集RS =的Server.CreateObject(ADODB.Recordset)
设置CMD =的Server.CreateObject(ADODB.Command) ''//初始化ADO对象和放大器;该存储过程的参数
cmd.ActiveConnection = GetConnectionString()
cmd.CommandText = sqlstmt
cmd.CommandType =为adCmdText
cmd.CommandTimeout = 900 ''//即把PARAMS在cmd propietary功能
collectParams CMD,则params ''//执行的只读查询
rs.CursorLocation =为adUseClient
rs.Open CMD,adOpenForwardOnly,ADLOCKREADONLY
如果Err.Number的> 0,则
BuildErrorMessage()
退出功能
万一 ''//断开记录
设置cmd.ActiveConnection =什么
设置CMD =什么
设置rs.ActiveConnection =什么 ''//返回生成的记录
设置RunSQLReturnRS = RS结束功能
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
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屋!