代码之家  ›  专栏  ›  技术社区  ›  Wayne Barron

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

  •  0
  • Wayne Barron  · 技术社区  · 10 年前

    下面的代码正在生成该错误。

    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 回复  |  直到 10 年前
        1
  •  0
  •   user692942    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
    
        2
  •  0
  •   Community Mohan Dere    9 年前

    无论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;
    

    停止关闭 ADODB.Recordset 为初始值返回的对象 INSERT 活动

    在添加之前,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
    

    首先 If While 循环执行相同的操作,接受 虽然 重复执行,直到满足条件 如果 完全无关。 这个 虽然 循环检查 记录集 没有 设置NOCOUNT; 返回为 adStateClosed (由于 插入,插入 操作) 。这很好,但通过更改 虽然 循环自

    while not rsList.eof
    

    While rsList.State <> adStateOpen
    

    条件已更改 虽然 不再检查 rsList.EOF 当我们枚举 记录集 对象然而,现在它只检查 .State 属于 rsList 曾经 .NextRecordset 将作为打开退出循环 记录集 对象已返回。由于条件已满足,循环将只运行一次调用 .MoveNext 只返回一条记录。

    因此,使用

    设置NOCOUNT;
    

    一开始会否定整个场景,但嘿 "I don't understand the code" 显然地