DataReader question

  • Thread starter Thread starter Walid Magd
  • Start date Start date
W

Walid Magd

Hi everyone



My understanding is that DataReader is connected object. So can some one
please help me explaining this behavior?



Few iterations down in this loop, I stoped my SQL Server services and the
code continued to work



SqlDataReader oDataReader = cmd.ExecuteReader();



while(oDataReader.Read() )

{

Console.WriteLine(oDataReader["CustomerID"] + " - " +
oDataReader["CompanyName"]);

}

cn.Close();



On the other hand, if I moved the cn.Close() to just right after
cmd.ExecuteReader();

I get the expected unhandled exception.



What is going on? What is the difference between closed and broken
connection?
 
While the DataReader is "connected", it's also cached. If the rowset fits in
cache, the connection is not needed to fetch more rows.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
DataReader is connected - but it also buffered some data in it's default
behavior - if you continue reading through a datareader, with the database
down, you will after a few KB's of data, you will indeed get an exception.
You can change how much is buffered using CommandBehavior.SequentialRead.
This is explained in detail, alongwith practical usage in Chapter 4 of my
book.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
Back
Top