无法打开记录集

时间:2013-08-02 05:40:55

标签: asp-classic

我编写了一个基本的asp经典类来处理与数据库的所有连接。 当被叫时,一切正常,但第二次被称为记录集时没有打开任何想法?

Class SQLConnection
Private Sub Class_Initialize
    set ConnectionObject = Server.CreateObject("ADODB.Connection")
    Set RecordsetObject = Server.CreateObject("ADODB.Recordset")
End Sub
Private Sub Class_Terminate
    Set ConnectionObject = Nothing
    Set RecordsetObject = Nothing
End Sub

Public Default Property Get Item(sString)
        On Error Resume Next
            Item = RecordsetObject(sString)
        On Error GoTo 0
If Err.Number  <> 0 then
        Item = null
    End if
End Property

Public Sub MoveNext
    If Not RecordsetObject.EOF Then RecordsetObject.MoveNext
End Sub

Public Function EOF 
    EOF = RecordsetObject.EOF
End Function

Public Sub Open(SQLStr,ConnStr)
    ConnectionObject.Open ConnStr
    RecordsetObject.Open SQLStr, ConnectionObject, 3
End Sub

Public Sub Close
    RecordsetObject.Close
    ConnectionObject.Close
End Sub

End Class


Set SQLConn = New SQLConnection
SQLConn.Open "SELECT top 10 id FROM tblProfileVillages", ConnectionString
Do While Not SQLConn.EOF
    Response.write(SQLConn("id"))
    SQLConn.MoveNext
Loop
SQLConn.Close
Set SQLConn = nothing

2 个答案:

答案 0 :(得分:1)

我认为错误发生在Item属性中,您必须在调用err.Number之前检查on error goto 0

像这样使用:

Public Default Property Get Item(sString)
    On Error Resume Next
    Item = RecordsetObject(sString)
    If Err.Number  <> 0 then
        Item = null
    End if
    On Error GoTo 0
End Property

此外,我还没有在vbScript中看到“null”。这是你做的一个常数,还是你错过了某个地方的错误? on error goto next业务有时候很烦人。 :)

答案 1 :(得分:0)

事实证明,如果您的sql语句无法在网格中显示,那么该对象会立即关闭,就像插入语句一样。

我通过在使用If RecordsetObject.State = 1 then

运行代码之前检查对象是否已打开来解决此问题