我在经典 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 显然是一个对象,所以我不明白。
谢谢