Solved

Read result set into array of Dictionary's.

Posted on 2004-04-05
7
2,139 Views
Last Modified: 2012-06-21
Whats the most efficient way to read a database queury result set into an array of Dictionary objects.
ie. each array element contains a Dictionary representing a row, and the dictionary stores column name=column value pairs.
0
Comment
Question by:objects
  • 3
  • 3
7 Comments
 
LVL 28

Accepted Solution

by:
sybe earned 400 total points
ID: 10763442
I am afraid that you are stuck with this procedure:

Query -> Recordset
    Loop through Recordset
    For each record:
           create new element in Array
           make element dictionary
                for each field in recordset
                        add element to ditcionary


This is not very efficient in terms of performance (the code is quite short though), but at the moment i don't see another solution

<%
Set oRS = connection.Execute("SELECT * FROM tablename")
Do While Not oRS.EOF
    ReDim Preserve aResult(Ubound(aResult)+1)
    Set aResult(Ubound(aResult)) = Server.CreateObject("Scripting.Dictionary")
    For i = 0 To oRS.Field.Count - 1
        aResult(Ubound(aResult))(oRS.Fields(i).Name = oRS(i).Value
    Next
Loop
%>

                 
0
 
LVL 92

Author Comment

by:objects
ID: 10763529
> This is not very efficient in terms of performance

WHat are the inefficient parts?
0
 
LVL 28

Expert Comment

by:sybe
ID: 10763709
there's lots of inefficiency. If you do a GetRows() on the recordset, you have a 2-dimensional array, which is much faster then an array of Dictionaries.

Also ReDim Preserve is not a very efficient procedure, especially not with an array of Dictionaries. You could speed that up by ReDimming the array before looping through the recordset - if you know the number of records (oRS.RecordCount)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Author Comment

by:objects
ID: 10763864
Thats what I thought, is there any way to determine the column names if GetRows is used?
0
 
LVL 28

Expert Comment

by:sybe
ID: 10763916
no, GetRows() itself does not give the column names. But you could of course create a special array for the column names.

<%
Set oRS = connection.Execute("SELECT * FROM tablename")
Set aData = oRS.GetRows()
ReDim aColumnNames(oRS.Fields.Count-1)
For i = 0 To oRS.Fields.Count-1
    aColumnNames(i) = oRS.Fields(i).Name
Next
oRS.Close
Set oRS = Nothing

' aData holds the data from the recordset
' aColumnNames holds the fieldnames
%>
0
 
LVL 15

Assisted Solution

by:deighc
deighc earned 100 total points
ID: 10765065
I agree with @sybe - there's alot of overhead in "saving" a recordset into a dictionary object. In fact I suspect that this would give inferior performance to a disconnected recordset.

For ultimate performance arrays are the best option but, again, there's a downside with the field names as you already know. It's very annoying when you add or remove fields to your query to have to update you array indexes. You can define constants to make this easier but it's still a bit of a nuisance.

Another option I've used with some sucess is saving a recordset into an XML DOM and accessing the data that way. If you use the newer versions of MSXML the performance is very good. The XML contains not only the data but a full definition of the database columns, so it's very flexible.
0
 
LVL 92

Author Comment

by:objects
ID: 10779994
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now