Class that runs the SQL statement on the specified database

Source: Internet
Author: User
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
%>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.