iTech College - Internet Technology, Online Education, E-Learning, Technician Training, Tech Tutorials, Distance Education, Virtual Learning ASP Course: Use Database - www.itechcollege.com ASP Course: Use Database - www.itechcollege.com
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
C++
ASP.NET
SQL
AJAX
FTP
XML

courses > ASP > Use Database

Use Database

First you need to create a database. Let's use MS Access. Make sure you have MS Access installed on your computer and you know how to configure it.

in Windows 95/98. you should go into the ODBC32 icon in your control panel, and add MS Access Drive on System DSN. Then create a database and a table. You must give a name to your database and to the table. Let's name the database "my_db.mdb", name the table "members" and the path to this database is:

C:\Inetpub\wwwroot\my_db.mdb

Let's design the table something like this:

userId user pass page
00234578 John John123 john.asp
-11350986 Mike Mike456 mike.asp
98782345 Joe Joe789 joe.asp
... ... ... ...

To open the database, we need the following code.

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Driver={Microsoft Access Driver (*.mdb)};
DBQ=C:\Inetpub\wwwroot\my_db.mdb"

Now you have established a connection to the database. To get data from the database, we can use SQL.

The next statement is combined ASP code and SQL:

sqlStr = "select * from members"
set RS = Conn.Execute(sqlStr)

You can write records from the database to your web browser;

while not RS.eof
Response.write "User name: " & RS("user") & "<br>"
Response.write "Password: " & RS("pass") & "<br>"
Rs.movenext
wend

"eof" stands "end of file". So the meaning of the above code is: if the search RS does not reach the end of the table, keep writing the records on web browser. When you execute the script, all the records of "user" and "pass" will be retriaved. The below is a complete ASP login page using database. Let's save it as second.asp:

<%
u=request("user")
p=request("pass")

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Driver={Microsoft Access Driver (*.mdb)};
DBQ=C:\Inetpub\wwwroot\my_db.mdb"

sqlStr = "select * from members where "
sqlStr = sqlStr & "user='& u &"' and pass='& p &"'"
set RS = Conn.Execute(sqlStr)

if not RS.eof then
Response.redirect " & RS("page")
else
Response.write "Sorry, invalid login"
end if
%>

The basic idea in this script is that when an user login from first.asp, the query strings of user name and password are sent to second.asp. The ASP opens database and selects records according to the query strings. If the records are fond, the ASP redirects page to a specific page. If there are no records match the query strings, then display a message and tell the user that the login was invalid.



  Back to Index Page
 Next Page:  

Introduction
ASP Tags and Include File
Response and Request
If Then
For Loop
Redirect
Sessiont
Use Database


Online Education, Online Training for Your Job, Distance Education, E-learning Programs for Your Career!Online Colleges, Online Universities, E-learning & Distance Education for Your Career! Easy Job Training & Online Courses.


   Email A Friend  
 
Earn Online Degree
for Your Job
I found a job after I earned an online degree!
Online classes for finding a Job. Online College, Online University, Degrees, E-learning school, online certificate Earn your
online degree now!
Online Training
for Good Job
Why I work so hard but earn so little? Why I cannot find a high paying job?
Online training for a Good Jobs. Online Degree, E-learning school, online certificate Go to
online school now!

Copyright ©2024, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech