473,324 Members | 2,214 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,324 software developers and data experts.

IDataReader

RP
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.
Dec 10 '07 #1
2 2819

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" <rp*********@gmail.comwrote in message
news:0f**********************************@e25g2000 prg.googlegroups.com...
>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.

Dec 10 '07 #2
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
Dec 10 '07 #3

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

Similar topics

5
by: Tamir Khason | last post by:
As far as I understand, IDataReader, based on command recieves isClosed property when the connection closes. How to prevent it?
2
by: Vivek Sharma | last post by:
Hi There, can some one please clarify the difference between SQLDataReader and IDataReader? What are the advantages and disadvantages of each? Thanks Vivek
5
by: Gelios | last post by:
Hello All! I am going to crazy and feeling myself so stupid but I don't understand such behaviour. I have code: public int getNextAgentId() { Int32 agent_id = 0; IDataReader dr =...
4
by: Mahesh Kumar.R | last post by:
What is the difference between SqlDataReader and IDataReader ...? kindly with small example... Mahesh~
3
by: js | last post by:
Hi, I want to debug the return idatareader, how to print out some fields value? many thanks.
2
by: יוני גולדברג | last post by:
Hi, In few places within my code the business object pass IDataReader to the GUI. Suddenly i noticed that nowhere in the code the IDataReader is being closed. Does the data binding operation...
2
by: Larry R | last post by:
Whenever I try the following, the reader that is returned is always closed. What am I missing ? When I look at the reader in the ExecuteReader, it is fine. THen it gets closed on the returm. ...
0
by: rsdev | last post by:
Hi, I am new to ASP.NET and I am trying to build a CMS application using theBeerHouse SK. I am adapting the Datareader to collect information from an SQL database and send it to an HtmlHeader. ...
0
by: Andrus | last post by:
RDLDesigner report designer requires IDataReader for data access. To use it with Linq, I need to convert Linq projection to IDataReader. var db = new NorthwindContext(); var q = from c in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.