0

How to define rule in stylecop to force developer to close SQLDataReader() or connection?

if there exists any other solution then also please suggest.

The solution required is to force developer to close sqldatareader

or

we can do any other changes in DAL or any other apporach to make sure that connection of datareader is close..

Dr. Rajesh Rolen
  • 14,029
  • 41
  • 106
  • 178

1 Answers1

0

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.

Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
Veldmuis
  • 4,678
  • 4
  • 25
  • 25