2013-02-18 102 views
1

我想從一個結果集獲得記錄計數R2從ASP VBscript的查詢SQL Server 2008中時:Recordset.RecordCount不允許

Conn = "Provider=SQLNCLI10; DataTypeCompatibility=80; Data Source=Source; Initial Catalog=catalog; User ID=id; Password=pw; Network Library=dbmssocn; Encrypt=Yes;" 

這將返回正確的記錄數:

consulta = "select 'x' x;" 

rs.open consulta, Conexao, 3 
Response.Write(rs.RecordCount) 

但是,當我從臨時表中選擇它會引發錯誤:

consulta = "select 'x' x into #t; select * from #t; drop table #t;" 

rs.open consulta, Conexao, 3 
Response.Write(rs.RecordCount) 

ADODB.Recordset error '800a0e78' 

Operation is not allowed when the object is closed. 

回答

3

我認爲您需要使用.NextRecordSet()

strsql = "select 'x' x into #t; select * from #t; drop table #t;" 

rs.Open strsql, conn 

'Move to your 2nd recordset to return the values (in this case 'x') 
Set rs = rs.NextRecordset() 
if not rs.eof then 
    response.write rs(0) 
end if 
rs.close 

這也適用於我,如果我的SQL字符串分離出來,並使用EXECUTEOPEN需要:

'Create Temp Table 
strsql = "select 'x' x into #t; " 
conn.execute strsql 

'Select From Temp Table 
strsql = "select * from #t;" 
rs.open strsql, conn 
if not rs.eof then 
    response.write rs(0) 
end if 
rs.close 

'Drop Temp Table 
strsql = "drop table #t;" 
conn.execute strsql 

希望這有助於。

+0

你正在使用什麼提供商? 'SQLOLEDB'或'SQLNCLI10' /'SQLNCLI11'? – 2013-02-19 11:41:51

+0

@Clodoaldo - 爲了測試,我使用SQLOLEDB連接到MSSQL 2008 R2數據庫。我的連接字符串如下所示:ConnectionString =「Provider = SQLOLEDB; Server = ServerName; Database = DBName; UId = UserId; Pwd = Password」 - 希望這會有所幫助。 – sgeddes 2013-02-19 13:26:41