Solved

Easy load a two dimensional array from a SqlDataReader

Posted on 2010-09-15
8
533 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
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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

19 Experts available now in Live!

Get 1:1 Help Now