Which one is better? from datatable or datareader?

Hi experts,

I need an array result from database.
I wonder which one is better? from datatable or datareader?

if I get it from datareader, I need 3 steps to get an array result.
datareader --> ArrayList --> Array

from datatable only 2 steps to get an array result.
datatable --> array.

really appreciate if your reply can include the reason. :)

Thank you.
Aciang.
aciangAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
large results, processing row by row -> datareader
small results -> datatable

do you really need an array, btw?
0
aciangAuthor Commented:
yes I need to copy the result to an array object.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
why? sorry to sound like a plague...
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

p_davisCommented:
i agree with angelIII and in addition to that... the datareader is a lot more light weight than a datatable b/c you don't deal with table schema like you would with  a datatable.
0
JimBrandleyCommented:
Are you building an array of atomic objects such as strings or ints or dates, or is it an array of business objects, such that any row returned in the result set from the DB contains more than one column?

Jim
0
aciangAuthor Commented:
For Example I have 3 tables.

Table 1: Marriage
Table 2: Kids
Table 3: Pets

Marriage:
-      Husband name
-      Wife name
-      Year of marriage
-      Etc..
Kids:
-      Name
-      Sex: female
-      Age:
-      Height
-      weight
Pets:
-      Sex
-      Age
-      biteOrNot

with above tables I make a class like attached
the array is for the Kids[] and Pets[]
public class Family
    {
        private Marriage _marrige;
        private Kids[] _kids;
        private Pets[] _pets;
 
        public Family()
        {
        }
 
        public Marriage marriage
        {
            get { return _family; }
            set { _family = value; }
        }
 
        public Kids[] KidList
        {
            get { return _kids; }
            set { _kids = value; }
        }
 
        public Pets[] PetList
        {
            get { return _pets; }
            set { _pets = value; }
        }
}

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I would fill 1 dataset with all those data, and make relations between the data tables:
http://www.c-sharpcorner.com/UploadFile/cvinodkumar/DataRelationVK11292005072318AM/DataRelationVK.aspx

0
JimBrandleyCommented:
I would say it depends on what you need to do with them. If you need to hang on to the data for some period of time before creating objects and lists of objects, angelll's suggestion of a DataSet with relations is pretty efficient.

However, If you are going to need to construct the objects right away as you retrieve the data, I do it somewhat differently. For a single row, such as I expect your Marriage object to be, I use:
public void SelectSingleRow(string sql, List<IDbDataParameter>, inParameters, ref Object[] results)
...
                     reader = mICommand.ExecuteReader(CommandBehavior.SingleRow);
      if (reader.Read())
      {
            reader.GetValues(results);
      }

The caller knows which columns were selected and in which order they they appear in the output. According to David Sceppa at MS, that's the most efficient way to load a subset of a single row.

For Pets and Kids, I use a DataAdapter to fill a DataTable. Once again , that that's the advice I got from MS.

Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.