Search Tools Links Login

Donutboy


This code shows the use of a dynamic array for the storage of data. Recordsets are memory hogs, and shouldn't be passed across the server, so why even use one? What I was doing with this code was displaying a list of users on my website. After getting the data, I would have it build a table for the information and create hyperlinks for the appropriate fields. I've seen others do this, but they use recordsets to build them, which defeats its purpose. All the code is done with one trip to the server, so you won't see all those carrot tags being opened and closed throughout my work : )

Original Author: Jeremy Pettit

Inputs

No parameters are used, no recordsets, no command objects. You do need to create a connection object though. After the connection object is created, you pass it a sql string for the data you want to bring back.
You could create command objects and parameters if you were to use stored procs, but I didn't feel that was necessary for this simple SELECT statement.

Assumptions

I am using ADO for my code, which is pretty much used everywhere, that's why I use it : )

Returns

The only thing being brought back is data. It is obtained by executing a SQL string with your connection object also using the GetRows method...
## vArray = adoCN.Execute(strSQL).GetRows ##
The data is stored in a variable. (which in ASP code, all are variants) A dynamic array will be created. Arrays are zero based, so the first field and first record will be zero.
To get the information you will call it like this...
To get the 2nd record and first field you would use these parameters of your array.
## vArray(0,1) ##

API Declarations

I wrote this code, but I was taught this, and hopefully you learn this too. I don't care if you just steal my code, but a good programmer should know how to do this themselves.

Code

<%@ Language=VBScript %>
<%
DIM vArray
DIM i

SUB FillArray

DIM adoCN
DIM strSQL
strSQL = "SELECT UserName, FirstName, LastName, Email, Website FROM Users Order by UserName"
SET adoCN= server.CreateObject("ADODB.Connection")
WITH adoCN
.ConnectionString= "FileDSN=DonutboyWeb"
.CursorLocation=3
.Open
vArray = adoCN.Execute(strSQL).GetRows
END WITH
SET adoCN=NOTHING

END SUB
response.write ""
response.write ""
response.write ""
response.write ""
response.write ""
response.write "

User Directory

"

FillArray()


Response.Write ""
Response.Write ""
for i = 0 to UBOUND(vArray,2)
Response.Write ""
Response.Write ""
Response.Write ""
Response.Write ""
IF instr(1,vArray(3,i),"@") THEN
Response.Write ""
ELSE
Response.Write ""
END IF
IF INSTR(1,vArray(4,i),"http://") THEN
Response.Write ""
ELSE
Response.Write ""
END IF
Response.Write ""
next
Response.Write "
User NameFirst NameLast NameEmailWebsite
" & vArray(0,i) & "" & vArray(1,i) & "" & vArray(2,i) & "" & vArray(3,i) & "" & vArray(3,i) & "" & vArray(4,i) & "" & vArray(4,i) & "
"
%>

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 103 times

Categories

ASP/ HTML

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.