Object-oriented ASP Programming 5-class encapsulation of ADODB

Source: Internet
Author: User

<Script language = "VBScript" runat = "server">
'*************************************** *************************
'Script compont Object Model
'Design for Active Server Pages
'Copyright 2004 Version 2.0
'Made by Yin Shuguang
'*************************************** *************************
'
'ADODB class Encapsulation
'
'*************************************** **************************
'####################################### ##########################
Function createcadoconnection ()
Set createcadoconnection = new cadoconnection
End Function

Class cadoconnection

Public objadoconnection

'================================================ ======================================
Public property get connectionstring
Connectionstring = objadoconnection. connectionstring
End Property

Public property let connectionstring (byval connstring)
Objadoconnection. connectionstring = connstring
End Property

'================================================ ======================================
Private sub class_initialize 'setup initialize event.

On Error resume next

Set objadoconnection = server. Createobject ("ADODB. Connection ")
Objadoconnection. connectiontimeout = 15
Objadoconnection. commandtimeout = 30
Objadoconnection. cursorlocation = 3

End sub
Private sub class_terminate 'setup terminate event.

Closedbconn ()
Set objadoconnection = nothing

End sub

'Open ADODB. Connection
Public sub opendbconn (dblink)
On Error resume next
Objadoconnection. connectionstring = dblink
Opendbconn2 ()
End sub

Public sub opendbconn2 ()
On Error resume next
Closedbconn ()
Objadoconnection. open ()
If err. Number> 0 then
Response. Write "failed to open the data warehouse! "
Response. End
End if
End sub

Public sub closedbconn ()
If objadoconnection. State = 1 then
Objadoconnection. Close
End if
End sub

Public Function getconnection ()
Set getconnection = objadoconnection
End Function

Public sub adoerror ()
Response. Write ("error description (applicable to technicians): <br> ")
If (objadoconnection. errors. Count> 0) then' has an error
For I = 1 to objconn. errors. Count
Response. Write I & ":" & objadoconnection. errors (I-1). Description & "<br>"
Next 'end of
Set adoerror = true
End if
Set adoerror = false
End sub

Public Function haveerror ()
If (objadoconnection. errors. Count> 0) then' has an error
Haveerror = true
Else
Haveerror = false
End if
End Function
'--------------------------------------------------------------
Public Function execute (SQL)
On Error resume next
Objadoconnection. Execute (SQL)
If err. Number> 0 then
Execute = false
Else
Execute = true
End if
End Function

Public Function executetors (SQL)
On Error resume next
Set executetors = objadoconnection. Execute (SQL)
End Function
End Class

'####################################### ##########################
Function createcadorecordset ()
Set createcadorecordset = new cadorecordset
End Function
Class cadorecordset

Public objadorecordset
Private objadocommand
Private objadoconnection

'================================================ ======================================
Public property get writable commandtype
Export commandtype = objadocommand. commandtype
End Property

Public property let initialize commandtype (byval struct type)
Objadocommand. commandtype = primitive type
End Property
'----------------------
Public property get pagesize
Pagesize = objadorecordset. pagesize
End Property

Public property let pagesize (byval ipagesize)
Objadorecordset. pagesize = ipagesize
End Property
'--------------------
Public property get absolutepage
Absolutepage = objadorecordset. absolutepage
End Property

Public property let absolutepage (byval iabsolutepage)
Objadorecordset. absolutepage = iabsolutepage
End Property
'-------------------
Public property get recordcount
Recordcount = objadorecordset. recordcount
End Property
'-------------------
Public property get pagecount
Pagecount = objadorecordset. pagecount
End Property
'--------------------
Public property get fieldcount
Fieldcount = objadorecordset. Fields. Count
End Property
'================================================ ======================================

Private sub class_initialize 'setup initialize event.

Set objadorecordset = server. Createobject ("ADODB. recordset ")
Objadorecordset. cachesize = 10
Objadorecordset. cursortype = 3
Objadorecordset. cursorlocation = 3
Objadorecordset. locktype = 3

Set objadocommand = server. Createobject ("ADODB. Command ")
Objadocommand. commandtype = 1

End sub
Private sub class_terminate 'setup terminate event.

Closerecordset ()
Set objadorecordset = nothing
Set objadocommand = nothing
If objadoconnection <> empty then
Set objadoconnection = nothing
End if

End sub

'---------------------------------------------------------------
Public sub closerecordset ()
If objadorecordset. State = 1 then
Objadorecordset. Close
End if
End sub

Public sub setconnection (objconnection)
Set objadoconnection = objconnection
Set objadocommand. activeconnection = objadoconnection
End sub

Public sub openrecordset (byval SQL)
Closerecordset ()
Objadocommand. commandtext = SQL
Set objadorecordset = objadocommand. Execute ()
End sub

Public sub openrecordset2 (SQL, Conn, cursortype, locktype)
Closerecordset ()
Objadorecordset. Open SQL, Conn, cursortype, locktype
End sub

Public sub cmdexecute (byval SQL)
Objadocommand. commandtext = SQL
Objadocommand. Execute ()
End sub

'---------------------------------------------------------------
Public Function getfieldvalue (byval fieldname)
Getfieldvalue = objadorecordset (fieldname). Value
End Function

Public Function getfieldname (byval fieldorder)
Getfieldname = objadorecordset (fieldorder). Name
End Function

Public Function setfieldvalue (fieldname, fieldvalue)
Objadorecordset (fieldname). value = fieldvalue
End Function

Public Function getbinaryfield (fieldname)

End Function

Public Function setbinaryfield (fieldname, fieldvalue)

End Function

Public Function rsupdate ()
Objadorecordset. Update
End Function

Public Function getrecordset ()
Set getrecordset = objadorecordset
End Function
'---------------------------------------------------------------
Function isempty ()
Isempty = (objadorecordset. bof and objadorecordset. EOF)
End Function

Function iseof ()
Iseof = objadorecordset. EOF
End Function

Function isbof ()
Isbof = objadorecordset. bof
End Function

'---------------------------------------------------------------
Function movefirst ()
Objadorecordset. movefirst
End Function

Function moveprevious ()
If (not objadorecordset. bof) then
Objadorecordset. moveprevious
End if
End Function

Function movenext ()
If (not objadorecordset. bof) then
Objadorecordset. movenext
End if
End Function

Function movelast ()
Objadorecordset. movelast
End Function

End Class
'####################################### ##########################
Function createcadocommand ()
Set createcadocommand = new cadocommand
End Function
Class cadocommand

Public objadocommand

'================================================ ======================================
Public property get writable commandtype
Export commandtype = objadocommand. commandtype
End Property

Public property let initialize commandtype (byval struct type)
Objadocommand. commandtype = primitive type
End Property

'================================================ ======================================

Private sub class_initialize 'setup initialize event.

Set objadocommand = server. Createobject ("ADODB. Command ")
Objadocommand. commandtype = 1

End sub
Private sub class_terminate 'setup terminate event.

Set objadocommand = nothing

End sub

Public sub cmdexecute ()

End sub

End Class

</SCRIPT>

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.