0

我编写了一个基本的 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
4

2 回答 2

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生意有时会很烦人。:)

于 2013-08-02T09:36:48.617 回答
0

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

我通过在运行代码之前检查对象是否打开来解决这个问题If RecordsetObject.State = 1 then

于 2013-08-05T01:52:11.293 回答