Это должно также работать:
ArrayList<String> orig = new ArrayList<String>();
ArrayList<String> copy = (ArrayList<String>) orig.clone()
Поскольку у вас есть блок using, будет вызван метод Dispose команды SQLCommand, который закроет соединение:
// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}
Дизассемблирование SqlConnection с использованием .NET Reflector :
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}
Он вызывает Close () внутри Dispose ()
Используя Reflector , вы можете увидеть, что метод Dispose
для SqlConnection
действительно вызывает Close ()
;
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}
Нет, вызов Dispose () в SqlConnection также вызывает Close ().
Нет, использование блока Using вызывает Dispose ()
в любом случае для вас, поэтому нет необходимости вызывать Close ()
.
Нет, нет необходимости закрывать соединение перед вызовом Dispose.
Некоторые объекты (например, SQLConnections) можно повторно использовать после вызова Close, но не после вызова Dispose. Для других объектов вызов Close аналогичен вызову Dispose. (ManualResetEvent и Streams, я думаю, ведут себя так)
Нет, Класс SqlConnection наследуется от IDisposable, и когда обнаруживается конец использования (для объекта подключения), он автоматически вызывает Dispose в классе SqlConnection.
The using keyword will close the connection correctly so the extra call to Close is not required.
From the MSDN article on SQL Server Connection Pooling:
"We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#"
The actual implementation of SqlConnection.Dispose using .NET Reflector is as follows:
// System.Data.SqlClient.SqlConnection.Dispose disassemble
protected override void Dispose(bool disposing)
{
if (disposing)
{
this._userConnectionOptions = null;
this._poolGroup = null;
this.Close();
}
this.DisposeMe(disposing);
base.Dispose(disposing);
}