AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=113&pId=-1
Database Class in Classic ASP
page
by Justin Owens
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25157/ 34

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
'Class Initialization
Private Sub Class_Initialize()
     'Empty
End Sub

'Terminate Class
Private Sub Class_Terminate()
     'Empty
End Sub

'Open Access Database
     Public Sub DBOpenAccess(strDBLoc)
     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

'Open SQL Database
Public Sub DBOpenSQL(strSERVER,strDATABASE,strUID,strPWD)
     strConnStr = "DRIVER={SQL Server};SERVER=" & strSERVER & ";UID=" & strUID & ";PWD=" & strPWD & ";DATABASE=" & strDATABASE & ";"
     Set cnnObj = server.CreateObject("ADODB.Connection")
     cnnObj.Open strConnStr
End Sub

'Close Database
Public Sub DBClose()
     cnnObj.Close
     Set cnnObj = Nothing
End Sub

'Functions
'Open Recordset
Public Function OpenRec()
     Set objRec = server.CreateObject("ADODB.Connection")
End Function

'Execute SQL
'Returns GetRows array - if no recset returned, it returns false.
Public Function ExecuteSQL(strSQLStatement)
     Set objRec = cnnObj.Execute(strSQLStatement)
     If Not objRec.EOF Then
          ExecuteSQL = objRec.GetRows()
     Else
          ExecuteSQL = False
     End If
End Function

'Execute SQL
'Updates, inserts or deletes records in tables
Public Function ExecuteUpdateSQL(strSQLStatement)
     Set objRec = cnnObj.Execute(strSQLStatement)
End Function

'Close RecordSet
Public Function CloseRec()
     objRec.close
     Set objRec = Nothing
End Function

End Class
%>


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-12-08 7:26:48 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search