rodchar,
There are a few ways to do this. The easiest way that I can think of to
do this would be when you call ExecuteReader on your command, you pass the
CloseConnection value from the CommandBehavior enumeration to the method.
Then, you make it a requirement that whatever routine gets the SqlDataReader
calls Dispose on the reader, so that the underlying connection is closed.
This won't clean up the command that is used to create the reader, but
it doesn't do much in its Dispose implementation anyways that is important
(it just releases a managed reference to some metadata, so that's no big
deal), so I think you can get away with not calling Dispose on the
connection (although I don't like the idea of not doing so, you can't call
Dispose on the connection before you return the reader).
If you REALLY want to be clean about things, what you can do is return a
class that implements IDisposable and exposes the SqlDataReader through a
property. You would then pass the command as well to the class (which would
hold onto it in a private field). You would then require that instead of
calling Dispose on the reader, you call Dispose on the class that returns
the reader. In the implementation of the Dispose method, you would call
Dispose on the data reader as well as the command.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"rodchar" <ro*****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
hey all,
how do you resolve this problem?
i have a public procedure in my DataAccessLayer that gets a SqlDataReader
how do i close the reader from inside the DataAccessLayer if I'm returning
the reader to get bound to my GridView? Any code after my return will be
unreachable.
thanks,
rodchar