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
99 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
2 Comments
 
LVL 29

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 45
Hey Audio! 3 29
Call Controller Action Method from ASPX 2 15
Function after success of Jquery/AJAX action 9 13
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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