Operation is not allowed when the object is closed(对象未关闭)

Operation is not allowed when the object is closed (Object is not closed)

以下代码会产生该错误。

Set getList = Server.CreateObject("ADODB.Command")
getList.ActiveConnection=EV_WikiConn
getList.Prepared = true
getList.commandtext= "declare @Lookup table(Id int identity(1, 1) , SongTitle nvarchar(512) ) 
insert into @Lookup(SongTitle)select * from ( values ('Deuce')) as x(a) 
select A.AlbumName, S.SongTitle , S.Writers , S.Vocals , S.SID , S.TheTime 
from Albums A inner join Songs S on A.AID = S.AID inner join @Lookup L on L.SongTitle = S.SongTitle order by L.Id"
set rsList = getList.execute
while not rsList.eof ' Error is on this line here.

我在这里添加了这段代码

Set getList = Server.CreateObject("ADODB.Command")
getList.ActiveConnection=EV_WikiConn
getList.Prepared = true
getList.commandtext= "declare @Lookup table(Id int identity(1, 1) , SongTitle nvarchar(512) ) 
insert into @Lookup(SongTitle)select * from ( values ('Deuce'),('Strutter'),('Parasite')) as x(a) 
select A.AlbumName, S.SongTitle , S.Writers , S.Vocals , S.SID , S.TheTime 
from Albums A inner join Songs S on A.AID = S.AID inner join @Lookup L on L.SongTitle = S.SongTitle order by L.Id"
set rsList = getList.execute
If rsList.State <> adStateOpen Then
While rsList.State <> adStateOpen
Set rsList = rsList.NextRecordset

rsList.movenext
wend
end if

这使得它成为 运行,但是,我只得到一条记录,而不是实际表格中的 10 条。所以,这是行不通的,但想展示我到目前为止所做的尝试。

好的,明白了。下面是使用的代码,代码中有一些注释,以显示我为使其工作所做的工作。

Set getList = Server.CreateObject("ADODB.Command")
getList.ActiveConnection=EV_WikiConn
getList.Prepared = true
getList.commandtext = _
    "SET NOCOUNT ON " & _
    "declare @Lookup table(Id int identity(1, 1) , " & _
    "SongTitle nvarchar(512) ) " & _
    "insert into @Lookup(SongTitle)select * from " & _
    "( values ('Hotter_Than_Hell'), ('Firehouse'), ('She'), " & _
    "('Parasite'), ('Nothin''_To_Lose')) as x(a) " & _
    "select A.AlbumName, S.SongTitle , S.Writers , S.Vocals , " & _
    "S.SID , S.TheTime from Albums A inner join " & _
    "Songs S on A.AID = S.AID inner join " & _
    "@Lookup L on L.SongTitle = S.SongTitle order by L.Id"

' the SET NOCOUNT ON, was added, but did not resolve the issue, I just left it in.
' The next 3 lines is what fixed the issue.
While rsList.State <> adStateOpen
    Set rsList = rsList.NextRecordset
Wend
While Not rsList.EOF%>

<%=rsList("SongTitle")%>

<%rsList.movenext
wend
rsList.Close
set rsList = nothing

无论 OP 认为这是什么

ADODB.Recordset error '800a0e78' Operation is not allowed when the object is closed

如果我们分析original code revision (忽略会导致VBScript错误的硬换行)

Set getList = Server.CreateObject("ADODB.Command")
getList.ActiveConnection=EV_WikiConn
getList.Prepared = true
getList.commandtext= "declare @Lookup table(Id int identity(1, 1) , SongTitle nvarchar(512) ) 
insert into @Lookup(SongTitle)select * from ( values ('Deuce')) as x(a) 
select A.AlbumName, S.SongTitle , S.Writers , S.Vocals , S.SID , S.TheTime 
from Albums A inner join Songs S on A.AID = S.AID inner join @Lookup L on L.SongTitle = S.SongTitle order by L.Id"
set rsList = getList.execute
while not rsList.eof ' Error is on this line here.

可以看出原来的问题是缺少

SET NOCOUNT ON;

停止为初始 INSERT 操作返回已关闭的 ADODB.Recordset 对象。

OP 进行的后续编辑混淆了问题,在此之前添加 SET NOCOUNT ON; 无需进一步更改即可解决问题。

next revision 是混乱开始的地方(同样,忽略会导致 VBScript 错误的硬换行)

Set getList = Server.CreateObject("ADODB.Command")
getList.ActiveConnection=EV_WikiConn
getList.Prepared = true
getList.commandtext= "declare @Lookup table(Id int identity(1, 1) , SongTitle nvarchar(512) ) 
insert into @Lookup(SongTitle)select * from ( values ('Deuce'),('Strutter'),('Parasite')) as x(a) 
select A.AlbumName, S.SongTitle , S.Writers , S.Vocals , S.SID , S.TheTime 
from Albums A inner join Songs S on A.AID = S.AID inner join @Lookup L on L.SongTitle = S.SongTitle order by L.Id"
set rsList = getList.execute
If rsList.State <> adStateOpen Then
While rsList.State <> adStateOpen
Set rsList = rsList.NextRecordset

rsList.movenext
wend
end if

让我们来剖析一下这部分

If rsList.State <> adStateOpen Then
While rsList.State <> adStateOpen
Set rsList = rsList.NextRecordset

rsList.movenext
wend
end if

首先,IfWhile 循环做同样的事情接受 While 重复执行直到满足条件,使 If 完全无关。 While 循环检查 ADODB.Recordset 的当前状态,没有 SET NOCOUNT ON; 返回为 adStateClosed (由于 INSERT 操作) 。这很好,但是通过将 While 循环从

更改为
while not rsList.eof

While rsList.State <> adStateOpen

条件已更改,While 不再检查 rsList.EOF,当我们枚举 ADODB.Recordset 对象时会出现这种情况。但是,现在它只检查 rsList.State,一旦调用 .NextRecordset 将退出循环,因为已返回打开的 ADODB.Recordset 对象。因为条件已经满足,所以循环只会 运行 一次调用 .MoveNext 一次并且只返回一条记录。

所以要使用

进行总结
SET NOCOUNT ON;

最初会否定这整个场景,但嘿 显然。