2016-02-08 168 views
0

以下代碼正在生成該錯誤。對象關閉時不允許操作(對象未關閉)

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就是在實際的形式。所以,這是行不通的,但想顯示我迄今爲止所嘗試的。

+0

請添加您獲取一條記錄的代碼,'while not rsList.eof'循環中的部分 – SearchAndResQ

+0

兩個代碼現在都有。最底層的代碼是隻給出一條記錄的代碼,即使在這個例子中有3個應該顯示。 –

+0

加入歌曲標題永遠不會是一個好主意,如果標題被調整會發生什麼?在可能的情況下使用ID來形成獨特的關係,文本的從不是一個好主意。 「歌曲」表格是否沒有針對您可以使用的每首歌曲的唯一ID而不是「SongTitle」? – Lankymart

回答

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

錯過了你沒有循環記錄集,這隻會導致1條記錄返回。仍然建議你在SQL中建立更強大的關係,儘管加入'SongTitle'會引起麻煩。 – Lankymart

+0

您是指這個..............L.SongTitle = S.SongTitle ............... SongTitle只存在於一個表中。不是2個表格,這並不比較來自2個不同表格的2個值... L = @Lookup –

+0

無論如何在文本字段上鍊接都是不好的做法。 – Lankymart

0

無論什麼樣的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 

首先IfWhile循環做同樣的事情接受While重複做它,直到條件得到滿足,使得If完全無關。 While循環檢查ADODB.Recordset的當前狀態,其中無SET NOCOUNT ON;返回爲adStateClosed(由於INSERT操作)。這是不錯,但由

while not rsList.eof 

改變While環路

While rsList.State <> adStateOpen 

的條件已經改變,While不再檢查rsList.EOF當我們歷數ADODB.Recordset這將是這種情況目的。但是,現在只是檢查.StatersList,其中一旦調用.NextRecordset將退出循環,因爲已打開ADODB.Recordset對象已被返回。由於條件已滿足,循環只會運行一次,只會調用.MoveNext一次且只返回一條記錄。

因此,要總結使用

SET NOCOUNT ON; 

最初會否定這整個方案,但嘿"I don't understand the code"明顯。

+0

說真的,現在,你想添加東西,應該在我提供工作代碼之前添加。 AN,你完全錯了.......這套NOCOUNT ON .......沒有解決問題,你沒看過我寫的東西。夥計,你有問題,你的代碼是錯誤的!!!!!!!!! –

+0

@WayneBarron剛剛閱讀答案,那麼也許你會明白,但你的磨蝕性質,我懷疑它。'SET NOCOUNT ON;'會起作用,我已經多次證明我在睡夢中寫這些東西。我只是覺得有必要在你的目標無意義的ramblings之後發佈這個,以證明我確實瞭解代碼,但似乎你並不知道! – Lankymart

相關問題