C# 数据表与数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2250/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 13:02:10  来源:igfitidea点击:

Datatable vs Dataset

提问by GateKiller

I currently use a DataTable to get results from a database which I can use in my code.

我目前使用 DataTable 从我可以在我的代码中使用的数据库中获取结果。

However, many example on the web show using a DataSet instead and accessing the table(s) through the collections method.

但是,网络上的许多示例显示使用 DataSet 代替并通过集合方法访问表。

Is there any advantage, performance wise or otherwise, of using DataSets or DataTables as a storage method for SQL results?

使用 DataSets 或 DataTables 作为 SQL 结果的存储方法,在性能方面或其他方面有什么优势吗?

采纳答案by ZombieSheep

It really depends on the sort of data you're bringing back. Since a DataSet is (in effect) just a collection of DataTable objects, you can return multiple distinct sets of data into a single, and therefore more manageable, object.

这实际上取决于您要带回的数据类型。由于 DataSet(实际上)只是 DataTable 对象的集合,因此您可以将多个不同的数据集返回到一个单一的、因此更易于管理的对象中。

Performance-wise, you're more likely to get inefficiency from unoptimized queries than from the "wrong" choice of .NET construct. At least, that's been my experience.

在性能方面,与 .NET 构造的“错误”选择相比,未优化的查询更有可能导致效率低下。至少,这是我的经验。

回答by Karl Seguin

in 1.x there used to be things DataTables couldn't do which DataSets could (don't remember exactly what). All that was changed in 2.x. My guess is that's why a lot of examples still use DataSets. DataTables should be quicker as they are more lightweight. If you're only pulling a single resultset, its your best choice between the two.

在 1.x 中,曾经有一些 DataTables 不能做而 DataSets 可以做的事情(记不清是什么了)。所有这一切都在 2.x 中发生了变化。我的猜测是这就是为什么很多示例仍然使用数据集的原因。DataTables 应该更快,因为它们更轻量级。如果您只提取一个结果集,那么它是您在两者之间的最佳选择。

回答by Shawn

One feature of the DataSet is that if you can call multiple select statements in your stored procedures, the DataSet will have one DataTable for each.

DataSet 的一项功能是,如果您可以在存储过程中调用多个 select 语句,则 DataSet 将为每个语句提供一个 DataTable。

回答by tbreffni

There are some optimizations you can use when filling a DataTable, such as calling BeginLoadData(), inserting the data, then calling EndLoadData(). This turns off some internal behavior within the DataTable, such as index maintenance, etc. See this articlefor further details.

填充 DataTable 时可以使用一些优化,例如调用 BeginLoadData(),插入数据,然后调用 EndLoadData()。这会关闭 DataTable 中的一些内部行为,例如索引维护等。有关更多详细信息,请参阅此文章

回答by Joshua Hudson

One major difference is that DataSets can hold multiple tables and you can define relationships between those tables.

一个主要区别是 DataSet 可以保存多个表,并且您可以定义这些表之间的关系。

If you are only returning a single result set though I would think a DataTable would be more optimized. I would think there has to be some overhead (granted small) to offer the functionality a DataSet does and keep track of multiple DataTables.

如果您只返回一个结果集,我认为 DataTable 会更优化。我认为必须有一些开销(允许很小)来提供 DataSet 所做的功能并跟踪多个 DataTables。

回答by UnconditionallyReinstateMonica

When you are only dealing with a single table anyway, the biggest practical difference I have found is that DataSet has a "HasChanges" method but DataTable does not. Both have a "GetChanges" however, so you can use that and test for null.

无论如何,当您只处理单个表时,我发现最大的实际区别是 DataSet 有一个“HasChanges”方法,而 DataTable 没有。但是,两者都有一个“GetChanges”,因此您可以使用它并测试是否为空。

回答by Nischal Tyagi

A DataTable object represents tabular data as an in-memory, tabular cache of rows, columns, and constraints. The DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects.

DataTable 对象将表格数据表示为内存中的行、列和约束的表格缓存。DataSet 由一组 DataTable 对象组成,您可以使用 DataRelation 对象将这些对象相互关联。