我应该 Dispose() DataSet 和 DataTable 吗? [英] Should I Dispose() DataSet and DataTable?

查看:25
本文介绍了我应该 Dispose() DataSet 和 DataTable 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataSet 和 DataTable 都实现了 IDisposable,因此,按照传统的最佳实践,我应该调用它们的 Dispose() 方法.

DataSet and DataTable both implement IDisposable, so, by conventional best practices, I should call their Dispose() methods.

但是,根据我目前所读到的内容,DataSet 和 DataTable 实际上没有任何非托管资源,因此 Dispose() 实际上并没有做太多事情.

However, from what I've read so far, DataSet and DataTable don't actually have any unmanaged resources, so Dispose() doesn't actually do much.

另外,我不能只使用 using(DataSet myDataSet...) 因为 DataSet 有一组 DataTables.

Plus, I can't just use using(DataSet myDataSet...) because DataSet has a collection of DataTables.

所以,为了安全起见,我需要遍历 myDataSet.Tables,处理每个 DataTables,然后处理 DataSet.

So, to be safe, I'd need to iterate through myDataSet.Tables, dispose of each of the DataTables, then dispose of the DataSet.

那么,在我的所有数据集和数据表上调用 Dispose() 是否值得?

So, is it worth the hassle to call Dispose() on all of my DataSets and DataTables?

附录:

对于那些认为应该处理 DataSet 的人:一般来说,dispose的模式是使用usingtry..finally,因为你要保证Dispose()会被调用.

For those of you who think that DataSet should be disposed: In general, the pattern for disposing is to use using or try..finally, because you want to guarantee that Dispose() will be called.

然而,这对于一个集合来说很快就会变得丑陋.例如,如果对 Dispose() 的调用之一抛出异常,您会怎么做?您是否吞下它(这是坏的")以便您可以继续处理下一个元素?

However, this gets ugly real fast for a collection. For example, what do you do if one of the calls to Dispose() thrown an exception? Do you swallow it (which is "bad") so that you can continue on to dispose the next element?

或者,您是否建议我只调用 myDataSet.Dispose(),而忘记在 myDataSet.Tables 中处理 DataTables?

Or, do you suggest that I just call myDataSet.Dispose(), and forget about disposing the DataTables in myDataSet.Tables?

推荐答案

这里有几个讨论解释了为什么 DataSet 不需要 Dispose.

Here are a couple of discussions explaining why Dispose is not necessary for a DataSet.

处置或不处置?:

DataSet 中的 Dispose 方法的存在仅仅是因为继承的副作用——换句话说,它实际上并没有在最终确定中做任何有用的事情.

The Dispose method in DataSet exists ONLY because of side effect of inheritance-- in other words, it doesn't actually do anything useful in the finalization.

应该在 DataTable 和 DataSet 对象上调用 Dispose 吗? 包括来自 MVP 的一些解释:

Should Dispose be called on DataTable and DataSet objects? includes some explanation from an MVP:

system.data 命名空间 (ADONET) 不包含非托管资源.因此,无需将其中任何一个作为只要你没有给自己添加一些特别的东西.

The system.data namespace (ADONET) does not contain unmanaged resources. Therefore there is no need to dispose any of those as long as you have not added yourself something special to it.

了解 Dispose 方法和数据集? 有来自权威 Scott Allen 的评论:

Understanding the Dispose method and datasets? has a with comment from authority Scott Allen:

在实践中,我们很少处理数据集,因为它几乎没有什么好处"

In pratice we rarely Dispose a DataSet because it offers little benefit"

因此,大家的共识是目前没有充分的理由在 DataSet 上调用 Dispose.

这篇关于我应该 Dispose() DataSet 和 DataTable 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