How about using a delegate? I don't know your exact architecture, but we have wrapped the ExecuteReader()
method in our own method, let's call it ExecuteReaderWrapper()
that ensures the correct DB connection is used etc. ExecuteReaderWrapper()
basically just returns the SqlDataReader
returned by ExecuteReader()
.
Now we don't write any custom code using these methods (we use DataSets
in stead), so we don't have the problem you do. But one option would be to change ExecuteReaderWrapper()
to expect a delegate in stead of returning the SqlDataReader
. Then ExecuteReaderWrapper()
would pass the SqlDataReader
to the delegate and to all the required cleanup afterwards. Then the developers don't have to worry about closing at all.