Datareader to Array best practice?

  • Thread starter Thread starter Cory Toms
  • Start date Start date
C

Cory Toms

Hey All,

I have question about the best way to go about doint this:


SqlDataReader _dr=components.getItems();
fooclass _myarray = new fooclass[5]; //create new array of my class
int i=0;

while (_dr.Read()) //loop through data reader to add items to the array
{
_myarray=new myarray();
_myarray.title=_dr["sometitle"].ToString();
_myarray.description=_dr["somedescription"].ToString();
i++;
}
_dr.Close();



Since I don't know how large the datareader will be until I loop through it
what is the best way to manage the size of this array? Is it most efficient
to check if the my array is full and the allocate another N spaces in it?

Thanks alot,

-a newb trying to improve
 
...
I have question about the best way to go about doint this:


SqlDataReader _dr=components.getItems();
fooclass _myarray = new fooclass[5]; //create new array of my class
int i=0;

while (_dr.Read()) //loop through data reader to add items to the array
{
_myarray=new myarray();
_myarray.title=_dr["sometitle"].ToString();
_myarray.description=_dr["somedescription"].ToString();
i++;
}
_dr.Close();



Since I don't know how large the datareader will be until I
loop through it what is the best way to manage the size of
this array? Is it most efficient to check if the my array
is full and the allocate another N spaces in it?


Well, I'm sure there are even better ways than this, but it should at least
be "better":

SqlDataReader _dr = components.getItems();
ArrayList _myFoos = new ArrayList(); // use a dynamic Collection
FooClass _item = null;

while (_dr.Read()) //loop through data reader to add items to the array
{
_item = new FooClass();
_item.title=_dr["sometitle"].ToString();
_item.description=_dr["somedescription"].ToString();
_myFoos.Add(item);
}
_dr.Close();

There are also methods for extracting the references in "array" form from an
ArrayList, if you really need to.

// Bjorn A

I have never really understood the need to prefix variables with "_", and I
still don't... ;-)
 
Yes, I agree with Bjorn. Because you cannot know in advance the number
of items in a data reader as you can with data set, you should make use
of an array list and call ArrayList.ToString to get the array if that
is what you want.
Example:
return (FooClass[]) _myFoos.ToArray(typeof(FooClass));
 
you cannot know in advance the number of items in a data reader

Well, you can, but it is admittedly kinda ugly. If you call a select
count(...) in your sql before the actual data you retrieve as a compound
query (for lack of a better term), SQL Server will return 2 resultsets back.
A data reader can access each result set returned, so you read the first
result set consisting of a single row which containts the count, and then
process the second.
 
I'm new to C#. Is there a preferred way to take the rows out of the
DataReader? I see I can use sqlDataReader.GetValues(object[]), or I can
iterate through each column of each row.

Any comments on the preferred method?

Thank you for your time,
tberry
 
I'm new to C#. Is there a preferred way to take the
rows out of the DataReader? I see I can use
sqlDataReader.GetValues(object[]), or I can
iterate through each column of each row.

Any comments on the preferred method?

I would rather say there *is* no "preferred" way in this case.

It depends on how you will make use on the data after you've read it from
the database.

In most cases I've encountered, a row corresponds to an instance of a
defined class, which you have to "populate" somehow, much as in my previous
example:

ArrayList myFoos = new ArrayList();
FooClass item = null;

while (dr.Read())
{
item = new FooClass();
item.field1 = dr["field1"];
item.field2 = dr["field2"];
myFoos.Add(item);
}
dr.Close();

....but as I said, it all depends on how you actually design your
application.

// Bjorn A
 
Thank you

Bjorn Abelli said:
I'm new to C#. Is there a preferred way to take the
rows out of the DataReader? I see I can use
sqlDataReader.GetValues(object[]), or I can
iterate through each column of each row.

Any comments on the preferred method?

I would rather say there *is* no "preferred" way in this case.

It depends on how you will make use on the data after you've read it from
the database.

In most cases I've encountered, a row corresponds to an instance of a
defined class, which you have to "populate" somehow, much as in my previous
example:

ArrayList myFoos = new ArrayList();
FooClass item = null;

while (dr.Read())
{
item = new FooClass();
item.field1 = dr["field1"];
item.field2 = dr["field2"];
myFoos.Add(item);
}
dr.Close();

....but as I said, it all depends on how you actually design your
application.

// Bjorn A
 
Bjorn Abelli said:
...
I have question about the best way to go about doint this:


SqlDataReader _dr=components.getItems();
fooclass _myarray = new fooclass[5]; //create new array of my class
int i=0;

while (_dr.Read()) //loop through data reader to add items to the array
{
_myarray=new myarray();
_myarray.title=_dr["sometitle"].ToString();
_myarray.description=_dr["somedescription"].ToString();
i++;
}
_dr.Close();



Since I don't know how large the datareader will be until I
loop through it what is the best way to manage the size of
this array? Is it most efficient to check if the my array
is full and the allocate another N spaces in it?


Well, I'm sure there are even better ways than this, but it should at least
be "better":

SqlDataReader _dr = components.getItems();
ArrayList _myFoos = new ArrayList(); // use a dynamic Collection
FooClass _item = null;

while (_dr.Read()) //loop through data reader to add items to the array
{
_item = new FooClass();
_item.title=_dr["sometitle"].ToString();
_item.description=_dr["somedescription"].ToString();
_myFoos.Add(item);
}
_dr.Close();

There are also methods for extracting the references in "array" form from an
ArrayList, if you really need to.

// Bjorn A

I have never really understood the need to prefix variables with "_", and I
still don't... ;-)


Thanks for the help, that worked well.

I also don't understand the "_"'s, but thats the way things go when you
inherit code.
 
Back
Top