DB Class

Database Class in Classic ASP
An old article I found that helps a lot these days.
Published: 13 Nov 2003 by Justin Owens
This tutorial is an attempt to simplify and standardize database access within ASP applications.
The examples can be used in any ASP application.
While working with RecordSets, the GetRows method is used, thus releasing the database connection immediately.
by Justin Owens
Views (Total / Last 10 Days): 43728/ 54

Database Class Code -- Database Class -
In order to return data, the data is returned from the class with an array using getrows, so the array must be erased after use to free up memory.

<%
' Usage
' Returning Data
' Set db = New dbaccess
' Call db.DBOpenAccess("/dblocation/dbfile.mdb")
' Call db.OpenRec()
' arrArray = db.ExecuteSQL("SELECT * FROM [widgets];")
' Call db.CloseRec()
' Call db.DBClose()
' Response.Write(arrArray(0,0))
' Erase arrArray
' -----------OR------------
'Update Functions
'Set db = New dbaccess
'Call db.DBOpenAccess("/dblocation/dbfile.mdb")
'Call db.ExecuteUpdateSQL("DELETE FROM [widgets] WHERE [ID] = 1;")
'Call db.DBClose()
'++---------------------------------------------------------------
'++Class dbaccess
'++Author: Justin Owens
'++Date: 08/01/2002
'++You may use this class when developing your own applications
'++as long as this section of comments remains with the class.
'++---------------------------------------------------------------
Class dbaccess
 'Declarations
 Private cnnObj
 Private objRec
 Private strConnStr
 'Subs
 Private Sub Class_Initialize() 'Class Initialization
  'Empty
 End Sub
 Private Sub Class_Terminate() 'Terminate Class
  'Empty
 End Sub
 Public Sub DBOpenAccess(strDBLoc) 'Open Access Database
  strConnStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="
  strConnStr = strConnStr & server.mappath(strDBLoc) & ";"
  Set cnnObj = server.CreateObject("ADODB.Connection")
  cnnObj.Open strConnStr
 End Sub
 Public Sub DBOpenSQL(strSERVER,strDATABASE,strUID,strPWD) 'Open SQL Database
  strConnStr = "DRIVER={SQL Server};SERVER=" & strSERVER & ";UID=" & strUID & ";PWD=" & strPWD & ";DATABASE=" & strDATABASE & ";"
  Set cnnObj = server.CreateObject("ADODB.Connection")
  cnnObj.Open strConnStr
 End Sub
 Public Sub DBClose() 'Close Database
  cnnObj.Close
  Set cnnObj = Nothing
 End Sub
 'Functions
 Public Function OpenRec() 'Open Recordset
  Set objRec = server.CreateObject("ADODB.Connection")
 End Function
 Public Function ExecuteSQL(strSQLStatement) 'Execute SQL  'Returns GetRows array - if no recset returned, it returns false.
  Set objRec = cnnObj.Execute(strSQLStatement)
  If Not objRec.EOF Then
   ExecuteSQL = objRec.GetRows()
  Else
   ExecuteSQL = False
  End If
 End Function
 Public Function ExecuteUpdateSQL(strSQLStatement) 'Execute SQL 'Updates, inserts or deletes records in tables
  Set objRec = cnnObj.Execute(strSQLStatement)
 End Function
 Public Function CloseRec() 'Close RecordSet
  objRec.close
  Set objRec = Nothing
 End Function
End Class
%>

Views 1506 Downloads 838


ANmarAmdeen
612
Database Classic ASP
Revisions

v2.0