Preparation
For this example, we are going to use a MS SQL Server database. This doesn't
mean you have to, as the SQL statements will work whatever the database; you'll
just need to change the connection code.
Before we start writing any ASP code, create a new MS SQL Server database called
testdb, and then create a new table in it called members. Add the
following columns (we've included both the MS SQL Server, and the Access descriptions)
|
Name
|
SQL Server Data Type
|
Access Data Type
|
Notes
|
| id |
int (AutoIncrement=True) |
AutoNumber |
Primary Key |
| username |
varchar (20) |
Text (FieldSize=20) |
Unique |
| password |
varchar (20) |
Text (FieldSize=20) |
|
Now, we'll create a short ASP script that connects to the database. Save it
as inc-dbconnection.asp.
|
inc-dbconnection.asp
<%
Dim objConn
'create an ADO connection object
Set objConn = Server.CreateObject("ADODB.Connection")
'open the connection to the database
'sqlservername = the name of the SQL server (if used)
'accessdb = the path to the access db from this script
'username = username to connect to the db
'password = password to connect to the db
'// Use this for SQL Server
objConn.Open "Driver={SQL Server};Server=sqlservername;" &
_
"UID=username;PASSWORD=password;DATABASE=testdb;"
'// Use this for an Access 2000 database
objConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
Server.MapPath("accessdb")
'// Use this for an Access database (earlier than 2000)
objConn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" &
Server.MapPath("accessdb")
'we are now connected to the database
%>
|