0

我在经典 ASP 中有错误:

Microsoft VBScript runtime error '800a01a8'
Object required: 'SqlStatement'
/Data/Database.asp, line 186 

第 186 行是SqlStatement.CommandText = sql

这是我的代码:

Dim Db, SqlStatement, RS
Set Db = Nothing
Set SqlStatement = Nothing
Set RS = Nothing

' Initialize the database once
Private Sub SqlInitDb()
    If isNull(Db) Or varType(Db) = vbEmpty Or IsObject(Db) = False Then
        ' Create database connection object
        Set Db = Server.CreateObject("ADODB.Connection")
        ' Set connection timeout
        Db.ConnectionTimeout = Session("ConnectionTimeout")
        ' Set command timeout
        Db.CommandTimeout = Session("CommandTimeout")
        ' Open database
        Db.Open Session("ConnectionString")
    End If

    If isNull(SqlStatement) Or varType(SqlStatement) = vbEmpty Or IsObject(SqlStatement) = False Then
        ' Create a sql statement
        Set SqlStatement = Server.CreateObject("ADODB.Command")
        ' Set opened database as active connection for the SQL statement
        SqlStatement.ActiveConnection = Db
    End If
End Sub

' Close the database if it is not closed
Private Sub SqlClose()
    If Not isNull(SqlStatement) And varType(SqlStatement) <> vbEmpty And IsObject(SqlStatement) = True Then
        SqlStatement.ActiveConnection.Close
    End If

    Set SqlStatement = Nothing

    If Not isNull(Db) And varType(Db) <> vbEmpty And IsObject(Db) = True Then
        For Each objErr In Db.Errors
          response.write("<p>")
          response.write("Description: ")
          response.write(objErr.Description & "<br>")
          response.write("Help context: ")
          response.write(objErr.HelpContext & "<br>")
          response.write("Help file: ")
          response.write(objErr.HelpFile & "<br>")
          response.write("Native error: ")
          response.write(objErr.NativeError & "<br>")
          response.write("Error number: ")
          response.write(objErr.Number & "<br>")
          response.write("Error source: ")
          response.write(objErr.Source & "<br>")
          response.write("SQL state: ")
          response.write(objErr.SQLState & "<br>")
          response.write("</p>")
        Next
        Db.Close()
        Set Db = Nothing
    End If
End Sub

' Insert a user to the database
Public Function InsertUser(ByVal userObj)
    If isNull(userObject) Or varType(userObject) = vbEmpty Then
        InsertUser = -1
        Exit Function
    End If
    Call SqlInitDb()
    Set RS = Server.CreateObject("ADODB.Recordset")

    Dim sql
    sql = "INSERT INTO users(Firstname,Surname) VALUES ('"&userObj.getFirstname()&"','"&userObj.getSurname()&"')"
    SqlStatement.CommandText = sql
    SqlStatement.CommandType = 1

    ' Run the insert statement
    SqlStatement.Execute
    InsertUser = Err.Number
    Call SqlClose()
End Function

SqlStatement 显然是一个对象,所以我不明白。

谢谢

4

1 回答 1

3

VBScript 有很多方法可以将变量定义为空、空、无等。

您偶然发现的问题是Nothing实际上是一个对象。它不是空的,它不是空的,它是完全有效的对象,只是碰巧是一种特殊的类型。

因此,您的条件总是失败,即使变量是 Nothing,也会返回 False。

要解决此问题,请使用 VBScript 中唯一正确的方法来检查该特殊值:

If SqlStatement Is Nothing Then
    ' Create a sql statement
    Set SqlStatement = Server.CreateObject("ADODB.Command")
    ' Set opened database as active connection for the SQL statement
    SqlStatement.ActiveConnection = Db
End If

唯一的缺点是您现在必须确保将变量初始化为Nothing,您现在就这样做,否则会抛出错误。

于 2013-04-14T07:35:43.403 回答