Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell if a SqlConnection has an attached SqlDataReader?
    text
    copied!<p>This is something now more of curiosity than actual purpose. If you have a <code>SqlConnection</code> opened and attach a <code>SqlDataReader</code> to it, and then try to run another query using the same <code>SqlConnection</code> then it will throw an error. My question is how does the <code>SqlConnection</code> know that a reader is attached to it. There is not a public property or anything for <code>HasDataReader</code>, so how does the <code>SqlConnection</code> class know?</p> <p><br /></p> <p><strong>Original Question:</strong> (which is no longer relevant)</p> <p>Hi, I'm setting up a little thing for connection pooling and on of the more common bugs that we have occur(its always an easy fix, but we just can't remember <code>reader.Close()</code>!) it is when we have a connection that is used by a lot of classes/methods and one method opens a data reader and forgets to close it. This isn't really bad cause a lot of times all you have to do is go into the debugger and go up one level and see the function before it was and check if it had an unclosed data reader.</p> <p>Now, here is the bigger problem. In this connection pool, if a datareader is open, then it's not known until a thread gets a connection and tries to use it and the thing that originally opened the data reader may no longer even be alive. </p> <p>So quite simply, how can you detect if a data reader is open on a connection and is there a way to close the reader without closing the connection? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload