homecode zoneQuick Codedownloadssubmit codeabout usfeedbackscontact ussitemap
 
Beginners
General
Advance Scripting
Database
Function
Variables
Response Object
Request Object
ASP Components
Using Database
Pro - Languages
C/C++
Java
Visual basic
asp
vb.net 2003/2005
asp.net
Quick Code
SUBMIT CODE
Mail List
LINKS
Submit LINK
 
ProgrammerGuide.net - ASP Using Database.
Home » ASP Zone » ASP-Using Database

Why use Database?

Database, like text files, cookies, and the Session and Application objects, have their time and place. If you need to store a lot of information over a long period of time, databases are clearly the way to go. An entire branch of computer science is dedicated to study database design and how to create efficient database system. Due to this intensive research into database system, a database is the most effective and efficient method of storing method.

Working with Database Using ASP

Now that we've discussed the importance of using database, it's time to get your hand dirty and begin working with them! We will start with simple example using Microsoft Access 2000 or 2002 database for query through ASP page. Create a database named 'FriendsContactInfo.mdb', and create a table named 'Friends' that contains the following columns.

Field Name Data Type
Name Text
Street Address Text
City Text
State Text
Zip Text
Phone Number Text

Now add some information for friends of yours into table. You have now created a database with one table using Access 2000 or 2002 . Now that you have a database, you can query it through ASP page. To communicate with a database through an ASP page, you need to use the ActiveX Data Object component (ADO). This Component provides a number of objects that can be used to connect to and query a database. When you want to retrieve information from a database, you need to use a two-step process.

1. Establish a connection to the database.
2. Query the database, asking the database for the information you are interested in.

The following listing contains the code for display information of friends you added in database. It uses the Connection and Recordset objects to display the information from Friends tables.

<!--#Include Virtual="/adovbs.inc">
<%
'Open up a connection to our Access Database
'We will use a DSN-Less connection.
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "DRIVERS={Microsoft Access Driver (*.mdb)};" & _
"DBQ=c:\database\FriendsContactInfo.mdb"
objConn.Open

'Create Recordset object instance and retrieve the information
Dim objRS
Set objRS = Server.CreateObject("ADODB.RecordSet")
objRS.Open "Friends", objConn, , adCmdTable

'Display the content of the Friends table
Do while Not objRS.EOF
Response.Write "<B>" & objRS("Name") & "</B><BR>"
Response.Write objRS("Street Address") & "<BR>"
Response.Write objRS("City") & ", " & objRS("State") & "<BR>"
Response.Write objRS("ZIP") & "<BR>"
Response.Write objRS("Phone Number") & "<p><HR><P>"

'Move to the next row in the friends table
objRS.MoveNext
Loop

'Clean up our ADO objects
objRS.Close
Set objRS = Nothing

objConn.Close
Set objConn = Nothing
%>

« Previous 1 2 3 Next »

Viewers: 17
Visitors:127291
Last Updated on:Sunday, January 17, 2010
Home | About us | Feedback | Contact us | Sitemap
Copyright © 2008 ProgrammerGuide.net. All rights reserved.
This site is best viewed in IE 6.0, Firefox 1.5 and above with screen resolution of 1024 X 768.