Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read result set into array of Dictionary's.

Posted on 2004-04-05
7
Medium Priority
?
2,149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

730 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