Solved

Easy load a two dimensional array from a SqlDataReader

Posted on 2010-09-15
8
539 Views
Last Modified: 2012-06-22
I want to read two columns from a SqlDataReader into a dynamic two-dimensional string array - or some way to easily persist the data so I can iterate through it. What is the best way to do this?

example:

var someCollection = new someCollection();
while (reader.Read()
{
   someCollection.Add(reader["col1"].ToString(),reader["col2"].toString());
}
0
Comment
Question by:purplesoup
[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
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:klakkas
ID: 33682103
Why not put it in a Datatable and use this to iterate through your data?

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 33682109
There are bunches of ways to do this.  If you want a Key/Value pairing, you can use a HashTable.  Myself, I might create a struct with the values like:

public struct MyStruct
{
    string ColumnOneValue;
    string ColumnTwoValue;
}

List<MyStruct> myStructList = new List<MyStruct>();

Then, in the loop:

MyStruct ms = new MyStruct();
ms.ColumnOneValue = reader["col1"].ToString();
ms.ColumnTwoValue = reader["col2"].ToString();

and

if ( !myStructList.Contains( ms ) )
{
   myStructList.Add( ms ) );
}

or you could use a HashSet instead of a List.
0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 250 total points
ID: 33682268
I'm proposing to use Anonymous Types if you don't to bother for the Type of each item. And also notice I'm not using .ToString() as it may throw exception if the value is null.
IList list = new List();
            while (reader.Read())
                list.Add(new { ColumnOne = (string)reader["col1"], ColumnTwo = (string) reader["col2"] });

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:purplesoup
ID: 33682576
klakkas - yes a DataTable is an option, I'd like to understand what the other options are first before using it.

Dan7el - I don't want key/value pairing so no hashtable, I'd also rather not create a separate structure just to hold the values.

Novice_Novice - I was wondering about anonymous types, this might be the way to go, I'd like to use strong typing though with strings.

I'll keep this open a little longer and see if anything else comes up..
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33682886
It will be good to know your intent to help us understand better and suggest the best.
0
 

Author Comment

by:purplesoup
ID: 33689564
I'm iterating through a master/detail table relationship.
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33689581
No, actually I meant what you will do with this list?
0
 
LVL 7

Assisted Solution

by:klakkas
klakkas earned 250 total points
ID: 33689612
I will stick with the suggestion of the datatable for your problem purplesoup, for the following reasons:

1. It is easy to load the data to the datatable from the DataReader.
2. It is very easy to iterate through the data
3. You do not worry about datatypes, DataRowItem can contain anything.
4. Important: You can use the Datatable.Select method to either select some data off your datatable or even sort it automatically.
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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