Data | database | statement <%
' Class that runs the SQL statement on the specified database
' Use method:
' Dim runs
' Set Runs=new RunSQL
' Runs.setdbn= database name
' If Runs.ifok then
' Response.Write Runs.errs
' Response.End
' End If
' Runs.setsql=sql
' Runs.run
' If Runs.ifok then
' Response.Write Runs.errs
' Response.End
' Else
' Response.Write ' Successful execution
' End If
On Error Resume Next
Class RunSQL
Private dbname ' database name
Private SQL ' SQL statement to execute
Private Ifsure ' is used to save the flag of success, if the success value is false, the failure is true, the initial value is True
Private Errstr ' Save the text that explains the error
' Get ifsure value
Property Get Ifok ()
Ifok=ifsure
End Property
' Get Errstr value
Property Get Errs ()
Errs=errstr
End Property
'
Private Sub Class_Initialize ()
' Set the initial value of the IFSURE,ERRSTR
Ifsure=true
errstr= "Execute SQL statement on pointing database"
End Sub
' Assigning value to dbname
Property Let Setdbn (DBN)
Dbname=dbn
Ifexistdb DBN
End Property
' Assign a value to the SQL
Property Let SetSQL (s)
Sql=s
End Property
' Perform action
Public Sub Run ()
' Restore class state
Class_Initialize
' Check whether the parameters have been filled in full
If IsNull (dbname) or IsEmpty (dbname) or CStr (dbname) = "" Then
Errstr= "dbname cannot be empty"
Exit Sub
End If
If IsNull (SQL) or IsEmpty (SQL) or CStr (sql) = "" Then
errstr= "SQL cannot be empty"
Exit Sub
End If
Dim conn ' Connect Database objects
Set Conn=server.createobject ("Adodb.connection")
If Err.number<>0 Then
Errstr= "Build adodb.connection against like failure."
Set objcreate=nothing
Exit Sub
End If
Errstr= "Cannot connect to a database"
' Connect to the database
conn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +server.mappath (dbname)
Conn.Open
errstr= "Execute SQL statement failed"
' Execute SQL statement
Conn.execute (SQL)
' If there is no error set the success flag
If Err.number=0 Then
Ifsure=false
End If
End Sub
Private Sub Ifexistdb (ByVal dbn)
' Restore class state
Class_Initialize
' If the database exists, set to true, because if it does not exist, you cannot continue to execute this class
' Check if the database already exists
Errstr= "Database does not exist"
Dim conn
Set Conn=server.createobject ("Adodb.connection")
conn.connectionstring= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +server.mappath (DBN)
Conn.Open
If Err.number=0 Then
Ifsure=false
End If
End Sub
End Class
%>