Is there a need to close DbConnection if a use clause is used?
No, there is no need to close DbConnection if the use clause is used?
and
Yes, this is not necessary here, because using the ends of the connection will mean closing and freeing all memory.
Since DBConnection implements the IDisposable interface, the close function is in the Dispose DBConnection method.
But if some lines are after a close line, then itβs useful
using (DbConnection db = GetDbConnection()) { // do data-access stuff // ... db.Close(); //Useless }
But here is useful
using (DbConnection db = GetDbConnection()) { // do data-access stuff // ... db.Close(); //Useful // Some more code }
In this case, you can do
using (DbConnection db = GetDbConnection()) {
Nikhil Agrawal
source share