C# 使用后是否需要处理 DbCommand?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070667/
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
Is is necessary to dispose DbCommand after use?
提问by Vivek
We use Enterprise Library 3.0 to access Oracle DB (microsoft oracle client). What happens when I do not dispose a DbCommand instance after a stored procedure or function is called? Does .NET automatically garbage collect them? Note that we do make sure that the transaction/connection gets closed and disposed properly.
我们使用 Enterprise Library 3.0 访问 Oracle DB(Microsoft oracle 客户端)。如果在调用存储过程或函数后不释放 DbCommand 实例,会发生什么情况?.NET 会自动垃圾收集它们吗?请注意,我们确实确保事务/连接被关闭并正确处理。
采纳答案by John Saunders
This is a duplicate, but I don't have time to find the original.
这是一个副本,但我没有时间找到原件。
If it implements IDisposable, and if you created it, then you need to call Dispose on it. That's why the developer of the class made it implement IDisposable.
如果它实现了 IDisposable,并且是您创建的,那么您需要对其调用 Dispose。这就是该类的开发人员使其实现 IDisposable 的原因。
The garbage collector does not call Dispose on all IDisposable-implementing objects.
垃圾收集器不会对所有 IDisposable 实现对象调用 Dispose。
回答by Matthew Groves
Not 100% sure about Oracle, but when using SqlCommand, it must be disposed after use. You could either just call .Dispose(), or just put it in a using block, like so:
对于 Oracle 不是 100% 确定,但是在使用 SqlCommand 时,必须在使用后对其进行处理。您可以只调用 .Dispose(),也可以将它放在 using 块中,如下所示:
using(DbCommand cmd = new DbCommand(foo, bar))
{
// use cmd object
}
回答by Jeremy Frey
Reflector doesn't indicate that OracleCommand
specifically overrides Dispose (from System.ComponentModel.Component
's implementation, anyway), so chances are it won't hurt your application much if you don't call it.
Reflector 并没有指明OracleCommand
具体覆盖 Dispose (System.ComponentModel.Component
无论如何都是from的实现),因此如果您不调用它,它可能不会对您的应用程序造成太大伤害。
The important thing, though, is that OracleCommand
specifically implements IDbCommand
, which specifically implements IDisposable
. If you ever replaced your OracleCommand
with another IDbCommand
, then you would most likely want to use Dispose()
. And while SqlCommand
doesn't explicitly override Dispose()
, Odbc and OleDb certainly do.
不过,重要的是OracleCommand
具体实现IDbCommand
,具体实现IDisposable
。如果你曾经OracleCommand
用另一个替换过你的IDbCommand
,那么你很可能想要使用Dispose()
. 虽然SqlCommand
没有明确覆盖Dispose()
,Odbc 和 OleDb 肯定会覆盖。
In short, since it's IDisposable
, you should dispose it, just to be on the safe side.
简而言之,既然是IDisposable
,就应该处置它,以防万一。
回答by Mike Hofer
From the documentation for IDisposable
:
从文档中IDisposable
:
The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.
Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.
此接口的主要用途是释放非托管资源。当某个对象不再使用时,垃圾收集器会自动释放分配给该对象的内存。但是,无法预测垃圾收集何时发生。此外,垃圾收集器不了解非托管资源,例如窗口句柄或打开的文件和流。
使用此接口的 Dispose 方法与垃圾收集器一起显式释放非托管资源。当不再需要对象时,对象的使用者可以调用此方法。
Given this, an object that implements IDisposable
potentially maintains references to unmanagedresources. These resources are not released until the garbage collector comes along and collects the object. However, since you cannot know when the garbage collector will do this, disposable objects (such as OracleDbCommand
) can hang around far longer than you might want them to.
鉴于此,实现的对象IDisposable
可能维护对非托管资源的引用。在垃圾收集器出现并收集对象之前,不会释放这些资源。但是,由于您不知道垃圾收集器何时会执行此操作,因此一次性对象(例如OracleDbCommand
)可能会比您希望它们停留的时间长得多。
If an object implements IDisposable
, you shouldcall it as soon as possible to release the unmanaged resources that it holds references to. This can be accomplished by either calling Dispose
directly or by declaring it within a using block.
如果一个对象实现了IDisposable
,你应该尽快调用它来释放它持有引用的非托管资源。这可以通过Dispose
直接调用或在 using 块中声明来实现。