C# 在处理 SQLConnection 之前,我是否必须关闭()它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1195829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:34:46  来源:igfitidea点击:

Do I have to Close() a SQLConnection before it gets disposed?

c#asp.netusingsqlconnectionsqlcommand

提问by John Bubriski

Per my other question here about Disposable objects, should we call Close() before the end of a using block?

根据我在这里关于 Disposable objects 的另一个问题,我们应该在 using 块结束之前调用 Close() 吗?

using (SqlConnection connection = new SqlConnection())
using (SqlCommand command = new SqlCommand())
{
    command.CommandText = "INSERT INTO YourMom (Amount) VALUES (1)";
    command.CommandType = System.Data.CommandType.Text;

    connection.Open();
    command.ExecuteNonQuery();

    // Is this call necessary?
    connection.Close();
}

采纳答案by CMS

Since you have a using block, the Dispose method of the SQLCommand will be called and it will close the connection:

由于您有一个 using 块,将调用 SQLCommand 的 Dispose 方法并关闭连接:

// 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);
}

回答by Justin Niessner

No, calling Dispose() on SqlConnection also calls Close().

不,在 SqlConnection 上调用 Dispose() 也会调用 Close()。

MSDN - SqlConnection.Dispose()

MSDN - SqlConnection.Dispose()

回答by blparker

No, the SqlConnection class inherits from IDisposable, and when the end of using (for the connection object) is encountered, it automatically calls the Dispose on the SqlConnection class.

不是,SqlConnection类继承自IDisposable,当遇到使用结束(对于连接对象)时,会自动调用SqlConnection类上的Dispose。

回答by Jason Evans

No, having the Using block calls Dispose()for you anyway, so there is no need to call Close().

不,Dispose()无论如何让Using 块为您调用,因此无需调用Close().

回答by pipTheGeek

No, it is not necessary to Close a connection before calling Dispose.

不,在调用 Dispose 之前没有必要关闭连接。

Some objects, (like SQLConnections) can be re-used afer calling Close, but not after calling Dispose. For other objects calling Close is the same as calling Dispose. (ManualResetEvent and Streams I think behave like this)

某些对象(如 SQLConnections)可以在调用 Close 后重用,但不能在调用 Dispose 后重用。对于其他对象,调用 Close 与调用 Dispose 相同。(我认为 ManualResetEvent 和 Streams 的行为是这样的)

回答by statenjason

Disassembly of SqlConnection from using .NET Reflector:

使用.NET Reflector对 SqlConnection 进行反汇编:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }

    this.DisposeMe(disposing);
    base.Dispose(disposing);
}

It calls Close() inside of Dispose()

它在 Dispose() 内部调用 Close()

回答by Aaron Daniels

Using Reflector, you can see that the Disposemethod of SqlConnectionactually does call Close();

使用Reflector,你可以看到实际上调用了 的Dispose方法;SqlConnectionClose()

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}

回答by Thomas Bratt

The using keyword will close the connection correctly so the extra call to Close is not required.

using 关键字将正确关闭连接,因此不需要额外调用 Close。

From the MSDN article on SQL Server Connection Pooling:

从关于SQL Server 连接池的 MSDN 文章:

"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 statementin C#"

“我们强烈建议您在使用完毕后始终关闭连接,以便将连接返回到池中。您可以使用 Connection 对象的 Close 或 Dispose 方法,或通过打开一个内部的所有连接来完成此操作。在 C# 中使用语句"

The actual implementation of SqlConnection.Dispose using .NET Reflectoris as follows:

SqlConnection.Dispose 使用.NET Reflector的实际实现如下:

// 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);
}