ASP Database Fundamentals (Part 1)

Ok. So you've learned ASP and you can make some pretty cool and interactive web pages. But sooner or later you are going to have a need to use ASP to access a database of some sorts.

Written by
Rating: 5 stars5 stars5 stars5 stars5 stars / 102
August 13, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


There are many applications that would benefit from database integration. Examples might include message forums, product listings, and user sign-in databases among other things.

The purpose of these articles is to introduce you to the base concepts of accessing and utilizing a database with ASP. With this knowledge, you should be able to begin creating dynamic data-driven websites using the ASP architecture.

What's All The Ado About ADO?

The default method to communicate with a database from within ASP is to use ADO. ADO stands for ActiveX Data Objects, and is an application program interface from Microsoft that lets a programmer writing Windows applications get access to a relational or non-relational database from both Microsoft and other database providers.

ADO can be utilized in many different development languages for Windows, including Visual Basic and Visual C++. As an example, you might using ADO to connect your Visual Basic application to an Oracle Database, or your Visual C++ application to SQL Server. In our case, we will focus on its use within ASP.

In addition, in our examples we will utilize a Microsoft Access database as the back-end data repository. There are many web hosting providers available that will host your ASP websites, but most of them charge extra for usage of a robust database like SQL2000. On the other hand, most of these providers offer the usage of MS Access databases at a very small fee, if any at all.

Connecting To A Database

When accessing a database using ADO, the first thing you need to do is make a connection. Creating a connection to a database can sometimes be the hardest part of building an ASP application. When you create a connection object, you can reference the database one of two ways. The first way is to fully qualify the database name, the driver, and location within the connection string for the database. The second is to create a DSN (Data-source name) for the database and refer to it by name in the connection string.

For example, connecting to an access database using a fully qualified connection string may look like this:

 <%
ConnectionString "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" "C:\database\mydb.mdb"
%>

Being that information about database driver, name, and location are specified when a DSN is created in the system control panel, you only need to specify the DSN name when utilizing it. For example, to reference a DSN called "MyDB", the connection information would look like this:

 <%
ConnectionString "DSN=MyDB"
%>

Establishing The Connection

ADO is an object-oriented programming interface. In layman's terms, this means you will be working with different "objects" in order to communicate with the database. The first of these objects you will work with is the Connection Object.

Lets take a look at a code sample that creates a database connection object:

 <%
ConnectionString "DSN=MyDB"

set conn server.createobject("adodb.connection")
conn.open ConnectionString
%>

In the above example, you can see that we create an object called "conn", which is defined as an ADO connection. Once we have an object for the connection, we attempt to open that connection using the open method of the object.

One thing I'd like to mention here is you will see that I am referencing the "connection" object of the "adodb" class in my createobject method. By default that statement will fail, since IIS does not know that that object is or how to make it. There are two ways to make this information known to IIS.

The first is to create an object reference to the ADO library inside your GLOBAL.ASA file. See my article on the GLOBAL.ASA to see how this is done.

Secondly, you can use the ADOVBS.INC module. This is an include file that comes with IIS, that you can include in any ASP page. It contains the appropriate declarations to be able to used the ADO objects within your code.

Closing The Connection

When you are finished with the reference to the database connection object, it is important to correctly close and destroy it. Not doing so can potentially cause issues on your web server, due to memory resources that have been allocated but not properly released.

Follows is an acceptable method for closing and destroying a ADO connection object:

<%
Conn.close
Set conn 
nothing
%> 

As you can see, the first thing we did was close the actual connection to the database. Next, we set the object to a value of "nothing", which destroys the actual object reference and releases the memory back to the operating system.

What's Next?

Once you have established your connection to the database, you are able to begin putting the pieces in place to add information to the database, or to search and extract existing data for presentation on your website.

In the next article in this series, we will go into recordset objects, and how you use them to pull information out of your database.


About the author:
Rich Smith owns and operates Jamsoft Development, a programming firm who specializes in custom systems for small businesses.
blog comments powered by Disqus
ASP ARTICLES

- Using MySQL with ASP
- ADO for the Beginner
- ADO.NET 101: Data Rendering with a DataGrid ...
- Introducing SoftArtisans OfficeWriter 3.0 En...
- Getting Remote Files With ASP
- The Real Basics of Functions in ASP
- Enhancing Readability with ASP
- Mimicking PHP's String Formatting Functions
- Windows Server Hacks 12, 77, and 98
- How to Sort a Multi-Dimensional Array
- Developing an Information Management Tool wi...
- What are Active Server Pages?
- Getting Remote Pages with ASP
- FTP’ing Files with ASP
- Apply Single-Sign-On to Your Application

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials