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
97 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
Comment Utility
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
Comment Utility
Generic collection , List<T>, doesn't it have a Find method with a key?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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!
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now