ASP database class

zhaozj2021-02-16  82

ASP database class

First, preface mentioned the ASP operation database, most people will think of: shared connection string constr, conn.execute sqlcmd mode execution command, recordset.open sql, conn, 1, 1 acquisition record Set, this method is indeed adopted by 99% of people or companies. For the error generated during the operation database, I am afraid that 99% will not process it. Either join the ON Error Resume next "easy" to jump over the beginning of the program, or let the error message together with the error code "Ruth" in front of the viewer . For the former case, it may generate a wonderful weird outcomes, the latter case, may expose your sensitive information at a certain time (such as connecting the database), affecting the security of the website. Of course, there is still an individual responsible programmer comrades to add if Err.xxxx that is easy to generate errors to handle possible errors, but this doesn't seem to have a good way, and you may miss it. I didn't want to understand why IN Error Goto existing in VB and ASP.NET, is delayed in the ASP. Also, when you use on Error Resume next, you don't want to get next, sorry, no way, you can only "implement it". Look at the exception mechanisms of other languages, try..catch..finally is really cool!

Sarrely, don't introduce new content such as an abnormal mechanism, after all, the ASP language itself decides that this is impossible (ASP.NET is implemented), just wants the most common most popular in the ASP. In the wrong database operation, find a valid error handling method, and encapsulate conn, recordset, etc. to achieve the maximum streamline. Then there is the following database class.

Second, the database class 1, the function is just as mentioned earlier, the purpose of this class is to encapsulate the cumbersome operations such as AdoDb.Connection, AdoDb.Recordset and implement errors in the class. Now look at the members, attributes and methods of the class: 1) Members: (no public or protective members) 2) Properties: classname- Return to class Version- Return version LastError- Return the last error IgnoreError-Setup / return whether to ignore database errors Connection- Returns the connection object (AdoDb.Connection) Connectionstring- Settings / Return Connection Strings (This example is SQL Server, such as other please set up according to actual settings) FIELDCOUNT, PageSize, PageCount, AbsolutePage, AbsolutePosition, Bof, EOF- Please refer AdoDb.recordset Corresponding Content 3) Method: Setup- Set the account, password, database name, host / ip connect-Connection database close-turn the database connection and release the database query command and return to the Data Set EXESQL- Execute SQL Command (Do not return database) FieldName- Return Field Name Fields - Return the value of the specified (serial number or field name) field Data- with MoveNext, Movese, MoveFirst, Movelast- Please refer to AdoDb.Recordset The corresponding content 2, Implement code (dbsql.inc.asp)

Too long, click here to open / fold ... <% '================================== =============================================== : 2003-12-18 'Site: http://kacarton.yeah.net/' blog: http://blog.9cbs.net/conch 'email: kacarton@sohu.com' modify: '2004-6-25 : Upgrade the data engine, returning the error code less than 0 (can also be an ASP log in 'definition), modify the error detection err.Number> 0 ==> Err.Number <> 0' 2004-6-30: Modify the error handling, ignore the cursor type change, etc., the cursor type change, the author is the author original. Please contact him before reprinting, please indicate the article, retain the author information, thank you for your support! '==================================================== ======================= Class clsdb 'Name of this class' var string' @access private '@see printy: name private m_strname' Version of this class 'var string' @access private '@see proty: 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_passwo rd '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 Last Error Messages.' Usage: Otemplate.lasterror '@Access public public Property Get LastError () lasterror = m_lasterror endproperty'

