C# +处理 DbConnection 和 DbCommand 并捕获错误
我试图了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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 atry/finally
block withdispose()
called infinally
.DbCommand
should be wrapped in ausing
statement as it implementsIDisposable
.Note that
DbCommand
is actually an abstract class so you will need to eitherIDbCommand
)SqlCommand
.DbConnection
is also an abstract class so you will need to do something similar as I have suggested above forDbCommand
for this too.The general recommendation is that if an object implements
IDisposable
, it should be wrapped in ausing
statement such thatDispose()
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
andDbDataAdapter
objects in ausing
statement.是的,我会在 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.
像您一样将对象包装在
using
块中应该就足够了;即使抛出异常,这也会使代码调用Dispose
。是的,您应该对
DbCommand
对象以及DbDataAdapter
和DataTable
执行此操作。它们都(直接或间接)实现IDisposable
。Wrapping the objects in
using
blocks as you do should be sufficient; this will make the code callDispose
even if there is an exception thrown.And yes, you should do that for the
DbCommand
object, as well as theDbDataAdapter
and theDataTable
. They all (directly or indirectly) implementIDisposable
.