ASP's Database Classes

Source: Internet
Author: User
Tags error code error handling modify
Data | Database ASP Database Class

First, the preface
Referring to ASP operation database, most people would think: Common connection string constr, Conn.Open constr Establish database connection, Conn.execute sqlcmd Execution Command, Recordset.Open Sql,conn, 1,1 get a Recordset, it is true that this method is adopted by 99% of people or companies. For errors that occur during the operation of the database, it is feared that 99% of the people will not process it, either by adding on Error Resume Next "easily" at the beginning of the program, or by having the error message "corpse" with the error code in front of the viewer. The first situation can be a strange and bizarre result, the latter case, may be at a certain time (such as connecting to the database) to expose your sensitive information, affecting the security of the site. Of course, there are individual responsible programmers who will add an if err.xxxx to handle possible errors after an error-prone operation, but this does not seem to be a good idea, inadvertently may be omitted.
I still do not want to understand, why in VB and asp.net exist in the Error Goto, but in the ASP was canceled.
Also have to mention is that when you use On Error Resume Next and do not want to resume next in the back, sorry, no way, you can only "carry it out". Look at the exception mechanisms in other languages, try ... Catch.. Finally arbitrary, really cool!

Say so much, and do not introduce new content such as exception mechanism for ASP, after all, the ASP language itself also determines that this is not possible (implemented in asp.net), just want in the ASP is the most common and most prone to error in the database operations, find an effective way to deal with the error, and the Conn , the recordset and so on encapsulation, achieves the maximum simplification. So we have the following database class.


Second, the database class
1, function
As mentioned earlier, the purpose of this class is to encapsulate the tedious operations of Adodb.connection and Adodb.recordset and to implement error handling in the class. Now look at the members, properties, and methods of the class:
1) Members: (no public or protected members)
2) Properties:
classname-Return class name
version-return version
lasterror-returns the last error
ignoreerror-Sets/Returns whether database errors are ignored
connection-return Connection object (adodb.connection)
connectionstring-Set/Return connection string (this example is SQL Server, as for other please according to the actual setting)
FieldCount, PageSize, PageCount, AbsolutePage, AbsolutePosition, Bof, eof-, please refer to adodb.recordset appropriate content
3) Method:
setup-set up the account number, password, database name, host/IP connection data server
connect-Connection Database
close-Close the database connection and release resources
query-Execute database Query command and return data set
exesql-Execute SQL command (does not return database)
fieldname-returns the field name of the specified ordinal
fields-returns the value of the specified (ordinal or field name) field
Data-ditto
MoveNext, MovePrevious, MoveFirst, movelast-please refer to adodb.recordset corresponding content


2, implementation code (DBSQL.INC.ASP)
Content is too long, click here to open/collapse ...
<%
'=======================================================================
' CLASS Name:clsdb
' Design by: Peng
' Date:2003-12-18
' site:http://kacarton.yeah.net/
' Email:kacarton@sohu.com
' MODIFY:
' 2004-6-25: Upgraded data engine, return error code less than 0 (also can be ASP to the value of the
' Define change ', modify error detection err.number>0 ==> err.number<>0
' 2004-6-30: Modifying error handling, ignoring error-like cursor type changes
'=======================================================================

Class Clsdb

' Name of this class '
' Var string
' @access Private
' @see Property:name
Private M_strname

' Version of this class
' Var string
' @access Private
' @see property:version
Private m_strversion

' Error Object
' @var ADODB. Connection.errors
' @access private
' @see Property:lasterror
Private M_lasterror

' Ingore all Connection.errors
' Var Boolean
' @access private
' @see Property:ignoreerror
Private M_ignoreerror

' Connection Object
' Var ADODB. Connection
' @access Private
' @see property:connection
Private m_connection

' is connection to database?
' Var Boolean
' @Private
Private M_bisconnect

' RecordSet
' Var RecordSet
' @access Private
Private M_recordset

' Connection string
' Var string
' @access Private
' @see property:connectionstring
Private M_connestr

' Database server host name or IP
' Var string
' @access Private
' @see Property:host
Private M_strhost

' Database name
' Var string
' @access Private
' @see property:database
Private M_strdatabase

' Account to connection database
' Var string
' @access Private
' @see Property:username
Private M_username

' Password to connection database
' Var string
' @access Private
' @see Property:password
Private M_password

' Get class name attribute.
' Usage:oTemplate.Name
' Access public
Public Property Get ClassName ()
ClassName = M_strname
End Property

' Get class version attribute.
' Usage:oTemplate.Version
' Access public
Public Property Get Version ()
Version = M_strversion
End Property

' Get class is last error messages.
' Usage:oTemplate.LastError
' @access public
Public Property Get LastError ()
LastError = M_lasterror
End Property

' Get or Set Ignore connection.errors
Public Property Get Ignoreerror ()
Ignoreerror = M_ignoreerror
End Property

Public Pro



Related Article

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.