C# +处理 DbConnection 和 DbCommand 并捕获错误

发布于 2024-08-26 06:35:47 字数 882 浏览 5 评论 0原文

我试图了解 DbConnection 和 DbCommand,以及使用后处理这些对象的正确方法。

以下是我的代码片段。 通过在 DbConnection 和 DbCommand 上使用“using 语句”就足够了吗?我正在努力防止可能的内存泄漏。

第二个问题,

我是否必须 Dispose DbCommand 对象?

多谢

DbProviderFactory fac = DbProviderFactories.GetFactory(this.DatabaseProviderName);

using (DbConnection dbConn = fac.CreateConnection())
{
     dbConn.ConnectionString = this.ConnectionString;

     using (DbCommand comm = fac.CreateCommand())
     {
          comm.CommandText = "select * from aTable";
          comm.Connection = dbConn;
          DataTable targetTable = new DataTable();

          DbDataAdapter facDA = fac.CreateDataAdapter();
          facDA.SelectCommand = comm;
          facDA.Fill(targetTable);

          //assuming Adapter would open / close connection (right assumption?)

          //do something with the datatable
     }
}

I am trying to understand DbConnection and DbCommand, and the proper way to dispose those objects after use.

Following is the code snippet I have.
By using "using statement" on DbConnection and DbCommand, would it be sufficient? I am trying to prevent possible memory leak.

2nd question,

Do I have to Dispose DbCommand object?

thanks a lot

DbProviderFactory fac = DbProviderFactories.GetFactory(this.DatabaseProviderName);

using (DbConnection dbConn = fac.CreateConnection())
{
     dbConn.ConnectionString = this.ConnectionString;

     using (DbCommand comm = fac.CreateCommand())
     {
          comm.CommandText = "select * from aTable";
          comm.Connection = dbConn;
          DataTable targetTable = new DataTable();

          DbDataAdapter facDA = fac.CreateDataAdapter();
          facDA.SelectCommand = comm;
          facDA.Fill(targetTable);

          //assuming Adapter would open / close connection (right assumption?)

          //do something with the datatable
     }
}

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

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

发布评论

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

评论(3

抠脚大汉 2024-09-02 06:35:47

using using 与在 finally 中调用 dispose()try/finally 块相同。

DbCommand 应包含在 using 语句中,因为它实现了 IDisposable

请注意,DbCommand 实际上是一个抽象类,因此您需要

  • 从它的
  • 代码派生到接口 (IDbCommand),
  • 使用预定义的派生类之一,例如 SqlCommand

DbConnection 也是一个抽象类,因此您也需要执行与我上面针对 DbCommand 建议类似的操作。

一般建议是,如果对象实现了 IDisposable,则应将其包装在 using 语句,以便调用 Dispose() 来释放资源,即使在 in 中抛出异常语句块。在您的示例中,一个好的做法是将每个连接、命令、DataTable 和 DbDataAdapter 对象包装在 using 语句中。

using using is the same as a try/finally block with dispose() called in finally.

DbCommand should be wrapped in a using statement as it implements IDisposable.

Note that DbCommand is actually an abstract class so you will need to either

  • derive from it
  • code to an interface (IDbCommand)
  • use one of the predefined derived classes such as SqlCommand.

DbConnection is also an abstract class so you will need to do something similar as I have suggested above for DbCommand for this too.

The general recommendation is that if an object implements IDisposable, it should be wrapped in a using statement such that Dispose() is called to free resources, even if an Exception is thrown within in the statement block. In your example then, a good practice would be to wrap each of the connection, command, DataTable and DbDataAdapter objects in a using statement.

海拔太高太耀眼 2024-09-02 06:35:47

是的,我会在 DbCommand 对象上调用 Dispose。一般来说,规则应该是,如果它实现了 IDisposable,则应该在适当的时候调用它的 Dispose 方法。你的代码对我来说看起来格式很好。我认为你走在正确的道路上。

编辑 实际上,您可能还想将 DbDataAdapter 包装在 using 语句中,因为它也实现了 IDisposable。

Yes I would call Dispose on the DbCommand object. In general, the rule should be that if it implements IDisposable, you should call its Dispose method when appropriate. Your code looks well formed to me. I think you are on the right track.

EDIT Actually, you might want to also wrap your DbDataAdapter in a using statement since it too implements IDisposable.

哎呦我呸! 2024-09-02 06:35:47

像您一样将对象包装在 using 块中应该就足够了;即使抛出异常,这也会使代码调用 Dispose

是的,您应该对 DbCommand 对象以及 DbDataAdapterDataTable 执行此操作。它们都(直接或间接)实现IDisposable

Wrapping the objects in using blocks as you do should be sufficient; this will make the code call Dispose even if there is an exception thrown.

And yes, you should do that for the DbCommand object, as well as the DbDataAdapter and the DataTable. They all (directly or indirectly) implement IDisposable.

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