Академический Документы
Профессиональный Документы
Культура Документы
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 2 de 142
</body></html>
The form responder dblistrespond.asp is:
<html><head>
<TITLE>dblistrespond.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
my_author=request.form("authorname")
%>
You choose <%=my_author%><br>Thanks!<br>
</body></html>
<html><head>
<TITLE>subdblist.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form>
<%
theDSN="DSN=student;uid=student;pwd=magic"
call query2list("select distinct city from publishers","cy",theDSN)
call query2list("select distinct state from publishers","st",theDSN)
call query2list("select distinct zip from publishers","zp",theDSN)
%>
</form>
<!--#include virtual="/learn/test/subdblist.inc"-->
</body></html>
<%sub query2list(myquery,myname,myDSN)
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(myquery)
%>
<Select name="<%=myname%>">
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 3 de 142
<%
do while not rstemp.eof
thisfield=trim(RStemp(0))
if isnull(thisfield) or thisfield="" then
' ignore
else
response.write "<option>" & thisfield & "</option>"
end if
rstemp.movenext
loop
%>
</select>
<%rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
<html><head>
<TITLE>subdblistbest.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form>
<%call db2list("^","select distinct city from publishers^city^New York")%>
<%call db2list("^","select distinct state from
publishers^state^NY^table^DSN=student;uid=student;pwd=magic")%>
<%call db2list("^","select distinct zip from publishers^Zip Code^^table")%>
</form>
<!--#include virtual="/learn/test/lib_dblistbest.asp"-->
<%
sub db2list(mydelim,myparm)
dim myparameters
myparameters=SPLIT(myparm,mydelim)
parmcount=ubound(myparameters)
myquery=myparameters(0)
label=myparameters(1)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 4 de 142
default=myparameters(2)
if parmcount>2 then
format=lcase(myparameters(3))
end if
if parmcount>3 then
connstring=myparameters(4)
else
connstring="DSN=Student;uid=student;pwd=magic"
end if
set conntemp=server.createobject("adodb.connection")
conntemp.open connstring
set rstemp=conntemp.execute(myquery)
If format="table" then%>
<table width="100%" border="0" cellspacing="1"><td>
<%end if
response.write label
If format="table" then%>
</td><td>
<%end if%>
<Select>
<option value="<%=default%>" selected><%=default%></option>
<%
do while not rstemp.eof %>
<option><%=RStemp(0)%></option>
<%
rstemp.movenext
loop
conntemp.close
%>
</select>
<%If format="table" then%>
</td><tr></table>
<%end if
end sub%>
Custom Security/Authentication #1
You can limit access to specific pages in your website using several methods documented at
http://www.activeserverpages.com/learn/authenticate.asp Here we will demonstrate how to use custom
authentication and also cover session and application issues. You can try our authentication example by:
Now if you login at test/securitylogin.asp. Sample logins that will work because of the database
data are:
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 5 de 142
user=chaz2, password=chaz2, securitylevel=2
You can also try logging out at test/securitylogout.asp which will put you back to square one. No
security level will exist then.
The next page details the source code for all scripts needed to implement our example but here is the list:
the login screen where someone can enter username/password and
securitylogin.asp
confirm security level. It is a form that submits to
securityloginrespond.asp
securityloginrespond.asp
the screen to abandon someone's username/password and security
securitylogout.asp
level
securitylevel1required.asp which can be included on individual pages to limit access to people with
securitylevel2required.asp that security level, i.e/:
securitylevel3required.asp <!--#include file="securitylevel1required.asp"-->
securitytestlevel1.asp
which demonstrate how security is implemented. These scripts cannot
securitytestlevel2.asp
be seen unless you login.
securitytestlevel3.asp
which anyone attempting to access a page without appropriate security
securitynotallowed.asp
level is redirected to.
a 3 column Access database: username, password, security level.
Sample data is:
user=chaz, password=chaz, securitylevel=1
/learn/test/customsecurity.mdb user=chaz2, password=chaz2, securitylevel=2
user=chaz3, password=chaz3, securitylevel=3
Download Database
In a production application, this database would be located
OUTSIDE of the web structure (and accessed by DSN) so it could
never be downloaded by a user.
Custom Security/Authentication #2
To implement custom security via a database, we use the following scripts that we will present the source
code for:
<html><head>
<title>securitylogin.asp</title>
</head><body bgcolor="#FFFFFF">
Sign In Page:<p>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 6 de 142
<input type="submit"><input type="reset">
</form></body></html>
<html><head>
<TITLE>securityloginrespond.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
dbname="/learn/test/secret/customsecurity.mdb"
myname=request.form("username")
mypassword=request.form("userpassword")
set conntemp=server.createobject("adodb.connection")
set rstemp=conntemp.execute(SQLTemp)
If rstemp.eof then%>
<%response.end
end if
If rstemp("Password")=mypassword then
session("name")=rstemp("user")
session("securitylevel")=rstemp("securitylevel")
else%>
Password Unrecognized<br>
<%response.end
end if
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 7 de 142
rstemp.close
conntemp.close
set rstemp=nothing
set conntemp=nothing
%>
</body></html>
<html><head>
<title>securitylogout.asp</title>&
<body>
<%
session.abandon
%>
</body>
</html>
<%
response.expires=0
if session("securitylevel")>0 then
' nothing to do
else
response.redirect "securityunauthorized.asp"
end if
%>
<html><head>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 8 de 142
<title>unauthorized</title>&
<body>
</body></html>
<!--#include file="securitylevel1required.asp"-->
<html><head>
</head><body>
</body>
</html>
you want all accounts of every type in one userbase, specifically the NT user account database, for
administrative reasons.
You want to be able to add and modify users from ASP and don't want the ASP pages to have SysAdmin
priviledges
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 9 de 142
You are concerned about performance. In addition to the speed associated with filter based solutions,
AuthentiX is unique in that it does not impersonate an NT account to grant access, eliminating the CPU-
expensive call to LogonUser on every request.
You want to authenticate multiple IIS servers against a single ODBC machine on the LAN.
you need to protect all content in a directory: htm, asp, gif, jpg, zip, and so on.
AuthentiX is a fast, filter based third party tool for IIS authentication developed by Flicks
Software (me).
It allows you to protect content directories and individual files by asking for usernames and
passwords held separately from the Windows NT usernames and passwords, ensuring the the
security of your NT accounts.
Definitions
ODBC = Open Database Connectivity.
Create a user. From the main AuthentiX dialog, click the Users button, then Add. Type a username and
password and click OK. The user will be added to the User List. Click OK.
Create a group. From the main AuthentiX dialog, click the Groups button, then Add. Type a Groupname,
click on a user (to highlight it) listed in the Non-Members list box, and click Add. The user will be moved
to the Members list box. Click OK. You should now see the group in the group list. Click OK.
Protect a directory. From the main AuthentiX dialog, click the Access button, then Add. Click the Browse
button and select a directory that is part of your web directories, and that you would like to protect. Click
the By Group button and add the group you created in the previous step. Click OK. You should now see
that the group is protecting that directory. Verify that the group is protecting the desired directory and click
OK twice
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 10 de 142
Using a browser, go to the URL that the directory is accessed from using IIS. It should prompt you for your
username and password.
Type the username and password and you should be granted access.
You can see how to set up ODBC and other advanced options by downloading the online
Windows help file or checking out the online Guided Tour. Because the pace of enhancements
and improvements to this product sometimes outstrips the documentation, you can find out more
by working with the free evaluation download.
Instr(string,searchstring)
Mid(string,start,length)
Mid(string,start)
Trim(string)
<html><head>
<title>citystatezip.asp</title>
</head><body bgcolor="#FFFFFF">
</form>
</body></html>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 11 de 142
San Fransisco, CA xxxxx
Pike's Peak, CO xxxxx
and the program can use these functions to separate the data.
<html><head>
<title>citystateziprespond.asp</title>
</head><body>
<%
alldata=request("csz")
IF instr(alldata,",")=0 THEN%>
<%response.end
END IF
findcomma=instr(alldata,",")
city=mid(alldata,1,findcomma-1)
leftover=trim(mid(alldata,findcomma+1))
findspace=instr(leftover," ")
state=mid(leftover,1,findspace)
leftover=mid(leftover,findspace+1)
zip=leftover
%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 12 de 142
</body></html>
SPLIT(string,delimiter)
is a function that returns an array with as many elements as are separated by delimiters within the string.
The user can enter a state or multiple states. We write the program utilizing SPLIT to
demonstrate how this can be used.
statesplit.asp
Test the Script --> /learn/test/statesplit.asp
<html><head>
<title>states.asp</title>
</head><body bgcolor="#FFFFFF">
</form>
</body></html>
statesplitrespond.asp
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
<html><head>
<title>statesrespond.asp</title>&
<body>
<%
myDSN="DSN=student;uid=student;pwd=magic"
public allstates
tempinput=request("states")
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 13 de 142
allstates=split(tempinput,",")
maxcounter=ubound(allstates)
thisstate=allstates(counter)
NEXT
Call Query2Table(mySQL,myDSN)
%>
</body></html>
REPLACE(string,oldvalue,newvalue)
results in a string that has all occurences of oldvalue replaced with a newvalue.
The user can enter a state or multiple states. We write the program utilizing REPLACE to
demonstrate how this function can be used.
statereplace.asp
Test the Script --> /learn/test/statereplace.asp
<html><head>
<title>statereplace.asp</title>
</head><body bgcolor="#FFFFFF">
</form>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 14 de 142
</body></html>
statereplacerespond.asp
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
<html><head>
<title>statesrespond.asp</title>&
<body>
<%
myDSN="DSN=student;uid=student;pwd=magic"
tempinput=request("states")
response.write mySQL
Call Query2Table(mySQL,myDSN)
%>
</body></html>
JOIN(array,delimiter)
The user can enter a state or multiple states. We write the program using the JOIN function to
demonstrate how this function can be used.
statejoin.asp
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 15 de 142
Test the Script --> /learn/test/statejoin.asp
<html><head>
<title>statejoin.asp</title>
</head><body bgcolor="#FFFFFF">
</form>
</body></html>
statejoinrespond.asp
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
<html><head>
<title>statejoinrespond.asp</title>&
<body>
<%
myDSN="DSN=student;uid=student;pwd=magic"
public allstates
tempinput=request("states")
allstates=split(tempinput,",")
response.write mySQL
Call Query2Table(mySQL,myDSN)
%>
</body></html>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 16 de 142
Arrays to store data Part #1 by Charles Carroll
Here would be a code sample without arrays:
<html><head>
<title>arraysnot.asp</title>
</head><body bgcolor="#FFFFFF">
<%
dim x,y,z
x=7
y=20
z=x+y
response.write z
%>
</body></html>
<html><head>
<title>arrays.asp</title>
</head><body bgcolor="#FFFFFF">
<%
dim allstuff(3)
allstuff(0)=7
allstuff(1)=20
allstuff(2)=allstuff(0)+allstuff(1)
response.write allstuff(2)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 17 de 142
%>
</body></html>
http://www.activeserverpages.com/learn/subdates.asp
has a good example of arrays in a practical context.
<html><head>
<title>arraysredim.asp</title>
</head><body bgcolor="#FFFFFF">
<%
dim a_array(100)
x=100
dim my_array(x)
%>
</body></html>
Assigning an array size with a variable produces an error unless the Redim command is
used.
Test the Script --> /learn/test/arraysredimcorrect.asp
<html><head>
<title>arraysredimcorrect.asp</title>
</head><body bgcolor="#FFFFFF">
<%
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 18 de 142
dim my_array()
x=100
my_array(20)="Hi!"
lowcount=lbound(my_array)
highcount=ubound(my_array)
response.write "lowcount=" & lowcount & ";highcount=" & highcount & "<p>"
next
%>
</body></html>
<html><head>
<title>arraysload.asp</title>
</head><body bgcolor="#FFFFFF">
<%
dim my_months(13)
my_months(0)=""
my_months(1)="Jan"
my_months(2)="Feb"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 19 de 142
my_months(3)="Mar"
my_months(4)="Apr"
my_months(5)="May"
my_months(6)="Jun"
my_months(7)="Jul"
my_months(8)="Aug"
my_months(9)="Sep"
my_months(10)="Oct"
my_months(11)="Nov"
my_months(12)="Dec"
lowcount=lbound(my_months)
highcount=ubound(my_months)
response.write "lowcount=" & lowcount & ";highcount=" & highcount & "<p>"
next
%>
</body></html>
<html><head>
<title>arraysloadbest.asp</title>
</head><body bgcolor="#FFFFFF">
<%
dim my_months
my_months=array("Jan","Feb","Mar","Apr", _
"May","Jun","Jul","Aug", _
"Sep","Oct","Nov","Dec")
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 20 de 142
lowcount=lbound(my_months)
highcount=ubound(my_months)
response.write "lowcount=" & lowcount & ";highcount=" & highcount & "<p>"
next
%>
</body></html>
<html><head>
<title>dictionarybasics.asp</title>
</head><body bgcolor="#FFFFFF">
<%
set mysample=server.CreateObject("Scripting.Dictionary")
next
set mysample=nothing
%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 21 de 142
</body></html>
<html><head>
<title>dictionaryarrays.asp</title>
</head><body bgcolor="#FFFFFF">
<%
dim people(3)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 22 de 142
response.write whatever & "="
next
for counter=0 to 2
set people(counter)=nothing
next
%>
</body></html>
The dictionary items can be removed individually unlike array elements. See:
http://www.activeserverpages.com/docs --> vbscript docs for all the rest of the details about the
dictionary object.
<HEAD><TITLE>subdates.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<form action="subdatesrespond.asp">
Depart:
<%call showdateform("depart")%>
<br>
Arrive:
<%call showdateform("arrive")%>
</form>
<%
sub showdateform(mylistname)
call showmonth(mylistname)
call showday(mylistname)
call showyear(mylistname)
end sub
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 23 de 142
%>
<%sub showmonth(listname)%>
<select name="<%=listname%>month">
<%
dim monthname
monthname=array("Jan","feb","Mar","Apr", _
"May","Jun","Jul","Aug", _
"Sep","Oct","Nov","Dec")
for counter=0 to 11
temp=monthname(counter)
next%>
</select>
<%end sub%>
<%sub showday(listname)%>
<select name="<%=listname%>day">
<%for counter=1 to 31
next%>
</select>
<%end sub%>
<%sub showyear(listname)%>
<select name="<%=listname%>year">
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 24 de 142
next%>
</select>
<%end sub%>
</BODY></HTML>
<HEAD><TITLE>subdatesrespond.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
dday=request.querystring("departday")
dmonth=request.querystring("departmonth")
dyear=request.querystring("departyear")
aday=request.querystring("arriveday")
amonth=request.querystring("arrivemonth")
ayear=request.querystring("arriveyear")
If isdate(departdate) then
else
end if
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 25 de 142
If isdate(arrivedate) then
else
end if
%>
</BODY></HTML>
<HEAD><TITLE>subdbtable.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
<HEAD><TITLE>subdbtable2.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 26 de 142
</BODY></HTML>
<HEAD><TITLE>subdbtable3.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
<HEAD><TITLE>subdbtable4.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
<HEAD><TITLE>subdbtable5.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 27 de 142
mySQL="select * from title_author"
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
<HEAD><TITLE>subdbtable6.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
<%
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<td><b><%=rstemp(i).name%></B></TD>
</tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 28 de 142
do while not rstemp.eof %>
<tr>
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
<html><head>
<TITLE>subdblist.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form>
<%
theDSN="DSN=student;uid=student;pwd=magic"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 29 de 142
call query2list("select distinct city from publishers","cy",theDSN)
%>
</form>
<!--#include virtual="/learn/test/subdblist.inc"-->
</body></html>
<%sub query2list(myquery,myname,myDSN)
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(myquery)
%>
<Select name="<%=myname%>">
<%
thisfield=trim(RStemp(0))
' ignore
else
end if
rstemp.movenext
loop
%>
</select>
<%rstemp.close
set rstemp=nothing
conntemp.close
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 30 de 142
set conntemp=nothing
end sub%>
<HEAD><TITLE>subreusabledataform.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/subflexible.asp"-->
<%
%>
</BODY></HTML>
<HEAD><TITLE>subreusabledatalist.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/lib_subflexible.asp"-->
<%
%>
</BODY></HTML>
<HEAD><TITLE>subreusabledatatable.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 31 de 142
<!--#include virtual="/learn/test/subflexible.asp"-->
<%
%>
</BODY></HTML>
<HEAD><TITLE>subreusableform.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/subflexible.asp"-->
<%
%>
</BODY></HTML>
<%
dim fieldnames
fieldnames=false
sub query2list(myquery,listname)
htmlend="</select>"
rowstart="<option>"
rowend="</option>"
fieldstart=""
fieldend=""
call query2html(myquery)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 32 de 142
end sub
sub query2table(myquery)
htmlstart="<table border=1>"
htmlend="</table>"
rowstart="<tr>"
rowend="</tr>"
fieldstart="<td valign=top>"
fieldend="</td>"
call query2html(myquery)
end sub
sub query2form(myquery)
htmlstart=""
htmlend=""
rowstart=""
rowend="<hr>"
fieldstart=""
fieldend="<br>"
fieldnames=true
namestart=""
nameend=" = "
call query2html(myquery)
end sub
sub query2entryform(myquery)
htmlstart=""
htmlend=""
rowstart=""
rowend=""
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 33 de 142
fieldstart="%name% = <input type='text name='%name%' value='"
fieldend="' size='%size%'><br>"
fieldnames=false
namestart=""
nameend=" ="
call query2html(myquery)
end sub
sub query2html(inputquery)
set conntemp=server.createobject("adodb.connection")
conntemp.open "DSN=Student;uid=student;pwd=magic"
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1
redim fsa(howmanyfields)
redim fea(howmanyfields)
for i = 0 to howmanyfields
tempstart=replace(fieldstart,"%name%",rstemp(i).name)
tempend=replace(fieldend,"%name%",rstemp(i).name)
tempstart=replace(tempstart,"%size%",rstemp(i).actualsize)
tempend=replace(tempend,"%size%",rstemp(i).actualsize)
fsa(i)=tempstart
fea(i)=tempend
next
counter=0
do until rstemp.eof
for i = 0 to howmanyfields
if fieldnames=true then
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 34 de 142
end if
next
counter=counter+1
rstemp.movenext
if response.isclientconnected=false then
exit do
end if
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
response.write htmlend
end sub
%>
<html><head>
<TITLE>subdblistbest.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<form>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 35 de 142
<%call db2list("^","select distinct state from
publishers^state^NY^table^DSN=student;uid=student;pwd=magic")%>
</form>
<!--#include virtual="/learn/test/lib_dblistbest.asp"-->
<%
sub db2list(mydelim,myparm)
dim myparameters
myparameters=SPLIT(myparm,mydelim)
parmcount=ubound(myparameters)
myquery=myparameters(0)
label=myparameters(1)
default=myparameters(2)
if parmcount>2 then
format=lcase(myparameters(3))
end if
if parmcount>3 then
connstring=myparameters(4)
else
connstring="DSN=Student;uid=student;pwd=magic"
end if
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 36 de 142
set conntemp=server.createobject("adodb.connection")
conntemp.open connstring
set rstemp=conntemp.execute(myquery)
If format="table" then%>
<%end if
response.write label
If format="table" then%>
</td><td>
<%end if%>
<Select>
<%
<option><%=RStemp(0)%></option>
<%
rstemp.movenext
loop
conntemp.close
%>
</select>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 37 de 142
</td><tr></table>
<%end if
end sub%>
<!--#include virtual="/learn/test/lib_htmlstuff.asp"-->
<html><head>
<body>
<form action="lib_htmldemorespond.asp">
<%
response.write "<br>"
response.write "<br>"
Call Form_TextBox("City","cy",20,20,"")
response.write "<br>"
Call Form_TextBox("State","st",2,2,"")
response.write "<br>"
response.write "<br>"
%>
</form>
</body>
</html>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 38 de 142
The library that enables this (named lib_htmlstuff.asp) looks like:
<%
Response.Write( Text )
End Sub
'----------------------------------------------------------------
Response.Write( Text )
End Sub
'----------------------------------------------------------------
Response.Write( Text )
End Sub
'----------------------------------------------------------------
Form_Parm_Type "Hidden"
Form_Parm_Name Name
Form_Parm_Value Value
HTML ">"
End Sub
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 39 de 142
'----------------------------------------------------------------
HTML ">"
HTML "<strong>"
Display Label
HTML "</strong>"
HTML "</label>"
End If
End Sub
'----------------------------------------------------------------
Form_Parm_Type "text"
Form_Parm_Name Name
Form_Parm_Size Size
Form_Parm_ID Name
Form_Parm_MaxLength MaxLength
Form_Parm_Value Value
HTML ">"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 40 de 142
End Sub
'----------------------------------------------------------------
Form_Parm_Type "password"
Form_Parm_Name Name
Form_Parm_Size Size
Form_Parm_ID Name
Form_Parm_MaxLength MaxLength
Form_Parm_Value Value
HTML ">"
End Sub
'----------------------------------------------------------------
Form_Parm_Name Name
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 41 de 142
End If
End If
Form_Parm_ID Name
HTML ">"
Display Value
End If
HTML "</textarea>"
End Sub
'----------------------------------------------------------------
Form_Parm_Type "reset"
Form_Parm_Value Value
Form_Parm_Name Name
HTML ">"
End Sub
'----------------------------------------------------------------
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 42 de 142
Form_Parm_Type "button"
Form_Parm_Value Value
Form_Parm_Name Name
HTML ">"
End Sub
'----------------------------------------------------------------
Form_Parm_Type "submit"
Form_Parm_Value Value
Form_Parm_Name Name
HTML ">"
End Sub
'----------------------------------------------------------------
Form_Parm_Type "radio"
Form_Parm_Value Value
Form_Parm_Name Name
If Checked Then
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 43 de 142
HTML " checked "
End If
End If
HTML ">"
End Sub
'----------------------------------------------------------------
Form_Parm_Type "checkbox"
Form_Parm_Name Name
Form_Parm_Value Value
End If
End If
HTML ">"
'
End Sub
'----------------------------------------------------------------
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 44 de 142
Sub Form_Begin( Action )
HTML "action =" & Chr(39) & Action & Chr(39) & " "
HTML ">"
End Sub
'----------------------------------------------------------------
HTML "</FORM>"
End Sub
'----------------------------------------------------------------
HTML "border=" & Chr(39) & Border & Chr(39) & " "
End If
Form_Parm_Width Width
HTML ">"
End Sub
'----------------------------------------------------------------
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 45 de 142
Sub Form_Table_End()
HTML "</table>"
End Sub
'----------------------------------------------------------------
Form_Parm_Align Align
Form_Parm_VAlign VAlign
HTML ">"
End Sub
'----------------------------------------------------------------
HTML "</tr>"
End Sub
'----------------------------------------------------------------
Form_Parm_Width Width
Form_Parm_Align Align
Form_Parm_VAlign VAlign
HTML ">"
End Sub
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 46 de 142
'----------------------------------------------------------------
HTML "</td>"
End Sub
'----------------------------------------------------------------
Form_Parm_Name Name
Form_Parm_Size Size
If Multiple Then
End If
End If
HTML ">"
End Sub
'----------------------------------------------------------------
Form_Parm_Value Value
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 47 de 142
If Selected Then
End If
End If
HTML ">"
End Sub
'----------------------------------------------------------------
Sub Form_ComboBox_End()
HTML "</select>"
End Sub
'----------------------------------------------------------------
HTML "<title>"
Display Title
HTML "</title>&
End Sub
'----------------------------------------------------------------
HTML "<p align=" & Chr(39) & "center" & Chr(39) & ">"
Display Text
HTML "</p>"
End Sub
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 48 de 142
'----------------------------------------------------------------
HTML "<p align=" & Chr(39) & "left" & Chr(39) & ">"
Display Text
HTML "</p>"
End Sub
'----------------------------------------------------------------
HTML "<p align=" & Chr(39) & "right" & Chr(39) & ">"
Display Text
HTML "</p>"
End Sub
'----------------------------------------------------------------
Display " align=" & Chr(39) & "left" & Chr(39) & " "
Display " align=" & Chr(39) & "center" & Chr(39) & " "
Display " align=" & Chr(39) & "right" & Chr(39) & " "
Case Else
End Select
End If
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 49 de 142
End Sub
'----------------------------------------------------------------
Display " align=" & Chr(39) & "top" & Chr(39) & " "
Display " align=" & Chr(39) & "center" & Chr(39) & " "
Display " align=" & Chr(39) & "bottom" & Chr(39) & " "
Case Else
End Select
End If
End Sub
'----------------------------------------------------------------
Display " name=" & Chr(39) & Name & Chr(39) & " "
End If
End Sub
'----------------------------------------------------------------
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 50 de 142
Display " value=" & Chr(39) & Value & Chr(39) & " "
End If
End Sub
'----------------------------------------------------------------
Display " type=" & Chr(39) & TypeValue & Chr(39) & " "
End If
End Sub
'----------------------------------------------------------------
End If
End Sub
'----------------------------------------------------------------
Sub Form_Parm_ID( ID )
Display " id=" & Chr(39) & ID & Chr(39) & " "
End If
End Sub
'----------------------------------------------------------------
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 51 de 142
Sub Form_Parm_MaxLength( MaxLength )
End If
End Sub
'----------------------------------------------------------------
End If
End Sub
'----------------------------------------------------------------
End If
End Sub
'----------------------------------------------------------------
HTML "<a href=" & Chr(39) & HRef & Chr(39) & ">"
Display TextToDisplay
HTML "</a>"
End Sub
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 52 de 142
'----------------------------------------------------------------
HTML "<ul>"
HTML "<li>"
Form_Parm_Align "Left"
Form_Parm_VAlign "Bottom"
HTML ">"
HTML "</p>"
HTML "</li>"
HTML "</ul>"
End Sub
'----------------------------------------------------------------
'
' Standard constants from the MS web site but not built
'
Const TristateTrue = -1
Const TristateFalse = 0
Const TristateUseDefault = -2
'
FormatMoney = vbNullString
Else
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 53 de 142
, False, TristateTrue)
End If
'
End Function
'----------------------------------------------------------------
%>
<title>functionworkingdays.asp</title>
<body bgcolor="#FFFFFF">
<%
response.write "3 working days from today is " & dtaddWorkingDays(now(),3) &
"<p>"
%>
</body>
Dim dtEndDate
Dim iLoop
dtEndDate=DateAdd("ww",Int(nDaysToAdd/5),dtStartDate)
dtEndDate=DateAdd("d",1,dtEndDate)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 54 de 142
'If Saturday increment to following Monday
If WeekDay(dtEndDate)=vbSaturday Then
dtEndDate=DateAdd("d",2,dtEndDate)
End If
Next
dtAddWorkingDays=dtEndDate
End Function
%>
Eval function
Test the Script --> /learn/test/vbs5eval.asp
<html><head>
<title>vbs5eval.asp</title>
</head>
<body>
<%
x="2+2*3"
response.write eval(x)
%>
</body>
</html>
Classes:
Test the Script --> /learn/test/vbs5classes.asp
<html><head>
<title>vb5classes.asp</title>
</head>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 55 de 142
<body>
<%
Dim myCustomer
myCustomer.Add "Charles","Carroll"
myCustomer.EmailName = "charlescarroll@aspalliance.com"
myCustomer.CreditLimit = 5000
response.write myCustomer.FullName
Class Customer
Private nCreditLimit
Private strEmailName
EmailName = strEmailName
End Property
StrEmailName = strName
End Property
End Property
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 56 de 142
if s >= 0 then
nSalary = s
End If
End Property
Salary = nSalary
End Property
FirstName = First
LastName = Last
End Sub
RaiseSalary = nSalary
End Function
End Class
%>
</body>
</html>
Execute Function:
Test the Script --> /learn/test/vbs5execute.asp
<html><head>
<title>vbs5execute.asp</title>
</head>
<body>
<%
Execute S
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 57 de 142
Call Hi()
%>
</body>
</html>
Regular Expressions
Test the Script --> /learn/test/vbs5reg.asp
<html><head>
<title>vbs5reg.asp</title>&
<body>
<%
address="joe@aol.com"
validmail=checkemail(address)
IF validmail THEN
ELSE
END IF
address="sallyaol.com"
validmail=checkemail(address)
IF validmail THEN
ELSE
END IF
FUNCTION CheckEmail(parmaddress)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 58 de 142
' Set the pattern to check for a word followed by
myRegExp.pattern = "\w+\@[.\w]+"
if myRegExp.Test(parmaddress) then
CheckEmail=True
else
CheckEmail=false
end if
END FUNCTION
%>
</body>
</html>
With
Test the Script --> /learn/test/vbs5w ith.asp
<html><head>
<title>vbs5with.asp</title>
</head>
<body>
<%
with response
.write "Hi<br>"
end with
%>
</body>
</html>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 59 de 142
When you want a quick easy generic database display, go on over to:
http://www.ofifc.org/Eli/asp/homepage.asp
Here Eli Robillard has done a lot of work for you. You modify one ASP file that specifies your database
and query specs and his ASP scripts magically do the rest.
Here is an example where I make a pubs.asp designed to plug into his ASP scripts.
<%
Session("dbGenericPath") = "/learn/test/genericdb/"
Session("dbExitPage") = "http://www.activeserverpages.com"
Session("dbTitle") = "Pubs"
Session("dbType") = "SQL"
Session("dbConn") = "DSN=student;uid=student;pwd=magic"
Session("dbRs") = "Publishers"
Session("dbKey") = 1
Session("dbOrder") = 2
Session("dbRecsPerPage") = 10
Session("dbFooter") = 1
Session("dbDispList") = "011101000000100010"
Session("dbDispView") = "111111111111111111"
Session("dbDispEdit") = "011111111111111111"
Session("dbSearchFields") = "011100010010001000"
Session("dbDefault6") = Date()
Session("dbDefault17") = 10
Session("dbWhere") = ""
Session("dbDebug") = 1
Session("dbCanEdit") = 1
Session("dbCanAdd") = 1
Session("dbCanDelete") = 1
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 60 de 142
Session("dbConfirmDelete") = 1
Session("dbViewPage") = Request.ServerVariables("PATH_INFO")
%>
There is even a listserve supporting this great FREE script collection. It is not a general database
troubleshooting list, instead only questions concerning these wonderful scripts are allowed.
(see /learn/dbtablepaged.asp)
(see www.activeserverpages.com/learn/dbmaxrecs.asp)
Side note: If anyone you know believes Access queries are done asynchronously, running a couple of
these scripts will prove them wrong. Access queries execute one web user at a time sequentially.
After Optimization
Here is a very fast table display going against an identical huge SQL Server Table:
<%response.buffer=true%>
<HEAD><TITLE>dbtablefast.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN = "DSN=student;UID=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 61 de 142
<!--#include virtual="/learn/test/lib_dbtablefast.asp"-->
</BODY></HTML>
<%
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
DO UNTIL rstemp.eof
counter=counter+1
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 62 de 142
response.write "<tr>"
for i = 0 to howmanyfields
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if
next
response.write "</tr>"
rstemp.movenext
If response.isclientconnected()=false THEN
EXIT DO
END IF
response.flush
END IF
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 63 de 142
conntemp.close
set conntemp=nothing
end sub%>
Before Optimization
Here is the original slow script which basically demonstrates techniques that may work if your data and
concurrency load is light, but the script above demonstrates the typical changes made to speed up a
script when it becomes needed or you just want to wring every ounce of speed from your site. This script
will probably timeout before it's completion!
<HEAD><TITLE>dbtableslow.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN = "DSN=student;uid=student;pwd=magic"
call query2table(mySQL,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtableslow.asp"-->
</BODY></HTML>
<%
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 64 de 142
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<tr>
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 65 de 142
Query took 6 seconds.
Method: LOOP, .movenext and periodic response.flush commands. String is assembled with &
operator and writen periodically.
Method: GetRows command with no LOOP + movenext but loop through the array with periodic
response flushes.
Note: Getstring often wins in many tests but it depends on many factors. I have seen a straight
loop take 18 secs where GetRows takes 9 seconds and getstring take 4 secs.
Method: GetRows command of a fixed row count (say 500 record clusters for example) with a
reponse.flush after each array is read!
Method: Displaying a portion of the data (say the first 500 records) only.
Note: This was recorded as 7 seconds but actually was much faster actually due to speed the
browser reported the results.
Now here is the code that was used to gather all the data.
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 66 de 142
Here is a table display with a simple LOOP:
<%response.buffer=true%>
<HEAD><TITLE>dbtableLoopAll.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
<%
server.scripttimeout=240
optimize=optimize_LoopAll
myDSN = "DSN=student;UID=student;pwd=magic"
Call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
Call TimerEnd
%>
</BODY></HTML>
<%response.buffer=true%>
<HEAD><TITLE>dbtableLoopAllstring.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
<%
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 67 de 142
server.scripttimeout=240
optimize=optimize_LoopAll_string
myDSN = "DSN=student;UID=student;pwd=magic"
Call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
Call TimerEnd
%>
</BODY></HTML>
<%response.buffer=true%>
<HEAD><TITLE>dbtablegetstringall.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
<%
server.scripttimeout=240
optimize=optimize_GetStringAll
myDSN = "DSN=student;UID=student;pwd=magic"
Call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 68 de 142
response.write OptimizationName(optimize) & "<br>"
Call TimerEnd
%>
</BODY></HTML>
<%response.buffer=true%>
<HEAD><TITLE>dbtablegetstringbuffered.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
<%
server.scripttimeout=240
optimize=optimize_GetStringBuffered
myDSN = "DSN=student;UID=student;pwd=magic"
Call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
Call TimerEnd
%>
</BODY></HTML>
<%response.buffer=true%>
<HEAD><TITLE>dbtablegetrowsall.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 69 de 142
<%
server.scripttimeout=240
optimize=optimize_getrowsall
myDSN = "DSN=student;UID=student;pwd=magic"
call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
call TimerEnd
%>
</BODY></HTML>
<%response.buffer=true%>
<HEAD><TITLE>dbtableGetRowsBuffered.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
<%
server.scripttimeout=240
optimize=optimize_GetRowsBuffered
myDSN = "DSN=student;UID=student;pwd=magic"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 70 de 142
Call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
Call TimerEnd
%>
</BODY></HTML>
<%response.buffer=true%>
<HEAD><TITLE>dbtablelimitrows.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/adovbs.inc"-->
<!--#include virtual="/learn/test/lib_dbtablefastv2.asp"-->
<%
server.scripttimeout=240
optimize=optimize_LimitRows
myDSN = "DSN=student;UID=student;pwd=magic"
Call TimerStart
call query2table(mySQL,myDSN,optimize,howmany)
Call TimerEnd
%>
</BODY></HTML>
<%
Const optimize_LoopAll = 1
Const optimize_GetstringAll = 2
Const optimize_GetrowsAll = 3
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 71 de 142
Const optimize_GetrowsBuffered = 4
Const optimize_GetStringBuffered = 5
Const optimize_LimitRows = 6
Const optimize_LoopAll_String = 7
dim optimize_buffersize
dim optimize_started
dim optimize_ended
dim optimize_SQL
dim optimize_DSN
dim optimize_howmany
dim optimize_cursorlocation
dim optimize_maxrecs
dim optimize_disconnectRS
optimize_started=0
optimize_buffersize=200
'optimize_cursorlocation=aduseclient
optimize_maxrecs=500
optimize_cursorlocation=aduseserver
optimize_disconnectRS=false
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 72 de 142
optimize_stringwrite=false
SUB TimerStart()
optimize_started=now()
END SUB
SUB TimerEnd()
optimize_ended=now()
IF optimize_howmany=-1 THEN
optimize_howmany=querycount(optimize_DSN,optimize_SQL)
END IF
response.write "Notes:<br>"
pad=" "
IF optimize_cursorlocation=adUseClient THEN
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 73 de 142
END IF
IF optimize_cursorlocation=adUseServer THEN
END IF
END SUB
dim howmany
CASE 1
Call loopStandard(parmQuery,parmDSN,howmany)
CASE 2
Call loopGetString(parmQuery,parmDSN,howmany)
CASE 3
Call loopGetRows(parmQuery,parmDSN,howmany)
CASE 4
Call loopGetRowsBuffered(parmQuery,parmDSN,howmany)
CASE 5
Call loopGetStringBuffered(parmQuery,parmDSN,howmany)
CASE 6
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 74 de 142
Call LimitRows(parmQuery,parmDSN,howmany)
CASE 7
Call loopStandardStringWrite(parmQuery,parmDSN,howmany)
CASE ELSE
END SELECT
parmcount=howmany
If optimize_started<>0 THEN
optimize_DSN=parmDSN
optimize_SQL=parmquery
optimize_howmany=parmcount
END IF
END SUB
FUNCTION querycount(parmDSN,parmQuery)
set rstemp=Server.CreateObject("adodb.Recordset")
querycount=rstemp.recordcount
rstemp.close
set rstemp=nothing
END FUNCTION
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 75 de 142
SUB loopstandard(inputquery, inputDSN,inputcount)
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.cursorlocation=optimize_cursorlocation
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
IF optimize_disconnectRS=true THEN
conntemp.close
END IF
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
DO UNTIL rstemp.eof
counter=counter+1
response.write "<tr>"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 76 de 142
for i = 0 to howmanyfields
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if
next
response.write "</tr>"
rstemp.movenext
If response.isclientconnected()=false THEN
EXIT DO
END IF
END IF
loop%>
</table>
<%
inputcount=counter
rstemp.close
set rstemp=nothing
conntemp.close
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 77 de 142
set conntemp=nothing
END SUB%>
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.cursorlocation=optimize_cursorlocation
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
IF optimize_disconnectRS=true THEN
conntemp.close
END IF
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 78 de 142
tempSTR=""
DO UNTIL rstemp.eof
counter=counter+1
for i = 0 to howmanyfields
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if
tempSTR=tempSTR & "<td valign=top>" & thisvalue & "</td>" & vbcrlf
next
rstemp.movenext
If response.isclientconnected()=false THEN
EXIT DO
END IF
response.write tempSTR
response.flush
tempSTR=""
END IF
loop%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 79 de 142
</table>
<%
inputcount=counter
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
END SUB%>
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 80 de 142
</tr>
<%
response.write tempSTR
response.write "</table>"
inputcount=-1
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
END SUB%>
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 81 de 142
tablestart="<table border=1 cols=3><col width='15%'><col width='70%'><col
width='15%'><tr>"
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<%
DO
tempSTR=rstemp.getstring(,optimize_buffersize, "</td><td>",
"</td></tr><TR><TD>", " ")
response.write tempSTR
If response.isclientconnected()=false THEN
EXIT SUB
END IF
response.write "</table>"
inputcount=-1
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 82 de 142
END SUB
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<%
alldata=rstemp.getrows
numcols=ubound(alldata,1)
numrows=ubound(alldata,2)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 83 de 142
FOR rowcounter= 0 TO numrows
response.write alldata(colcounter,rowcounter)
response.write "</td>"
NEXT
If response.isclientconnected()=false THEN
EXIT FOR
END IF
END IF
NEXT
response.write "</table>"
inputcount=numrows
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
END SUB
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 84 de 142
dim conntemp, rstemp
set conntemp=server.createobject("adodb.connection")
conntemp.connectiontimeout=0
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<%
DO
alldata=rstemp.getrows(optimize_buffersize)
numcols=ubound(alldata,1)
numrows=ubound(alldata,2)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 85 de 142
response.write "<td valign=top>"
response.write alldata(colcounter,rowcounter)
response.write "</td>"
NEXT
NEXT
howmany=howmany+numrows
If response.isclientconnected()=false THEN
EXIT SUB
END IF
response.write "</table>"
inputcount=howmany
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
END SUB
set rstemp=Server.CreateObject("adodb.Recordset")
rstemp.maxrecords=optimize_maxrecs
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 86 de 142
'rstemp.open inputquery, inputDSN, adopenforwardonly, adlockReadOnly
howmanyfields=rstemp.fields.count -1
response.write tablestart
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<%
response.flush
response.write tempSTR
response.write "</td></tr></table>"
inputcount=optimize_maxrecs
rstemp.close
set rstemp=nothing
END SUB
FUNCTION optimizationName(parmNum)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 87 de 142
SELECT CASE parmnum
CASE optimize_LoopAll
optimizationName="LoopAll"
CASE optimize_GetstringAll
optimizationName="GetstringAll"
CASE optimize_GetrowsAll
optimizationName="GetrowsAll"
CASE optimize_GetrowsBuffered
optimizationName="GetrowsBuffered"
CASE optimize_GetStringBuffered
optimizationName="GetStringBuffered"
CASE optimize_LimitRows
optimizationName="LimitRows"
CASE ELSE
optimizationName="undefined"
END SELECT
END FUNCTION
%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 88 de 142
is available beginning with IIS4 (if you want to see if you have it, see /learn/versioncheck.asp) and can be
used within a page to determine if the user is still fetching that page, i.e.
<%server.scripttimeout=20
' this times out the script in 20 seconds whether done or not%>
<TITLE>infiniteloop.asp</TITLE>
<body bgcolor="#FFFFFF">
<%
DO
x=x+1
If x=10000 then
x=1
end if
LOOP
%>
</body></html>
This loop could tie up the CPU for quite a while, even though the user hit the "stop"
button the task would still be "spinning/executing" on the server even though the user
was long gone. This is particularly true in database tasks.
Test the Script --> /learn/test/infiniteloopfixed.asp
<%server.scripttimeout=20
%>
<TITLE>infiniteloopfixed.asp</TITLE>
<body bgcolor="#FFFFFF">
<%
DO
x=x+1
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 89 de 142
If x=10000 then
x=1
end if
%>
</body></html>
this code will stop when user hits the stop button.
I recommend that all loops through database recordsets incorporate this so that they
don't execute invisibly wasting CPU and database server resources. So here is a
database table listing script that incorporates this functionality:
Test the Script --> /learn/test/dbtableisclientconnected.asp
<TITLE>dbtableisclientconnected.asp</TITLE>
<body bgcolor="#FFFFFF">
<%
myDSN="DSN=Student;uid=student;pwd=magic"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
howmanyfields=rstemp.fields.count -1
%>
<table border=1><tr>
<td><b><%=rstemp(i).name %></B></TD>
</tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 90 de 142
do until (rstemp.eof or response.isclientconnected=false)%>
<tr>
<td valign=top><%=rstemp(i)%></td>
</tr>
<%rstemp.movenext
loop
%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>
<%
response.buffer=true
Response.ExpiresAbsolute = Now() - 1
%>
<html><head>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 91 de 142
<title>authorshow.asp</title>
</head><body bgcolor="#FFFFFF">
<%
myDSN="DSN=Student;uid=student;pwd=magic"
IDfield="AU_ID"
scriptresponder="authoredit.asp"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
howmanyfields=rstemp.fields.count -1
%>
<table border="1">
<tr>
<td valign="top">---</td>
<td><b><%=rstemp(i).name %></b></td>
</tr>
<tr><td valign="top">
<a HREF="<%=my_link%>">Edit</a></td>
<td valign="top"><%=rstemp(i)%></td>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 92 de 142
</tr>
<%
rstemp.movenext
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</table></body></html>
html><head>
<title>authoredit.asp</title>
</head><body bgcolor="#FFFFFF">
<%
myDSN="DSN=Student;uid=student;pwd=magic"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
form_ID=request.querystring("which")
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 93 de 142
set rstemp=conntemp.execute(sqltemp)
form_auID=rstemp("AU_ID")
form_author=rstemp("Author")
form_year_born=rstemp("Year_Born")
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
<body>
</form>
</body>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 94 de 142
Database Full Cycle #3 --
Display Table, Edit Record, Update Record
Here is the script that updates one record after the submit button is pushed on the previous script.
<body bgcolor="#FFFFFF"></HEAD>
<%
form_name=request.form("name")
form_year=request.form("year")
form_ID=request.form("ID")
conn.open "DSN=Student;uid=student;pwd=magic"
Set RS = Conn.Execute(SQLStmt)
If err.number>0 then
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 95 de 142
response.write "Help Context=" & err.helpcontext & "<P>"
end if
IF conn.errors.count> 0 then
next
else
end if
rs.close
set rs=nothing
Conn.Close
set conn=nothing
%>
</BODY>
</HTML>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 96 de 142
<html><head>
<TITLE>dbconvert.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
whichname="/upload/tests/authors.txt"
myDSN="DSN=Student;uid=student;pwd=magic"
showblank=""
shownull="<null>"
linestart=chr(34)
lineend=chr(34)
delimitersub=""
whichFN=server.mappath(whichname)
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
If rstemp.eof then
connection.close
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 97 de 142
set connection=nothing
response.end
end if
DO UNTIL rstemp.eof
thisline=linestart
thisfield=whatever.value
if isnull(thisfield) then
thisfield=shownull
end if
if trim(thisfield)="" then
thisfield=showblank
end if
thisfield=replace(thisfield,delimiter,delimitersub)
next
tempLen=len(thisline)
tempLenDelim=len(delimiter)
filetemp.WriteLine(thisline)
rstemp.movenext
LOOP
filetemp.Close
set filetemp=nothing
set fstemp=nothing
rstemp.close
set rstemp=nothing
conntemp.close
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 98 de 142
set conntemp=nothing
If err.number=0 then
else
end if
%>
</body></html>
Here is a script that will add a bunch of records with the AU_ID of 200:
<TITLE>dbaddmany.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<!--#include file="lib_errors.asp"-->
<%
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 99 de 142
myDSN = "DSN=Student;uid=student;pwd=magic"
conn.open myDSN
response.write thistask
Conn.Execute(mySQL)
Call ErrorVBScriptReport(thistask)
Call ErrorADOReport(mySQL,conn)
next
Conn.Close
set conn=nothing
%>
</BODY>
</HTML>
Now here is a script that will delete all the records the above script added:
<TITLE>dbdeletemany.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<!--#include file="lib_errors.asp"-->
<%
myDSN = "DSN=Student;uid=student;pwd=magic"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 100 de 142
Set Conn = Server.CreateObject("ADODB.Connection")
conn.open myDSN
Conn.Execute mySQL,howmany
response.write "The statement " & mySQL & "<b> deleted " & howmany & "
records</b><br>"
Call ErrorVBScriptReport("Deleting...")
Call ErrorADOReport(mySQL,conn)
Conn.Close
set conn=nothing
%>
</BODY>
</HTML>
The critical part here is having a cursor that supports it and including adovbs.inc to define the cursor
types. The file adovbs.inc can be obtained from http://www.learnasp.com/adovbs.inc
Adopenstatic cursor
The data is dead. If you retrieve a million records for example at 9:00am and it takes 6 minutes to read
the data, at the 6th minute you will not be retrieving fresh or recently added data. It is like a snapshot of
the data. Recordsets opened this way WILL contain an accurate recordcount.
The data is alive but you can only move forward. Attempts to move backward or to specific record
numbers will fail.. Recordsets opened this way WILL NOT contain an accurate recordcount, instead
returning -1.
Adopenkeyset cursor
The data is alive and any record read will be the most recent data. f you retrieve a million records for
example at 9:00am and it takes 6 minutes to read the data, at the 6th minute you will still be retrieving
fresh data but NOT data added or deleted since 9am. Recordsets opened this way WILL NOT contain an
accurate recordcount, instead returning -1.
Adopendynamic cursor
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 101 de 142
The data is alive and additions will be noticed. If you retrieve a million records for example at 9:00am
and it takes 6 minutes to read the data, at the 6th minute you will still be retrieving fresh data and records
added to the end of the data. Recordsets opened this way WILL contain an accurate recordcount.
<head><html>
<TITLE>dbcount.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
connectme="DSN=Student;uid=student;pwd=magic"
set rstemp=Server.CreateObject("adodb.Recordset")
howmanyrecs=rstemp.recordcount
rstemp.close
set rstemp=nothing
%>
</body></html>
It is also important to understand that any situation where you open the database and are able to obtain
an accurate record count is by nature slower than opening a database that also has the burden of
knowing how many records it has upon demand. The additional overhead and limitations of a
adopendynamic or adopenstatic cursor may be a poor way to retrieve the data because the recordset
even though they count accurately. If you must count AND retrieve, I recommend counting first, closing
the recordset and then re-opening the recordset with the best cursor for speed.
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 102 de 142
ADO Cursor Types by Phil Paxton (juggler@iquest.net)
ADO cursor types affects the properties and methods that are available as this table illustrates.
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 103 de 142
Requery Yes Yes Yes Yes
Resync N/A N/A Yes Yes
Supports Yes Yes Yes Yes
Update Yes Yes Yes Yes
UpdateBatch Yes Yes Yes Yes
Legend:
R/W = Read/Write
R/O = Read-Only
N/A = Not Available or Supported
Yes = Available/Supported
<html><head>
<title>dbnewrec.asp</title>&
<body bgcolor="#FFFFFF">
<% ' My ASP program that allows you to append a record %>
</form></body></html>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 104 de 142
http://www.activeserverpages.com/learn/dbtroubleshoot.asp to determine what the trouble is.
<body bgcolor="#FFFFFF">
<HTML>
<%
auname=request.querystring("name")
auyear=request.querystring("year")
auID=request.querystring("ID")
If auid<9000 then
auid=auid+9000
end if
conn.open "DSN=Student;uid=student;pwd=magic"
Set RS = Conn.Execute(SQLStmt)
set rs=nothing
If err.number>0 then
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 105 de 142
response.write "SQLState=" & err.sqlstate & "<P>"
else
end if
IF conn.errors.count> 0 then
next
else
end if
Conn.Close
set conn=nothing
%>
</BODY>
</HTML>
<html><head>
<title>dbnewrec.asp</title>&
<body bgcolor="#FFFFFF">
<% ' My ASP program that allows you to append a record %>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 106 de 142
<p>Author ID: <input type="TEXT" name="id"></p>
</form></body></html>
<TITLE>dbnewADO.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
auname=request.querystring("name")
auyear=request.querystring("year")
auID=request.querystring("ID")
If auid<9000 then
auid=auid+9000
end if
conn="DSN=Student;uid=student;pwd=magic"
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open "authors",Conn,adopenstatic,adlockoptimistic
RS.AddNew
RS("AU_ID")=auid
RS("Author") = auname
RS("Year_Born")= int(auyear)
RS.Update
RS.Close
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 107 de 142
set rs=nothing
If err.number>0 then
else
end if
%>
</BODY>
</HTML>
<TITLE>dbtablegetstring.asp</TITLE>
<body bgcolor="#FFFFFF">
<%
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 108 de 142
whichDSN="DSN=Student;uid=student;pwd=magic"
whichtable="publishers"
set conntemp=server.createobject("adodb.connection")
conntemp.open whichDSN
set rstemp=server.createobject("adodb.recordset")
adlockReadOnly, adCmdTable
response.write tempSTR
response.write "</td></tr></table>"
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>
<html><head>
<TITLE>dblistgetstring.asp</TITLE>&
<body bgcolor="#FFFFFF">
<%
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 109 de 142
myDSN="DSN=Student;uid=student;pwd=magic"
set conntemp=server.createobject("adodb.connection")
conntemp.open "DSN=Student;uid=student;pwd=magic"
set rstemp=server.createobject("adodb.recordset")
adlockReadOnly, adCmdUnknown
response.write tempSTR
response.write "</select></form>"
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>
<HEAD><TITLE>sqlservertablelist.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 110 de 142
<%
whichDSN="DSN=student;uid=student;pwd=magic"
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
Access allows you to list tables like this (assuming you allow system objects to be
visible within the database on the Access side):
Test the Script --> /learn/test/accesstablelist.asp
<HEAD><TITLE>accesstablelist.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 111 de 142
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<tr>
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
<html><head>
<TITLE>dbschemas.asp</TITLE>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 112 de 142
</head>
<body bgcolor="#FFFFFF">
<%
myDSN="DSN=Student;uid=student;pwd=magic"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
thistable=""
pad=" "
DO UNTIL rsSchema.EOF
prevtable=thistable
thistable=rsSchema("Table_Name")
thiscolumn=rsSchema("COLUMN_NAME")
IF thistable<>prevtable THEN
END IF
response.write "<br>" & pad & "Field=<b>" & thiscolumn & "</b><br>"
DIM colschema(27)
colschema(0)="TABLE_CATALOG"
colschema(1)="TABLE_SCHEMA"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 113 de 142
colschema(2)="TABLE_NAME"
colschema(3)="COLUMN_NAME"
colschema(4)="COLUMN_GUID"
colschema(5)="COLUMN_PROP_ID"
colschema(6)="ORDINAL_POSITION"
colschema(7)="COLUMN_HASDEFAULT"
colschema(8)="COLUMN_DEFAULT"
colschema(9)="COLUMN_FLAGS"
colschema(10)="IS_NULLABLE"
colschema(11)="DATA_TYPE"
colschema(12)="TYPE_GUID"
colschema(13)="CHARACTER_MAXIMUM_LENGTH"
colschema(14)="CHARACTER_OCTET_LENGTH"
colschema(15)="NUMERIC_PRECISION"
colschema(16)="NUMERIC_SCALE"
colschema(17)="DATETIME_PRECISION"
colschema(18)="CHARACTER_SET_CATALOG"
colschema(19)="CHARACTER_SET_SCHEMA"
colschema(20)="CHARACTER_SET_NAME"
colschema(21)="COLLATION_CATALOG"
colschema(22)="COLLATION_SCHEMA"
colschema(23)="COLLATION_NAME"
colschema(24)="DOMAIN_NAME"
colschema(25)="DOMAIN_CATALOG"
colschema(26)="DOMAIN_SCHEMA"
colschema(27)="DESCRIPTION"
FOR counter=4 to 27
thisColInfoType=colschema(counter)
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 114 de 142
thisColInfo=rsSchema(thisColInfoType)
If err.number<>0 then
thiscolinfo="-error-"
err.clear
END IF
IF thisColInfo<>"" THEN
END IF
NEXT
response.flush
rsSchema.MoveNext
LOOP
rsSchema.Close
set rsSchema=nothing
conntemp.close
set conntemp=nothing
%>
</body></html>
Here is the contents of lib_fieldtypes.asp which is included to make this example work:
<%
FUNCTION fieldtypename(parm1)
CASE 0
fieldtypename="adEmpty"
CASE 16
fieldtypename="adTinyInt"
CASE 2
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 115 de 142
fieldtypename="adSmallInt"
CASE 3
fieldtypename="adInteger"
CASE 20
fieldtypename="adBigInt"
CASE 17
fieldtypename="adUnsignedTinyInt"
CASE 18
fieldtypename="adUnsignedSmallInt"
CASE 19
fieldtypename="adUnsignedInt"
CASE 21
fieldtypename="adUnsignedBigInt"
CASE 4
fieldtypename="adSingle"
CASE 5
fieldtypename="adDouble"
CASE 6
fieldtypename="adCurrency"
CASE 14
fieldtypename="adDecimal"
CASE 131
fieldtypename="adNumeric"
CASE 11
fieldtypename="adBoolean"
CASE 10
fieldtypename="adError"
CASE 132
fieldtypename="adUserDefined"
CASE 12
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 116 de 142
fieldtypename="adVariant"
CASE 9
fieldtypename="adIDispatch"
CASE 13
fieldtypename="adIUnknown"
CASE 72
fieldtypename="adGUID"
CASE 7
fieldtypename="adDate"
CASE 133
fieldtypename="adDBDate"
CASE 134
fieldtypename="adDBTime"
CASE 135
fieldtypename="adDBTimeStamp"
CASE 8
fieldtypename="adBSTR"
CASE 129
fieldtypename="adChar"
CASE 200
fieldtypename="adVarChar"
CASE 201
fieldtypename="adLongVarChar"
CASE 130
fieldtypename="adWChar"
CASE 202
fieldtypename="adVarWChar"
CASE 203
fieldtypename="adLongVarWChar"
CASE 128
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 117 de 142
fieldtypename="adBinary"
CASE 204
fieldtypename="adVarBinary"
CASE 205
fieldtypename="adLongVarBinary"
CASE ELSE
fieldtypename="Undefined by ADO"
END SELECT
END FUNCTION
%>
Text fields with single quote ' cannot be placed into SQL statements unmodified. Notice how the last
example below uses the VBScript replace command to transform a string that may contain embedded ' .
Statement with Flaws Improved Statement (fixes in red)
added space before SET
UPDATE mytableSET UPDATE mytable SET
LocID=0007,Material=13 1/4 LocID='0007',Material='13 1/4',
Description=T-shirts ListPrice=35 WHERE Description='T-shirts', [List Price]=35
CusID=97 WHERE CusID=97
INSERT INTO authors (AU_ID, author, INSERT INTO authors (AU_ID, author,
year_born) VALUES (7000, Joe year_born) VALUES (7000, 'Joe
Smith,1950) Smith',1950)
SELECT * from atable where state = MD SELECT * from atable where state = 'MD'
and and
year born<1955 and [year born]<1955
<% <%
key=request.querystring("id") key=request.querystring("id")
au=request.querystring("author") au=request.querystring("author")
birthyear=request.querystring("year") au=Replace(au, "'", "''")
SQLstmt="INSERT INTO authors (AU_ID, birthyear=request.querystring("year")
author, year_born) VALUES (" SQLstmt="INSERT INTO authors (AU_ID,
SQLstmt= SQLstmt & key & "," author, year_born) VALUES ("
SQLstmt= SQLstmt & author & "," SQLstmt= SQLstmt & key & ",'"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 118 de 142
SQLstmt= SQLstmt & author & "',"
SQLstmt= SQLstmt & birthyear & ")"
SQLstmt= SQLstmt & birthyear & ")"
%>
%>
<body bgcolor="#FFFFFF">
<%
set conntemp=server.createobject("adodb.connection")
conntemp.open "DSN=Student;uid=student;pwd=magic"
p1=request.querystring("ID")
set rstemp=conntemp.execute(temp)
howmanyfields=rstemp.fields.count -1
%>
<table border=1>
<tr>
<td><b><%=rstemp(i).name %></B></TD>
</tr>
<tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 119 de 142
<% next %>
</tr>
<% rstemp.movenext
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing%>
</table>
</body>
</html>
ISBN Au_ID
PubID Name Company_Name Address City State Zip Telephone Fax Comments
"select * from publishers where state='MD'
"select * from publishers where PubID=10"
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 120 de 142
If you aren't sure how to spell the text field or are looking for sound alikes, LIKE supports %%
wildcards
"select * from publishers where Name like 'A%%'"
These basic WHERE clause guidelines above are the fundamental building block of searches.
"select * from publishers where PubID>10"
<HEAD><TITLE>SQLwhere1.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<HEAD><TITLE>SQLwhere2.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 121 de 142
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<HEAD><TITLE>SQLwhere3.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<HEAD><TITLE>sqlwhere4.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<%
sub query2table(inputquery)
set conntemp=server.createobject("adodb.connection")
conntemp.open "DSN=Student;uid=student;pwd=magic"
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<td><b><%=rstemp(i).name%></B></TD>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 122 de 142
</tr>
<tr>
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
<HEAD><TITLE>sqlwhereform1.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
Choose A State:<p>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 123 de 142
State: <Input NAME="st" MaxLength="2" size="3"><P>
</BODY></HTML>
<HEAD><TITLE>sqlwhereform1respond.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
myDSN="DSN=student;uid=student;pwd=magic"
mystate=request.querystring("st")
call query2table(SQLtemp,myDSN)
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<td><b><%=rstemp(i).name%></B></TD>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 124 de 142
</tr>
<tr>
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
If your goal is that several if conditions must ALL BE TRUE to suceed, this is the Role of the AND within a
WHERE clause, i.e.
"select * from publishers where state='MD' and city='Rockville'
"select * from authors where Year_Born>1960 and Year_Born<1970'
If several conditions can indivually be true this is the Role of an OR within a WHERE clause, i.e.
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 125 de 142
"select * from publishers where state='MD' OR state='NY'
Test the Script --> /learn/test/sqlcities.asp
<HEAD><TITLE>SQLcities.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
"cy","DSN=student;uid=student;pwd=magic")
%>
<P>
</BODY></HTML>
<!--#include virtual="/learn/test/lib_dblistm.asp"-->
<HEAD><TITLE>sqlcitiesrespond.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
citycount=request.form("cy").count
If citycount=0 then%>
<%
response.end
end if
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 126 de 142
firstcity=request.form("cy")(1)
SQLtemp = SQLtemp & " where city='" & firstcity & "'"
whichcity=request.form("cy")(counter)
SQLtemp = SQLtemp & " or city='" & whichcity & "' "
next
response.write SQLtemp
call query2table(SQLtemp,"DSN=student;uid=student;pwd=magic")
%>
<!--#include virtual="/learn/test/lib_dbtable.asp"-->
</BODY></HTML>
<%
set conntemp=server.createobject("adodb.connection")
conntemp.open inputDSN
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<tr>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 127 de 142
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
<%
SUB query2listm(myquery,myname,myDSN)
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(myquery)
%>
<%
thisfield=trim(RStemp(0))
' ignore
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 128 de 142
else
end if
rstemp.movenext
loop
%>
</select>
<%rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
END SUB
%>
"select count(*) from publishers where state='NY'
If you count a specific field you must groupby that field or it won't work:
"select count(city),city from publishers group by city"
The AS operator allows you to specify a name for the counted field:
"select count(city) as howmany,city from publishers group by city"
<HEAD><TITLE>SQLcount1.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 129 de 142
Test the Script --> /learn/test/sqlcount2.asp
<HEAD><TITLE>SQLcount2.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<HEAD><TITLE>SQLcount3.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<HEAD><TITLE>SQLcount4.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<%
%>
<!--#include virtual="/learn/test/subdbtable.inc"-->
</BODY></HTML>
<%
sub query2table(inputquery)
set conntemp=server.createobject("adodb.connection")
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 130 de 142
conntemp.open "DSN=Student;uid=student;pwd=magic"
set rstemp=conntemp.execute(inputquery)
howmanyfields=rstemp.fields.count -1%>
<table border=1><tr>
<td><b><%=rstemp(i).name%></B></TD>
</tr>
<tr>
thisvalue=rstemp(i)
If isnull(thisvalue) then
thisvalue=" "
end if%>
<td valign=top><%=thisvalue%></td>
</tr>
<%rstemp.movenext
loop%>
</table>
<%
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
end sub%>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 131 de 142
In this demonstration I will explain to you how joins between tables work in SQL. I will use the verbose SQL rather
than the shortcuts due to the fact that the shortcuts differ between databases.
Primary Key(PK): This is the unique field in your table that is used to identify each record. (Ex: RecID)
Foreign Key(FK): This is a column that references a primary key of another table. It can have duplicate values.
Customer
CustomerID(PK) CustomerName
1 Joe Schmoe
2 Fred Flintstone
Sales
3 1 $1.00
4 1 $22.00
5 1 $3.00
6 20 $22.00
Inner Joins
When joining two tables there are two ways to do it. The most common way is the inner join.
The inner join will return all data where all joined data exists.
In our example above, we are selecting all the Customer Names and amount of the sale where the customer numbers
exist in both tables. The result of the query is this:
CustomerName SalesAmount
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 132 de 142
Joe Schmoe $3.00
Note that record ID 6 in the sales table with customer ID of 20 is not in our result. Since that joined data does not
exist we do not see the data.
Note: The SQL above can be written a lot simpler, doing it this way will avoid confusion when other tables are
added:
Outer Joins
The outer join is useful when we want to return all data from one table, and also return linked data from another,
when it exists, but here is where we differ from the inner join, we want to return all data from table 1 no matter
what.
In our example we want to return all sales, even if there isn’t a valid customer associated with it.
CustomerName SalesAmount
$22.00
Notice we did a right join. We chose to select all the data from the right table in our join statement. What would it
look like if we changed to this:
Our results:
CustomerName SalesAmount
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 133 de 142
Joe Schmoe $3.00
Fred Flintstone
The results gave us all the records from the left table (Customer) and the linked data from the right.
As with the inner join there are shortcuts for joins, but that depends on which database you are using. Writing the
verbose SQL statement will work on all databases.
Aaron Alexander
Browser Capabilites
The script below demonstrates the most commonly used property of the Browser Capabilites
component.
<html><head>
<TITLE>bc.asp</TITLE>
</head><body bgcolor="#FFFFFF">
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 134 de 142
I noticed you allow me to play music<p>
<%
end if
set bc=nothing
%>
</body></html>
<%@ TRANSACTION=Required%>
<%
response.buffer=true
server.scripttimeout=20
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 135 de 142
%>
<HTML>
<TITLE>loop3.asp</TITLE>
<body bgcolor="#FFFFFF">
</BODY>
<%
DO
counter=counter+1
LOOP
response.flush
%>
</HTML>
<%
Sub OnTransactionAbort()
response.clear
end sub
%>
<%@ TRANSACTION=Required%>
<%
response.buffer=true
server.scripttimeout=40
%>
<HTML>
<TITLE>loop4.asp</TITLE>
<body bgcolor="#FFFFFF">
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 136 de 142
</BODY>
<%
DO UNTIL counter=400
counter=counter+1
LOOP
response.flush
%>
</HTML>
<%
Sub OnTransactionAbort()
response.clear
end sub
%>
<%response.buffer=true%>
<TITLE>doloop1.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
DO
counter=counter+1
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 137 de 142
response.write counter & "<br>"
response.flush
LOOP
%>
</BODY>
</HTML>
<%
response.buffer=true
server.scripttimeout=20
%>
<TITLE>loop2.asp</TITLE>
<body bgcolor="#FFFFFF">
<HTML>
<%
DO
counter=counter+1
response.flush
LOOP
%>
</BODY>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 138 de 142
</HTML>
It has been assumed that a timed out script was impossible to intercept, but the next lesson shows
how to use the transactional aspect of an ASP script to capture this elusive condition.
Session data is generally attached to one user. When a user visits their first page of your site, that page
and every page they visit is collectively called a session. Any data attached stored in that session object is
private to the pages that user is visiting.
The code to store data in a session variable is simple. Here we will allow a user to flip a coin, i.e.
flipcoin.asp and count their successes:
<%
randomize
randomnum=int(rnd*2)+1
IF randomnum=1 THEN
session("heads")=session("heads")+1
ELSE
session("tails")=session("tails")+1
END IF
%>
Even though there are many people at the site they all have different scores for their "heads" and "tails"
count. They each has a session and it co-ordinates and differentiates their values.
A much more practical example could protect access to a page based on a session variable that indicated
their security level determined once upon login, see:
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 139 de 142
http://www.learnasp.com/learn/security.asp
Session data is stored on the server, not in cookies. No user could examine the
session cookie and determine the contents of any session variables.
A cookie is used to co-ordinate the user's session ID. Once again the cookie
contains no data (just the session ID). This means if the user accepts no cookies,
you can't use sessions as described here.
If you absolutely need sessions without client cookies, installing an ISAPI filter
named "Cookie Munger" will solve your problem, but at a performance penalty.
Cookies are a necessity to maintain ASP session objects. Even though the session objects are physically
maintained on the server the essential ID that identifies a user session is kept in a cookie. This ISAPI filter
can only be applied globally but if applied, it actually rewrites any pages sent back to the user so that any
URLs in the page will pass back user session info without any cookies involved. Basically (at a
performance penalty) it transforms every page sent to the client to encode the session ID as part of the
URLs on the page. So conceptually if you send a page back to the users with 20 URLs each URL gets a
session ID "munged"/intermixed in so that when they click on the URL you have enough info to Identify
them without writing a cookie to their machine.
<%SELECT CASE
CASE 1 %>
<!#include virtual="whatever1.asp">
CASE 2 %>
<!#include virtual="whatever2.asp">
CASE 3 %>
<!#include virtual="whatever3.asp">
<%END SELECT%>
Three includes are performed before any ASP code is executed.
<%
whichfile="1"%>
<!#include virtual="whatever<%=whichfil%>.asp">
Though this is a reasonable idea.
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 140 de 142
<!#include virtual="whatever.asp">
The alternative is:
<html><head>
<TITLE>includedynamic.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
whichfile="bookscifi.asp"
Call ReadDisplayFile(whichfile)
response.write "<hr>"
whichfile="bookhorror.asp"
Call ReadDisplayFile(whichfile)
response.write "<hr>"
whichfile="/learn/test/bookmarketing.asp"
Call ReadDisplayFile(whichfile)
response.write "<hr>"
%>
</body></html>
<%
SUB ReadDisplayFile(FileToRead)
whichfile=server.mappath(FileToRead)
Set fs = CreateObject("Scripting.FileSystemObject")
tempSTR=thisfile.readall
response.write tempSTR
thisfile.Close
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 141 de 142
set thisfile=nothing
set fs=nothing
END SUB
%>
The only downside to this method is no ASP Code ( i.e. anything in <% %> ) will be parsed or executed in
the included file.
<html><head>
<TITLE>dblist.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
myDSN="DSN=Student;uid=student;pwd=magic"
set conntemp=server.createobject("adodb.connection")
conntemp.open myDSN
set rstemp=conntemp.execute(mySQL)
if rstemp.eof then
response.write mySQL
conntemp.close
set conntemp=nothing
response.end
end if
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa
http://www.learnasp.com/learn/dbtablelists.asp Página 142 de 142
%>
<Select name="authorname">
<%
<%
rstemp.movenext
loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
%>
</Select></form>
</body></html>
<html><head>
<TITLE>dblistrespond.asp</TITLE>
</head><body bgcolor="#FFFFFF">
<%
my_author=request.form("authorname")
%>
</body></html>
FMwww.flavioaraujo.cjb.net ou www10.ewebcity.com/flavioaraujo/fa