如何在 .NET 中关闭 OracleConnection

发布于 2024-07-16 06:28:19 字数 390 浏览 9 评论 0原文

假设我有这两个对象:

OracleConnection connection = new OracleConnection(connectionString);  
OracleCommand command = new OracleCommand(sql, connection);

要关闭连接或 Oracle,我是否必须调用 command.Dispose()、connection.Dispose() 或两者都调用?

这是否足够好:

using(connection)  
{
    OracleDataReader reader = cmd.ExecuteReader();
    // whatever...
}

Say I have these two objects:

OracleConnection connection = new OracleConnection(connectionString);  
OracleCommand command = new OracleCommand(sql, connection);

To close the connection or Oracle, do I have to call command.Dispose(), connection.Dispose(), or both?

Is this good enough:

using(connection)  
{
    OracleDataReader reader = cmd.ExecuteReader();
    // whatever...
}

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

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

发布评论

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

评论(4

像极了他 2024-07-23 06:28:19
using (OracleConnection connection = new OracleConnection(connectionString))
{
    using (OracleCommand command = new OracleCommand(sql, connection))
    {
        using (OracleDataReader reader = cmd.ExecuteReader())
        {
        }
    }
}

如果它实现了 IDisposable,并且您创建了它,则将其放入 using 块中。

using (OracleConnection connection = new OracleConnection(connectionString))
{
    using (OracleCommand command = new OracleCommand(sql, connection))
    {
        using (OracleDataReader reader = cmd.ExecuteReader())
        {
        }
    }
}

If it implements IDisposable, and if you create it, then put it in a using block.

没企图 2024-07-23 06:28:19

这两个答案都非常符合目标。 您几乎总是想在任何 IDisposeable 对象上调用 .Dispose()。 通过包装“using”,您可以告诉编译器始终为您实现 try/finialy 块。

1 注意点,如果你想避免嵌套,你可以编写相同的代码,如下所示:

 using (OracleConnection connection = new OracleConnection(connectionString))
 using (OracleCommand command = new OracleCommand(sql, connection))
 using (OracleDataReader reader = cmd.ExecuteReader())
    {
        // do something here
    }

Both answers are pretty much on target. You almost always want to call .Dispose() on any IDisposeable object. By wrapping in a "using" you tell the compiler to always implement a try/finialy block for you.

1 point of note, if you want to avoid the nesting, you can write the same code like this:

 using (OracleConnection connection = new OracleConnection(connectionString))
 using (OracleCommand command = new OracleCommand(sql, connection))
 using (OracleDataReader reader = cmd.ExecuteReader())
    {
        // do something here
    }
柒七 2024-07-23 06:28:19

这已经足够好了。 using 语句将包装 dispose 语句,因此即使抛出异常,您也是安全的,这是我处置资源的首选方式。

using(OracleConnection connection = new OracleConnection(connectionString);    )  
{
   //Create a command object 
    using(OracleCommand command = new OracleCommand(sql, connection))
    {
      using(OracleDataReader reader = cmd.ExecuteReader())
      {
      }

    }
    // whatever...
}

我认为通过使用“using”,您要求编译器注入一个 try ...finally 块,并在finally块中,它将为您关闭一次性对象。

This is good enough. using statement will wrap the dispose statement, so even if the exception is thrown, you are safe, it's my preferred way to dispose the resource.

using(OracleConnection connection = new OracleConnection(connectionString);    )  
{
   //Create a command object 
    using(OracleCommand command = new OracleCommand(sql, connection))
    {
      using(OracleDataReader reader = cmd.ExecuteReader())
      {
      }

    }
    // whatever...
}

I think by use "using", you are ask the compiler to inject a try ... finally block , and in finally block, it will close the disposable object for you.

野却迷人 2024-07-23 06:28:19

using 将确保您的连接已关闭。 您还可以将 CommandBehavior.CloseConnection 传递给命令的 ExecuteReader 方法,以在调用 Dispose 之前将其关闭。

using will ensure your connection is closed. You could also pass in CommandBehavior.CloseConnection to your command's ExecuteReader method to close it before Dispose is called.

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