Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read result set into array of Dictionary's.

Posted on 2004-04-05
7
Medium Priority
?
2,151 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 1600 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

876 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