Database Class in Classic ASP
page 1 of 1
Published: 13 Nov 2003
Unedited - Community Contributed
Abstract
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
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 45665/ 80

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
%>


User Comments

Title: nfl jerseys cheap   
Name: NIKE NFL jerseys
Date: 2012-07-02 10:08:44 AM
Comment:
http://www.jersey2shop.com
http://www.cheapjersey2store.com
http://www.jerseycaptain.com
http://www.yourjerseyhome.com
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes
Cheap NFL,NBA,MLB,NHL
,heap jerseys,2012 nike nfl Jerseys,nba jersey and shorts,oklahoma city thunder jersey,official jeremy lin new york knicks jersey,NFL Jerseys Wholesale,blake griffin jersey blue,NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
,Wholesale cheap jerseys,Cheap mlb jerseys,]Nike NFL Jerseys,Cheap China Wholesae,Wholesale jerseys From China,2012 nike nfl Jerseys,Jerseys From China,,2012 nike nfl Jerseys,Revolution 30 nba jerseys,jersey of nba chicago bulls direk rose ,nfl jerseys,green bay packers jerseys wholesale,Buffalo Bills nike nfl jerseys sale,good supplier soccer jerseys,cool base mlb jerseys,Revolution 30 nba jerseys,2012 stanley cup nhl jersey,
We are professional jerseys manufacturer from china,wholesal.cheap nike nfl jerseys, mlb jerseys, nhl jerseys,nba jerseys and shoes. www.yourjerseyhome.com
Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:32:19 PM
Comment:
[/pre]Cheap NFL,NBA,MLB,NHL
[url=http://www.jersey2shop.com/]Jerseys From China[/url]
[url=http://www.jersey2shop.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jersey2shop.com/]cheap China Jerseys[/url]
[url=http://www.jersey2shop.com/]Sports Jerseys China[/url]
[url=http://www.jersey2shop.com/NFL-Jerseys-c68/]NFL Jerseys China[/url]
[url=http://www.jersey2shop.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
NHL Jerseys China
[url=http://www.jersey2shop.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[/pre]
[pre]We Are Professional China jerseys Wholesaler
[url=http://www.cheapjersey2store.com/]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url= http://www.cheapjersey2store.com/]2012 mlb all atar jerseys[/url]
[url= http://www.cheapjersey2store.com/ [/url]Cheap China Wholesael[/url]
[url= http://www.cheapjersey2store.com/]Wholesale jerseys From China[/url]
[url=http://www.cheapjersey2store.com/]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.cheapjersey2store.com/]Jerseys From China[/url]
[url=http://www.cheapjersey2store.com/NFL-Jerseys-c68]NFL jerseys China[/url]
[url=http://www.cheapjersey2store.com/NHL-Jerseys-c96/]NHL Jerseys China[/url]
[url=http://www.cheapjersey2store.com/NBA-Jerseys-c77/]NBA Jerseys China[/url]
[url=http://www.cheapjersey2store.com/MLB-Jerseys-c94/]MLB Jerseys China[/url]
[url= http://www.cheapjersey2store.com/]China Jerseys[/url],Free Shipping
[/pre]
[/pre]
We are professional jerseys manufacturer from china,wholesal
sports [url= http://www.jerseycaptain.com/]cheap jerseys sale online [/url]
[url= http://www.jerseycaptain.com/]2012 nike nfl Jerseys[/url]
[url=http://www.jerseycaptain.com/NFL-Jerseys-c68]cheap NFL jerseys China[/url]
[url=http://www.jerseycaptain.com/NHL-Jerseys-c96/]NHL Jerseys C
Title: Database Class in Classic ASP   
Name: BENITO NAVARRO MARTINEZ
Date: 2010-10-15 2:13:10 PM
Comment:
¡Excellent, good job!
Title: wonder code   
Name: haruto kurasakii
Date: 2009-11-23 11:02:00 AM
Comment:
this the very good code!!!
i wana cry! T-T
why??
i don´t ASP and
code im happy!!
Title: Remove comment lines   
Name: aravinda
Date: 2009-10-23 3:13:44 AM
Comment:
Remove comment lines this..

‘Set db = New dbaccess
..
...
’Call db.DBClose()
Title: well done!   
Name: ashok
Date: 2008-06-19 5:17:57 AM
Comment:
This is wonderful code! Though it could be more readable, it covered everything I needed to create my database class for my website! Thanks so much for posting it!
Title: It is correct   
Name: Jim
Date: 2008-04-23 3:01:24 AM
Comment:
Baldev Rawat,,

The code is correct as posted.
Title: Very well done!   
Name: stars2night
Date: 2007-12-19 2:35:47 PM
Comment:
This is wonderful code! Though it could be more readable, it covered everything I needed to create my database class for my website! Thanks so much for posting it!
Title: Re : Ed   
Name: Martin
Date: 2007-03-23 5:56:43 AM
Comment:
You can use the IsArray() function to check if the array exist ;o)
Title: bass   
Name: saamy
Date: 2007-02-20 6:51:27 AM
Comment:
very nice work.thank you , will be useful for all
Title: Re: Ed   
Name: Justin
Date: 2006-02-15 2:18:59 AM
Comment:
I think the issue you are running into is the error handling for the returned array outside of the class.

If you are using the example code, you should notice I did not include error handling for uninitialized arrays (null recordsets). If the array is uninitialized and you try to erase the array, it will raise an error.
Title: Great   
Name: Ed
Date: 2006-02-14 6:32:03 PM
Comment:
Works ok,
However if no results returned it should return false, instead the script fails. Anyone have a clue?
Title: great   
Name: smail
Date: 2005-06-11 7:02:59 AM
Comment:
goooooooooooob & tk u very much
Title: Database Class Code   
Name: Rudi
Date: 2005-04-03 4:57:49 PM
Comment:
After spending 6 hours trying to find the correct syntax for setting up reads from an Access database, I came accross this code sample. What a find! Of all the code samples I tried (an could not get to work), this was that only one that worked right away (just adding my variable names). Thanks a million!
Title: great   
Name: ben
Date: 2005-03-09 4:51:20 PM
Comment:
great little pioece of code, thanks very much






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-03-24 3:59:37 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search