views:

371

answers:

2

Hello,


Does SqlDataReader fetch one record at a time from DB or one field at a time?


Assume the following query returns a single row:

select columns_1, column_2, column_3
from   some_Table

and suppose readerS ( readerS is an instance returned by SqlCommand.ExecuteReader() )only reads *column_3* before closing the connection:

readerS[“column_3”].ToString();


Now will readerS fetch from sql server just a *“column_3”* value, or will it fetch all 3 columns, even though it will only read one?


Reason I’m asking this is because I’m currently reading a book on website programming and author mentions that he will code DLL objects to use lazy load pattern.

In short - when DLL object is created, a DB query is performed (via DAL), which retrieves data from various columns and with it populates the properties of our DLL object. Since one of the fields ( call it "L" ) may contain quite a substantial amount of text, author decided to retrieve that field only when that property is read for the first time.

But when I looked at stored procedure used by author to retrieve columns and populate DLL object's properties ( except for property L, which won't get populated ), I've noticed that procedure also retrieved L column, even though it won't be used to populate a corresponding property. Now if DataReader retrieves a whole row, and not just the field we actually read, then author didn't really implemented lazy load pattern?!

+1  A: 

I believe the data reader only works on the row level, not field by field.

Without seeing the code my guess would be that the lazy loading comes in to play when populating the properties of the class, not the pulling of data from the database.

The lazy loading would be that the class property is not populated with the data from the database until something exiplicitly tries to access the property. In this case "L" is not loaded untill the "L" property's get method is called.

cptScarlet
That's basically what the author did. Essentially when "L" property is accessed for the first time, appropriate method is called which retrieves "L" from database. But since the procedure used to populate other class properties ( those populated at the time DLL object is created ) also retrives "L", then ...
SourceC
Doesn't look like I got all of your comment but I assume you were saying that there is a constructor in the object that loads all of the properties with the values from the database. If this is the case then the author is NOT implementing lazy loading. He would be if he did not load all of populate all of the properties in the constructor.
cptScarlet
Object's constructor doesn't load "L" property with a DB value, but it loads all of other properties with values from DB. But problem is that DB query (which was used to retrieve this data) also returned "L" field and thus DataReader also retrieved "L" column, even though it didn't read it.
SourceC
The "L" property not being loaded right away with the value from the "L" column is perfectly fine, like I said, that pretty much exactly what lazy loading is.Reading your previous comment it seems like you feel the "L" column should be loaded seperately when then "L" property is first accessed, am I correct? If so, I think this would be a mistake as, at the very least, it adds another call to the database that is not needed. The only way I could see this possibly being acceotable is if the "L" property is accesseed very rarely.
cptScarlet
+1  A: 

I don't know how the author implemented the pattern, but DataReader retrieves result row by row not column by column.

Canavar