473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Datareader to Array best practice?

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[i]=new myarray();
_myarray[i].title=_dr["sometitle"].ToString();
_myarray[i].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


Nov 17 '05 #1
7 36880

"Cory Toms" wrote...
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[i]=new myarray();
_myarray[i].title=_dr["sometitle"].ToString();
_myarray[i].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... ;-)
Nov 17 '05 #2
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));

Nov 17 '05 #3
>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.
"Truong Hong Thi" <th*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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));

Nov 17 '05 #4
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

"Truong Hong Thi" wrote:
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));

Nov 17 '05 #5

"ASP Yaboh" wrote...
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
Nov 17 '05 #6
Thank you

"Bjorn Abelli" wrote:

"ASP Yaboh" wrote...
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

Nov 17 '05 #7
Reg

"Bjorn Abelli" <bj**********@DoNotSpam.hotmail.com> wrote in message
news:e7**************@tk2msftngp13.phx.gbl...

"Cory Toms" wrote...
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[i]=new myarray();
_myarray[i].title=_dr["sometitle"].ToString();
_myarray[i].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.

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave O | last post by:
Is it possible to return a SQLdatareader from a web service?
7
by: DS | last post by:
Is there a way to automatically close the data reader connection? I'm using the MS Data Access Application block to substantially {entirely} separate the data access layer (DAL) from the business...
14
by: Bihn | last post by:
I was reading about datareader which is said to be slimmer & faster then dataset. Since the datareader have to go fetching the dat from the database every time it need it, the data it gets then...
1
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
2
by: =?Utf-8?B?ZGJhMTIz?= | last post by:
Note, I'm using C# I am wondering what type of control I should use for iterating through the set of records returned by callin this method below, so that I can then do some stuff with each...
3
by: Froefel | last post by:
Hi group I am creating a web application that uses a simple DAL as an ObjectDataSource. To retrieve data from the database, I use a DataReader object from which I then assign the various fields...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.