Solved

What's the best way to load up data read in from sql server to a generic colleciton variable?

Posted on 2014-11-09
2
102 Views
Last Modified: 2015-01-06
Should I use the SqlDataReader in a while loop to read each record into a generic colleciton var using the Add method?  assigning each column to a property in a object or is there a better way to do this?

After I load up the data into the generic collection variable, how do I find the object that let's say the Id property = 5?

Thank you.
0
Comment
Question by:lapucca
[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
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 40431882
It depends on your final goal.

E.g., you may get full row of data using reader.GetValues. It returns an array. Now you need to decide what you need.

Say, you may put all your records into a list. But you may need to iterate through records to get a desired one.

Or say, if you are going to look records by ID often then I'd put these records into a Dictionary where ID is a key.

One more option is to load everything into a DataTable:

            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            DataTable dt = new DataTable();
            dt.Load(dr);

Open in new window


DataTable gives more options on serach, e.g.: http://msdn.microsoft.com/en-us/library/y06xa2h1.aspx
0
 

Author Comment

by:lapucca
ID: 40437975
Generic collection , List<T>, doesn't it have a Find method with a key?
0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
itextsharp with c# 3 18
"The resource cannot be found" 1 25
c#, datatable, aspx 4 27
Need to find a web event on the IIS server 4 12
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

697 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