Get or Set Ignore connection.errors Public Property Get IgnoreError () IgnoreError = m_IgnoreError End Property Public Property Let IgnoreError (ByVal Value) m_IgnoreError = Value End Property 'Get Connection Public Property Get Connection () Connection = m_Connection End Property' Get connection string Public Property Get ConnectionString () ConnectionString = m_ConneStr End Property 'Set connection string Public Property Let ConnectionString (ByVal Value) m_ConneStr = Value End Property' Get data fields count Public Property Get FieldCount () FieldCount = m_RecordSet.Fields.Count End Property 'Get RecordSet PageSize Public Property Get PageSize () on error resume next PageSize = m_RecordSet.PageSize if err.number <> 0 then ShowError ( "Can not get PageSize!") End Property 'Set RecordSet Page Size Public Property Let PageSize (ByVal Value) on Error Resume Next M_RecordSet.pageSize = Value if Err.Number <> 0 THEN Showerror "Can not set PageSize to" & Value) End Property 'Get RecordSet page count Public Property Get PageCount () PageCount = m_RecordSet.PageCount End Property' Get RecordSet record count Public Property Get RecordCount () on error resume next RecordCount = m_RecordSet.RecordCount if err.number <> 0 then ShowError ( "Get RecordCount error.") End Property 'Get RecordSet Absolute Page Public Property Get AbsolutePage () on error resume next AbsolutePage = m_RecordSet.AbsolutePage if err.number <> 0 then ShowError ( " Can not get absolutepage! ") End Property '

Set RecordSet Absolute Page Public Property Let AbsolutePage (ByVal Value) on error resume next m_RecordSet.AbsolutePage = Value if err.number <> 0 then ShowError ( "Can not set AbsolutePage to" & Value) End Property 'Get RecordSet Absolute Position Public Property Get AbsolutePosition () on error resume next AbsolutePosition = m_RecordSet.AbsolutePosition if err.number <> 0 then ShowError ( "Can not get AbsolutePosition!") End Property 'Set RecordSet Absolute Position Public Property Let AbsolutePosition (ByVal Value) on error resume next m_RecordSet.AbsolutePosition = Value if err.number <> 0 then ShowError ( "Can not set AbsolutePosition to" & Value) End Property 'Bof Public Property Get Bof () Bof = m_RecordSet.Bof end Property' Eof Public Property Get Eof () EOF = m_recordset.eof endprys' setup the DATABEASE HOST NAME, DATABASE NAME, User Name (Account), Password Public Sub Setup (Account, Password, Database, Host) m_username = Account m_Password = Password if Database <> "" then m_strDatabase = Database if Host <> "" then m_strHost = Host m_ConneStr = "Driver = {SQL Server}; Server =" & m_strHost & "; Database =" & _ m_strDatabase & " UID = "& m_username"; pwd = "& m_password"; "End Sub 'Connect to Database Public Function Connect () on Error ResMe Next M_Connection.Open M_ConneStr if Err.Number <> 0 THEN ShowError (" Database connection Error: (Server: "& M_StrHost &", Database: "&"

m_strDatabase & ")") m_bIsConnect = true Connect = true 'todo: // end Function' Diconnect database Public Function Close () on error resume next Set m_RecordSet = Nothing Set m_Connection = Nothing m_bIsConnect = false Close = true if err.number < > 0 then ShowError ( "error database Connectivity off") end Function 'Query Public Sub Query (SQLCommand) on error resume Next if not m_bIsConnect then connect Set m_RecordSet = Server.CreateObject ( "Adodb.Recordset")' Set m_RecordSet = m_Connection .Execute (SQLCommand) m_RecordSet.Open SQLCommand, m_Connection, 1, 1 if err.number <> 0 then ShowError (SQLCommand): exit sub if m_Connection.Errors.Count> 0 And m_IgnoreError = false then ProcessError (SQLCommand) End Sub ' ExeSQL Command Public Sub ExeSQL (SQLCommand) on error resume Next if not m_bIsConnect then Connect m_Connection.Execute SQLCommand if err.number <> 0 then ShowError (SQLCommand): exit sub if m_Connection.Errors.Count> 0 And m_IgnoreError = false then ProcessError (SQLCommand) End Sub 'Get Fields Name Public Function FieldName (ByVal FieldId) on error resume next FieldName = m_RecordSet.Fields (FieldId) .Name if err.number <> 0 then ShowError ( "field can not be read" & FieldID & "Name!") End Function 'Get Fields Data Public Function Fields (Byval Fieldid) on Error ResMe Next Fields = M_RecordSet.fields (Fieldid) if Err.Number <> 0 Then ShrowError ("Cannot Read Field" & FieldID & FieldID & FieldID & FieldID & FieldID & Field "Data!") End Function 'Get Fields Data Public Function Data (Byval Fieldid) ON Error ResMe Next Data =

