Answered by:
Why DataReader uses a reference object?

Question
-
I want to ask as why the DataReader uses a reference object of Command class and why can't we create an instance of DataReader object.
i.e. we write like this:
SQLDataReader dr;
dr = com.ExecuteReader(); // ????
Satya Prakash JugranFriday, October 8, 2010 12:35 PM
Answers
-
That's right.
- Marked as answer by Satya Jugran Tuesday, October 12, 2010 10:34 AM
Tuesday, October 12, 2010 9:44 AM
All replies
-
I think you are misunderstandign a few things here.
You define a connection which describes where you're going to get the data from
You define a command which describes what data you are going to get.
Then you either call ExecuteReade, ExecuteScalar or ExecuteNonQuery to run the sql statement and retrieve the values returned by the statement.
in the case of ExecuteReader you will get a DataReader in return which allows you to iterate through the data
in the case of ExecuteScalar and ExecuteNonQuery only the out parameters and the return values are returned to you.
So why can you not directly create a DataReader? Well because a DataReader without an active connection to a retrieved dataset will not be able to read anything and would thus not be a logical thing to create on its own.
- Proposed as answer by SamAgain Monday, October 11, 2010 3:20 AM
Friday, October 8, 2010 1:05 PM -
Hi,
Thanks for your post. Just as Jesse Houwing said, it just a logical organization for the data access object model of ADO.NET.
Please mark the right answer at the right time.
Thanks,
Sam- Edited by SamAgain Monday, October 11, 2010 3:21 AM refine
Monday, October 11, 2010 3:20 AM -
This I know but why a reference object???
Can't we use like this:
SQLDataReader dr = new SQLDataReader(com);
dr.ExecuteReader();
In this scenario, the dr can get the command and active connection as well. But I want to ask when someone has written the assembly for SQLDataReader, why he din't use other methods like the above, why he stuck to only this way.
Please tell!!!
Satya Prakash JugranMonday, October 11, 2010 1:54 PM -
Because a DataReader object without an underlying TDS stream from SqlServer would not have any meaning. Executing the Command object retrieves the TDS stream and creates a correctly configured DataReader for you. This is conform the Creator Pattern (only let the object that has all the available information construct another object). The other scenario you're describing looks more like the SqlDataAdapter, which you configure with a SelectCommand and/or Insert, Update and Delete Commands. The SqlDataAdapter isn't dependent on any special objects or data streams and can thus be configured from user code.Monday, October 11, 2010 5:22 PM
-
Great Jesse,
This means that SQLDataReader is built only and only for com.ExecuteReader() to get its underlying stream and work upon it.
Am I right?
Satya Prakash JugranTuesday, October 12, 2010 9:27 AM -
That's right.
- Marked as answer by Satya Jugran Tuesday, October 12, 2010 10:34 AM
Tuesday, October 12, 2010 9:44 AM