Connecting Tech Pros Worldwide Help | Site Map

IDataReader

  #1  
Old December 10th, 2007, 03:05 PM
RP
Guest
 
Posts: n/a
I saw a code in a class where IDataReader is used to retrieve result
from query, even if only one column is to be retrieved. It uses a
CommandObject with ExecuteReader but not using ExecuteScalar.

For what IDataReader is used and what are the benefits of it as
compared to sime DataReader.
  #2  
Old December 10th, 2007, 03:15 PM
sloan
Guest
 
Posts: n/a

re: IDataReader



If you need a single value, use ExecuteScalar.

You can get a single value with ExecuteReader ..... but its unnecessary
overhead.

..........

Using IDataReader (which is hte interface) will allow you to future proof
your code, as in the future you could swap out
(for example)
OleDbReader for a SqlReader instead.

Its a basic OO concept.

IDataReader GetEmployees()
{
//this procedure could return either an OleDbReader, a SqlReader, a
OracleReader (sp??) or anything else that implements IDataReader

}


I code to the interface, even though I use a SqlReader 99.9% of the time.





"RP" <rpk.general@gmail.comwrote in message
news:0f441199-dca9-4a7d-b6ad-d2c3de8ca977@e25g2000prg.googlegroups.com...
Quote:
>I saw a code in a class where IDataReader is used to retrieve result
from query, even if only one column is to be retrieved. It uses a
CommandObject with ExecuteReader but not using ExecuteScalar.
>
For what IDataReader is used and what are the benefits of it as
compared to sime DataReader.

  #3  
Old December 10th, 2007, 03:45 PM
Marc Gravell
Guest
 
Posts: n/a

re: IDataReader


One other advantage of IDataReader would be when working with BLOB/
CLOB data; ExecuteReader can be used in sequential mode to read large
data more efficiently - i.e. in chunks rather than the either column
at once.

Another use might be when you want to verify that the data is
*exactly* as expected; in LINQ terms, ExecuteScalar is comparable to
"First" - where-as if you want to enforce a "Single" you need to check
that no *more* rows were received.

I haven't checked whether ExecuteScalar does this - but another issue
with db access is where a database error message *follows* the data-
stream. Because of how TDS works, you won't see the database error
unless you at the minimum ask the reader to read past its grids - i.e.
"while (dr.NextResult()) {...}". This scenario would be quite rare,
but it bit me once and I haven't forgotten it. The man page suggests
that ExecuteScaler simply stops reading once it has found an answer.

Getting less likely: you might have a common logging block that tracks
the data volumes returned; in the *general* case this might work with
IDataReader?

There are some other edge-case occasions when it might also be useful
- i.e. when you want to give it to something existing that expects an
IDataReader (perhaps it normally works with lots of data, and you just
happen to be giving it one cell). Two main examples here would be
object pipelines, and things like SqlBulkCopy [which would be overkill
for a single cell!].

Marc
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stuck with Idatareader object in N-Tier Architecture rsdev answers 0 July 10th, 2007 08:56 PM
Closing IDataReader יוני גולדברג answers 2 October 5th, 2006 05:15 PM
Difference between SQLDataReader and IDATAREADER...? Mahesh Kumar.R answers 4 December 2nd, 2005 04:55 AM
IDataReader Read() methid question Gelios answers 5 November 17th, 2005 02:19 AM