Will a using block close a database connection?

前端 未结 4 378
伪装坚强ぢ
伪装坚强ぢ 2020-12-03 04:07
using (DbConnection conn = new DbConnection())
{
    // do stuff with database
}

Will the using block call conn.Close()?<

相关标签:
4条回答
  • 2020-12-03 04:49

    surely yes because it will dispose the connection and before disposing the inner logic of the connection calls the close.

    0 讨论(0)
  • 2020-12-03 04:52

    A using block will ensure the destruction of DbConnection object by calling the Dispose() method. The Dispose() method will in turn call the Close() method and has to wait for it to finish closing the connection to the database.

    0 讨论(0)
  • 2020-12-03 05:01

    Yes, it will; the implementation of DbConnection.Dispose() calls Close() (and so do its derived implementations).

    0 讨论(0)
  • 2020-12-03 05:08

    Yes - http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.close.aspx

    edit: from Microsoft: "The connection is automatically closed at the end of the using block."

    0 讨论(0)
提交回复
热议问题