Solved

Easy load a two dimensional array from a SqlDataReader

Posted on 2010-09-15
8
531 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

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

0
 
LVL 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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
Comment Utility
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
 

Author Comment

by:purplesoup
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

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

Author Comment

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

Expert Comment

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

Assisted Solution

by:klakkas
klakkas earned 250 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

10 Experts available now in Live!

Get 1:1 Help Now