469,358 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Why not "new SqlDataReader()" ?


Why do I say:

SqlDataReader dr;

instead of

SqlDataReader dr = new SqlDataReader();

--
Texeme Textcasting Technology
http://texeme.com

Indie Pop Rocks @ SomaFM
http://somafm.com/

Nov 16 '05 #1
3 7105
This is because your SqlCommand object is going to return to you an instance
of SqlDataReader. You never create a datareader object yourself.

if you did the following you would have two SqlDataReader objects in memory.
and the first one would no longer have a reference to it.
SqlDataReader dr = new SqlDataReader();
dr = SqlCommand.ExecuteReader();

dr is the following case is basically a pointer. The SqlCommand return the
object that dr points to.
SqlDataReader dr

hope that clears it up for you.
Chris

"Ricola !" <se****************@tower.effeil> wrote in message
news:36*************@individual.net...

Why do I say:

SqlDataReader dr;

instead of

SqlDataReader dr = new SqlDataReader();

--
Texeme Textcasting Technology
http://texeme.com

Indie Pop Rocks @ SomaFM
http://somafm.com/

Nov 16 '05 #2
Ricola ! <se****************@tower.effeil> wrote:
Why do I say:

SqlDataReader dr;

instead of

SqlDataReader dr = new SqlDataReader();


There are no public constructors for SqlDataReader - if you could
construct one as above, what would you expect it to read?

You have to obtain a SqlDataReader reference from a call to a method of
another class - usually SqlCommand.ExecuteReader.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Well .. if you did do ..

SqlDataReader dr = new SqlDataReader();

What resultset will your data reader be working upon?

- Sahil Malik
http://codebetter.com/blogs/sahil.malik/
"Ricola !" <se****************@tower.effeil> wrote in message
news:36*************@individual.net...

Why do I say:

SqlDataReader dr;

instead of

SqlDataReader dr = new SqlDataReader();

--
Texeme Textcasting Technology
http://texeme.com

Indie Pop Rocks @ SomaFM
http://somafm.com/

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.