Solved

Read result set into array of Dictionary's.

Posted on 2004-04-05
7
2,142 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
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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Microsoft SQL ADO Conn Issue 6 57
Session on Html 8 36
Building JSON Results Table FROM DB 9 31
ASP Error Handler 5 14
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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

679 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