[thelist] conserving resources - closing a recordset in ASP
Sarah
poohbear at designshift.com
Mon Sep 22 15:40:29 CDT 2003
> >I am trying to make my ASP code a little easier to write, and read, by
> >creating a function to perform a query for me and return a recordset.
> >Here
> >is the function:
> > function doQuery(sql)
> > {
> > var rs = Server.CreateObject("ADODB.Recordset");
> > rs.Open(sql,conn,adOpenStatic);
> > return rs;
> > }
> >Of course, using this function means that the recordset doesn't get
> >closed.
>
>How about:
>
> function doQuery(sql)
> {
> var rs = Server.CreateObject("ADODB.Recordset");
> rs.Open(sql,conn,adOpenStatic);
> var myArr = rs.GetRows()
> rs.Close
> rs = null
> return myArr;
> }
>
>That will leave you with a nice, handy two-dimensional array of your
>query results, through which you can iterate without having to carry an
>object around.
Wade,
I tried this, but couldn't figure out how to loop through the returned
array. This is what I was trying:
var contents = doQuery('SELECT * FROM Contents');
for (i = 0; i < contents.length; i++)
{
Response.Write(contents[i][0] + '<br />');
}
But for some reason contents.length is undefined.
Also, I like being able to use the recordset because I can then refer to
the columns in the query by name. Maybe I will just give up on my function
and write out the full code each time - at least I know this works, and
(most importantly) I know how :)
Thanks for the suggestion, Wade!
Sarah
More information about the thelist
mailing list