Reputation:
I read and write data to Sql Server on another server and because of it i need to open many SqlConnection
, i close them after read or write finished,but sometimes i can't connect to server but after i restart the server every thing is ok,i guess that it's because of that some SqlConnection
was open,is there any way that i can find all of them if exist?
Upvotes: 3
Views: 157
Reputation: 1062745
The short answer is "no". The only way you could do this would be to explicitly add your own tracking code, but then what you are actually doing is risking holding onto them for longer, plus it is likely that you'd forget to add it in the 2 places where you are already forgetting to close things.
The preferred "fix" here is : stop worrying about closing things, and start using
them instead. If you use using
every time you touch a connection, you cannot forget to release it.
using(var conn = whatever.GetOpenConnection()) {
// ... do stuff
} // <=== here it will be disposed for us automatically, success or failure
// and disposable includes Close()
This applies equally to any types that implement IDisposable
. In the case of ADO.NET, that includes commands and readers too.
Upvotes: 4