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