我是否必须在 SQLConnection 被释放之前关闭它?

发布于 2024-07-30 07:47:15 字数 561 浏览 3 评论 0原文

根据我的另一个问题关于一次性对象,我们应该在 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();
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

萌化 2024-08-06 07:47:15

由于您有一个 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);
}

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

// 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);
}
嘿嘿嘿 2024-08-06 07:47:15

使用 .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);
}

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

Disassembly of SqlConnection from using .NET Reflector:

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()

那小子欠揍 2024-08-06 07:47:15

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

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

“我们强烈建议您始终
关闭连接时
使用完毕后,
连接将返回到
水池。 您可以使用以下任一方法来执行此操作
Close 或 Dispose 方法
连接对象,或通过打开所有
using 语句内的连接

在 C# 中”

使用.NET Reflector实际实现SqlConnection.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);
}

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);
}
梦初启 2024-08-06 07:47:15

使用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);
}

Using Reflector, you can see that the Dispose method of SqlConnection actually does call Close();

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        this._userConnectionOptions = null;
        this._poolGroup = null;
        this.Close();
    }
    this.DisposeMe(disposing);
    base.Dispose(disposing);
}
萝莉病 2024-08-06 07:47:15

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

MSDN - SqlConnection.Dispose()

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

MSDN - SqlConnection.Dispose()

悲念泪 2024-08-06 07:47:15

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

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

十雾 2024-08-06 07:47:15

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

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

No, it is not necessary to Close a connection before calling 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)

撩动你心 2024-08-06 07:47:15

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文