何时使用DataSet / DataTable / DataReader / Command

There is a very standard "mistake" that I see everywhere. I think I do not exagerate if I say that more than half of sample code, questions and answers we see about ADO.NET everywhere do the same mistake. Too many programmers think that you need a DataSet to work with ADO.NET.

我到处都看到一个非常标准的“错误”。 如果我说我们到处都看到的有关ADO.NET的示例代码,问题和答案中的一半以上都犯了相同的错误,那我并不会感到骄傲。 太多的程序员认为您需要使用DataSet才能使用ADO.NET。

This is not True (my VB showing here :-)).

这不是正确的(我的VB显示在这里:-)。

That misconception probably comes from the very first version of the Framework, version 1.0, where the DataSet offered methods that were not available in other classes, such as WriteXml. Very useful one, but you needed a DataSet to be able to store DataTables in an XML file easily. Programmers used DataSet everywhere only to have that extra work done for free.

这种误解可能来自Framework的第一个版本1.0版,该版本的DataSet提供了其他类(例如WriteXml)中不可用的方法。 这非常有用,但是您需要一个DataSet才能将DataTables轻松存储在XML文件中。 程序员到处使用DataSet只是为了免费完成这些额外的工作。

The framework was refined, and methods such as that one were later added to the DataTable, making it possible to do more with a standalone DataTable. Unfortunately, a lot of articles and books were written that were always using a DataSet as the basis for anything having to do with database, and everybody repeated these same ideas even after they became obsolete.

框架得到了完善,后来将诸如此类的方法添加到了DataTable中,从而使使用独立DataTable可以做更多的事情成为可能。 不幸的是,撰写了许多文章和书籍,这些文章和书籍总是使用DataSet作为与数据库有关的任何事情的基础,并且即使这些想法过时了,每个人也会重复这些想法。

There are 4 main ways of dealing with data in ADO.NET. Let's make the necessary nuances.

ADO.NET中有4种处理数据的主要方法。 让我们做出必要的细微差别。

DataSet数据集

A DataSet is a collection of tables linked together through relations. It is useful only when you need relations. When you work with only one table, or with many tables that are not linked together, a DataSet adds extra useless overhead. A DataTable is sufficient.

数据集是通过关系链接在一起的表的集合。 仅在需要关系时才有用。 当您仅使用一个表或未链接在一起的许多表时,数据集会增加额外的无用开销。 一个数据表就足够了。

Simply put, if there is not a DataRelation object in the code, there should not be a DataSet.

简而言之,如果代码中没有DataRelation对象,则不应有DataSet。

DataTable数据表

A DataTable is useful only when you need to get many rows. It requires a DataColumn object for each column and a DataRow object for each row. If you need only one row, once again, this is usually useless overhead. A DataReader is sufficient.

仅当需要获取许多行时,DataTable才有用。 它要求每一列都有一个DataColumn对象,每一行都需要一个DataRow对象。 如果只需要一行,那么通常这是无用的开销。 一个DataReader就足够了。

DataReader数据读取器

A DataReader reads lines one by one. The object containing the line is a DataRecord, an object that is a lot simpler and requires less resources dans the DataRow used by the DataTable.

DataReader逐行读取行。 包含该行的对象是DataRecord,这是一个简单得多且需要较少资源的对象,与DataTable所使用的DataRow相比,所需的资源更少。

Since it holds only one line at a time in memory, and since that line is readonly, it offers less features than a DataTable. But by the same token, it requires a lot less resources. So, when you do not need those extra features, why use a DataTable. The DataReader is an ideal tool to read data into controls that are used only for display, such as a ComboBox. It is also useful to populate collections or single objects when you work with your own classes as a data layer.

由于它一次只在内存中保留一行,并且由于该行是只读的,因此它提供的功能少于DataTable。 但是出于同样的原因,它需要更少的资源。 因此,当您不需要这些额外功能时,为什么要使用DataTable。 DataReader是将数据读入仅用于显示的控件(如ComboBox)的理想工具。 当您将自己的类用作数据层时,填充集合或单个对象也很有用。

But a DataReader is useful only when you need at least a complete row. If you need to retrieve only one value, say a count or the ID for a given row, once again, it brings useless overhead. A Command object would then be a better choice.

但是,只有当您至少需要完整的一行时,DataReader才有用。 如果您只需要检索一个值,例如给定行的计数或ID,将再次带来无用的开销。 一个Command对象将是一个更好的选择。

Command命令

A Command object, usually through a DataAdapter is used to fill a DataSet and a DataTable, and to post back the data to the database if it has been modified by the application.

通常通过DataAdapter的Command对象用于填充DataSet和DataTable,并将数据回传到数据库(如果应用程序已对其进行了修改)。

But it can also be used alone. Its ExecuteScalar method is the most efficient way retrieve only one value from the database.

但也可以单独使用。 它的ExecuteScalar方法是从数据库中仅检索一个值的最有效方法。

It also has a lot of different Execute and BeginExecute methods that can be used in different ways to handle trafic to and from the database, without having to ever use a DataReader, a DataTable or a DataSet.

它还具有许多不同的Execute和BeginExecute方法,可以用不同的方式来处理往返数据库的流量,而不必使用DataReader,DataTable或DataSet。

Conclusion结论

Do you put your car in a van and the van in a plane when you have to move only 60 miles?

当您只需要移动60英里时,您会将汽车放在厢式货车中,还是将厢式货车放在飞机上?

Do yourself, a favor and stop using DataSets and DataTables all the time. Keep them for when they are needed.

尽自己所能,帮个忙,不要一直使用DataSet和DataTables。 保留它们以备不时之需。

Do everybody a favor. Flag any useless use of a DataSet when you see one in a forum question or answer. Then maybe, one day, we will be seeing more efficient code everywhere.

请大家帮个忙。 当您在论坛问题或答案中看到某个无用的数据集时,请进行标记。 然后也许有一天,我们将在各处看到更高效的代码。

翻译自: https://www.experts-exchange.com/articles/10298/When-to-use-a-DataSet-DataTable-DataReader-Command.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值