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

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

问题描述

DataSet和DataTable都实现了IDisposable,所以通过常规的最佳实践,我应该调用它们的Dispose()方法。



然而,从我读过的远,DataSet和DataTable实际上并不具有任何非托管资源,所以Dispose()实际上并不太多。



另外,我不能只使用 using(DataSet myDataSet ...)因为DataSet有一个数据表集合。



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



所以,这是否值得麻烦来调用Dispose()所有的DataSet和DataTable?



附录:



谁认为DataSet应该被处理:
一般来说,处理的模式是使用使用 try..finally ,因为你想要保证Dispose()将被调用。



然而,这得到ugl y真正快速收集。例如,如果其中一个调用Dispose()抛出异常,该怎么办?你吞噬它(这是坏),以便您可以继续处理下一个元素?



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

解决方案

以下是几个讨论,解释为什么Dispose不需要DataSet。



处置或不要处理?


DataSet中的Dispose方法只因为继承的副作用而存在 - 换句话说,在最终确定中实际上并没有任何有用的东西。


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


system.data命名空间(ADONET)不包含
非托管资源。因此,没有必要处理任何一个
,因为你没有添加一些特别的东西。


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


在pratice中,我们很少处理DataSet,因为它没有什么好处


所以,共识就是目前没有好的理由在DataSet上调用Dispose。


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

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.

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

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

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

Addendum:

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.

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?

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

解决方案

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

To Dispose or Not to 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.

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

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.

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"

So, the consensus there is that there is currently no good reason to call Dispose on a DataSet.

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

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