关闭对象时不允许操作(对象未关闭)

时间:2016-02-08 04:25:25

标签: vbscript asp-classic adodb

以下代码正在生成该错误。

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条记录。所以,这不会起作用,但想展示我到目前为止所尝试的内容。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

无论OP认为这是否重复

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

如果我们分析original code revision (忽略将导致VBScript错误的硬包装)

return data.data;

你可以看到原来的问题是缺乏

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; 操作返回已关闭的ADODB.Recordset对象。

OP的后续修改会使问题混淆,在此之前添加INSERT可以解决问题而无需进一步更改。

next revision是混淆开始的地方(同样,忽略将导致VBScript错误的硬包装)

SET NOCOUNT ON;

让我们剖析一下这部分

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 If循环做同样的事情,接受While重复执行,直到条件满足为止,使While完全无关紧要。 If循环检查While的当前状态,其中ADODB.Recordset <{1}}返回为SET NOCOUNT ON; (由于adStateClosed操作)。这很好但是从

更改INSERT循环
While

while not rsList.eof

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

所以使用

进行摘要
.MoveNext

最初会否定整个场景,但显然是"I don't understand the code"