Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Easy load a two dimensional array from a SqlDataReader

Posted on 2010-09-15
8
Medium Priority
?
549 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 1000 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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