Why does a DataReader continue after losing connection?

C

Chris

From just about everything I have read, a DataReader reads only one
record at a time from a database and only caches that one record in
the client's memmory. However, a colleague of mine noted that this is
not exactly the case and proceeded to demonstrate this to me by
debugging through his code and breaking on a DataReader.Read() loop.
He then pulled the network cable from his computer as the database was
on a networked server and proceeded debugging through his code. To my
amazement, the DataReader continued to Read additional records. He
was using a SqlDataReader. Can someone explain this behavior to me?

The best I can come up with is this post on MSDN:
http://msdn2.microsoft.com/en-us/library/haa3afyz(vs.71).aspx

Which states "Results are returned as the query executes, and are
stored in the network buffer on the client until you request them
using the Read method of the DataReader."

So, is a DataReader storing more than one record on the client
computer? What exactly is the article referring to as the network
buffer? An example would be helpful.

Thanks,

Chris
 
M

Michael C

Chris said:
So, is a DataReader storing more than one record on the client
computer? What exactly is the article referring to as the network
buffer? An example would be helpful.

I'd be suprised if some sort of cache didn't exist.

Michael
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
record at a time from a database and only caches that one record in
the client's memmory. However, a colleague of mine noted that this is
not exactly the case and proceeded to demonstrate this to me by
debugging through his code and breaking on a DataReader.Read() loop.
He then pulled the network cable from his computer as the database was
on a networked server and proceeded debugging through his code. To my
amazement, the DataReader continued to Read additional records. He
was using a SqlDataReader. Can someone explain this behavior to me?

The best I can come up with is this post on MSDN:
http://msdn2.microsoft.com/en-us/library/haa3afyz(vs.71).aspx

Which states "Results are returned as the query executes, and are
stored in the network buffer on the client until you request them
using the Read method of the DataReader."

So, is a DataReader storing more than one record on the client
computer? What exactly is the article referring to as the network
buffer? An example would be helpful.

Thanks,

Chris

The DataReader processes one record at a time, but the underlying
database driver has a fixed size buffer that it uses to read records
from the database. When reading, it gets the number of records that fits
in the buffer.

The buffer is (IIRC) usually 8kb, so if your records are by average 800
bytes it will read about ten records at a time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top