?
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
Medium Priority
?
115 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
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…
Suggested Courses

762 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