(Original) SQL and Access Database Connection Management Class

xiaoxiao2021-03-06  111

Today, I have written this class. This class is very simple, but it is also very practical. It will only be able to change the type of the database at any time as long as the file you need directly in this class file is then changed.

<% '================================================ =============== 'Used: DBASECLASS' Usage: You can return SQL or Access's Connection object based on user parameters 'Usage:' (Access Database) 'set dbclass = new dbaseclass' DBClass .dbtype = "access" set to Access database type (default an Access database type) 'dbclass.dbpath = "data / data.mdb" Set Access database file address' set = dbclass.getrs ("SELECT * from [user] ") Returns the RS object 'set conn = dbclass.getConn () returned to the Conn object' set RS = dbclass.getBasers () returned to the Empty RecordSet Object '(SQL Database)' DBClass.dbType =" SQL "setting Setting the user name (default is the SA user) 'dbclass.sqlpassword = "1010" set the user password (default empty)' dbclass.sqlpassword = "1010" set (default empty) 'DBClass.sqlpassword = "1010" DBClass.sqlDatabaseName = "User" Set the library name of the SQL database 'dbclass.sqllocalname = "192.168.0.1" Settings the server address of the SQL database (default is the native address)' set = dbclass.getrs ("SELECT * FROM [User ] ") Returns the RS object 'set conn = dbclass.getConn () returned to the CONN object' set = dbclass.getbasers () returned to the empty recordset object 'set dbclass = nothing closes the empty recordset object' set dbclass = nothing off object '===== ============================================================================================================================================================================================================= ========= Class DBaseClass' database driver connection string variable DIM D_CONN Str '

Access database file DIM D_DBPATH'SQL Database Setting Variable D_SqlDatabaseName, D_Sqlpassword, D_SQLUSERNAME, D_SQLLOCalName'Conn Object D_Conn, D_RS 'Database Drive Type' value: "1", "A" "A" Access "Access" Access "The connection object 'value:" 2 "," s "" SQL "" S "" SQL "" S "" SQL "" S "" SQL "" S "" SQL "" S "" S "" SQL "" S "" SQL "" SQL "is the SQL database drive connection object DIM D_DBTYPE' ============= ========================================================================================================================================================================= ============================================================================================================================================== # "End sub '========================================================================================================================================================================= ====================================================================================================0000 .close set d_rs = Nothing end if if ISObject (d_conn) THEN D_CONN.CLOSE SET D_CONN = Nothing End IFEND SUB

Public Property Let DBTYPE (BNewValue) Select Case Lcase (CSTR (BNewValue)) Case "1", "A", "Access" D_DBTYPE = "Access" Case "2", "S", "SQL" D_DBTYPE = "SQL" case else D_DbType = "Access" End selectEnd PropertyPublic property Get dbType dbType = variable assignment of Public several properties D_DbTypeEnd Property'ACCESS data connection property Let dbPath (bNewValue) D_dbPath = bNewValueEnd PropertyPublic property Get dbPath dbPath = D_dbPathEnd property

'Public several properties variable assignment SQL data connection Property Let SqlDatabaseName (bNewValue) D_SqlDatabaseName = bNewValueEnd PropertyPublic Property Get SqlDatabaseName SqlDatabaseName = D_SqlDatabaseNameEnd Property

Public property let sqlpassword (bnewvalue) D_SQLPassword = BNewValueEnd PropertyPublic property get sqlpassword sqlpassword = d_sqlpasswordnd Property

Public property let sqlusername (BNewValue) D_SQLUSERNAME = BNewValueEnd PropertyPublic Property Get SQLUSERNAME SQLUSERNAME = D_SQLUSERNAMEEND PROPERTY

Public property let sqllocalname (BNewValue) D_SqllocalName = BNewValueEnd PropertyPublic Property Get SqllocalName SqllocalName = D_SQLLocalNameEnd Property

'=======================================================' function function : Get Database Connection Object 'Description:' Return Value: Database Connection Object '================================== ============ Public function getConn () ON Error ResMe next if isobject (d_conn) and not isnull (D_CONN) THEN getConn = D_CONN EXIT FUNCTION END IF SET D_CONN = Server.createObject ("adoDB. ") D_Conn.Open getConnStr () if err.number> 0 Then Err.clear set d_conn = Nothing getConn = null exit function end if set getConn = D_Connend function '============== =================================================================================================== Description: This function is available EXECUTE execution method of Connection objects' parameter: SQLSTR execution SQL language 'return value: database RECORDSET object' =========================== =================== Public Function GETRS (SQLSTR) if SQLSTR = "" THEN getrs = null exit functionend ifset getrs = getConn (). EXECUTE (SQLSTR) End Function

'=======================================================' function function : Get Database RecordSet Object 'Description: This function is obtained by RECORDSET object to establish a RECORDSET object.' Return Value: Database RecordSet Object '======================= ====================== Public function getBasers () IF isnull (D_RS) and not isnull (D_RS) THEN SET GETDBRS = D_RS EXIT FUNCTION ELSE SET D_RS = Server .CreateObject ("adodb.recordset") end if set getdbrs = d_rsend function '================================= ============= 'Function: Get the database connection driver string' Description: This function is the string 'return value that is not obtained according to the D_DBTYPE variable: Database Connection Drive String' == ==================================================== private function getConnstr () SELECT Case D_DBTYPE CASE "Access" getConnstr = getAccessconnstr () Case "SQL" getConnstr = getsqlconnstr () Case else getconnstr = "" End SelectEnd Function

'=======================================================' function function : Get an Access Database Connection Drive String 'Description: This function is based on the D_DbPath variable to get the string' return value: Access database connection drive string '=================== ==========================================================================================001 4.0; data source = "& server.mappath (d_dbpath) else getaccessconnstr =" "end ifnd function

'=======================================================' function function : Get SQL Database Connection Drive String 'Description: This function is based on D_SqlDatabaseName and D_SQLUSERNAME and D_SQLPassword and D_SQLPassword and D_SQLPassword Variables to get string' return value: SQL database connection driver string '============= ====================================================== "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" " Sqloledb; User ID = "& D_SqlUsername &"; Password = "& D_SqlPassword &"; Initial Catalog = "& D_SqlDatabaseName &"; Data Source = "& D_SqlLocalName &"; "else getSqlConnStr =" "end ifEnd FunctionEnd Class%>

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

New Post(0)