Solved

Easy load a two dimensional array from a SqlDataReader

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

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# MVC Insert Multiple Row into DB 2 61
VB.NET - Refactor Class per SOLID principles 2 33
TFS 2015 Access denied. 1 29
Selenium and Xpath 4 28
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 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