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
109 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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