<%
'this page demonstrates a dnsless connection
to an access database Const adOpenForwardOnly
= 0
Const adLockReadOnly = 1
'we will pull out the records where
the persons name is = 'Nathan'
'this is asuming you have an access
database in the root directory of
your website folder called customers.mdb
'please make sure you change the directory
of the database before making this
code live.
'The TableName we will be usiong is
called CustomerTable
'the fields we will be using are Name,Address,PhoneNumber,Town,ID
(which is your autonumber and primary
key)
'a good connection is recommended
like this
ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA
SOURCE=" & Server.MapPath("/customers.mdb")
'Response.Write ConnectionString
'a bad connection is not recommended
like this
'ConnectionString = "DRIVER={Microsoft
Access Driver (*.mdb)};BDQ="
& Server.MapPath("/customers.mdb")
Sql = "Select * From CustomerTable
Where Name = 'Nathan'"
Set rsName = Server.CreateObject("ADODB.Recordset")
'open up the recordset. You can only
move forward in the recordset and
you cant run updates from it
'this is one of the fastest ways to
open up a recordset although dealing
with tables with only about 500 -
1000 records in it it wont make much
of a difference
rsName.Open Sql,ConnectionString,adOpenForwardOnly,adLockReadOnly
'%>
<!-- build up our display table
-->
<table border="0" width="50%"
align="center" bgcolor="black">
<tr>
<td bgcolor="lightblue"
nowrap="nowrap"><font
face="verdana" size="2"><b>ID</b></font></td>
<td bgcolor="lightblue"
nowrap="nowrap"><font
face="verdana" size="2"><b>Name</b></font></td>
<td bgcolor="lightblue"
nowrap="nowrap"><font
face="verdana" size="2"><b>Address</b></font></td>
<td bgcolor="lightblue"
nowrap="nowrap"><font
face="verdana" size="2"><b>Phone
Number</b></font></td>
<td bgcolor="lightblue"
nowrap="nowrap"><font
face="verdana" size="2"><b>Town</b></font></td>
</tr>
<%
'loop through the record till all
have been displayed
'rsName.EOF will = true when you are
at the of the recordset
Do Until rsName.EOF
'to get the values out of our recordset
there are 2 ways, not defaulting and
defaulting
'1st way is rsName.Fields("Name").Value
'2nd Way is rsName("Name")
'in this example we will use defaulting
%>
<!-- start <%=rsName("Name")%>'s
Record -->
<tr>
<td bgcolor="white" nowrap="nowrap"><font
face="verdana" size="2"><%=rsName("ID")%></font></td>
<td bgcolor="white" nowrap="nowrap"><font
face="verdana" size="2"><%=rsName("Name")%></font></td>
<td bgcolor="white" nowrap="nowrap"><font
face="verdana" size="2"><%=rsName("Address")%></font></td>
<td bgcolor="white" nowrap="nowrap"><font
face="verdana" size="2"><%=rsName("PhoneNumber")%></font></td>
<td bgcolor="white" nowrap="nowrap"><font
face="verdana" size="2"><%=rsName("Town")%></font></td>
</tr>
<!-- end <%=rsName("Name")%>'s
Record -->
<%
'move to the next record in our recordset
rsName.MoveNext
Loop
%>
</table>
<!-- end display table -->
<%
'close recordset because we no longer
need it
rsName.Close
'release objects when you are finished
with them
Set rsName = Nothing
%> |