M_RecordSet.fields (Fieldid) if Err.Number <> 0 THEN Showerror ("You cannot read" & FieldID & "Data!") 'IF m_connection.errors.count> 0 Then Showerror ("You cannot read" & FieldID & " data! ") End Function 'Move to next record Public Sub MoveNext () on error resume next if m_bIsConnect then m_RecordSet.MoveNext if err.number <> 0 then ShowError (" MoveNext error ") End Sub' Move to Previous record Public Sub MovePrevious () on error resume next if m_bIsConnect then m_RecordSet.MovePrevious if err.number <> 0 then ShowError ( "MovePrevious error") End Sub 'Move to First record Public Sub MoveFirst () on error resume next if m_bIsConnect then m_RecordSet.MoveFirst if err.number <> 0 then ShowError ( "MoveFirst error") End Sub 'Move to Last record Public Sub MoveLast () on error resume next if m_bIsConnect then m_RecordSet.MoveLast if err.number <> 0 then ShowError ( "MoveLast error ") End Sub '2004-6-30 Private Sub Processerror (Byval Sqltxt) for i = 0 to m_Connecti on.Errors.Count-1 If m_Connection.Errors.Item (i) .Number <> 0 Then ShowError (sqltxt) Next End Sub 'This function is called whenever an error occurs and will handle the error' Additionally the error message will be saved in m_strLastError. '@param $ msg a string containing an error message' @access private '@return void Private Sub ShowError (ByVal sqltxt) for i = 0 to m_Connection.Errors.Count-1 Response.Write m_Connection.Errors.Item (i) & "(" & M_Connection.erroS.Item (i) .Number & ")

"Next m_lasterror = err.description m_connection.errors.clear response.write"
------------------------------- -----------------------
"& _" "& SQLTXT &" "'Response .Write
-------------------------------------------------------------------------------------------------------------------------------------------------- ----------
"& _ '" "& left (SQLTXT, 10) &" ... (Error message has been blocked), please contact the website Administrator contact! " 'Response.End End Sub' Class constructor, set class default attributes, you can change it Private Sub class_Initialize m_strName =" clsDB "m_strVersion =" 1.0 "Set m_Connection = Server.CreateObject (" ADODB.Connection ") 'Please modify the default value for you to connect the database. = NOTHING SET M_CONNECTION = Nothing End Sub End Class%> 3 (NULL) "_ else htmlencode = server.htmlencode (str) End Function

DIM SQL, ISET SQL = New CLSDBSQL.ConnectSql.exesql ("Update Customers Set Address = 'The' WHERE ID = 1" of the People's Republic of China) SQL.Query ("Select * from customers") response.write

"for i = 0 to sql.fieldcount-1 Response.write"
"& Server.htmlencode (SQL.FieldName (i)) &" "Next" "While Not Sql.eOf for i = 0 to Sql.fieldcount-1 Response.write" "& HTMLENCode (SQL.Data (i)) &" "This can be used directly with the field name i Next response.write "" SQL.MOVENEXTWENDRESPONSE.WRITE "" SQL.Closset SQL = Nothing%> Three, Summary This is just a relatively rough database class, there are many adoDB features no Adding, and flexibility is not enough. This article aims to provide you with another idea. Everyone thinks there is still a little harvest after reading this article. I am very satisfied. In addition, my next article "Package the ASP query page package" will be used.

转载请注明原文地址:https://www.9cbs.com/read-15881.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.044, SQL: 9