简体   繁体   English

ADO.NET数据集用法

[英]ADO.NET DataSet usage

I am currently trying to use an SQLite database with a DataGridView in C#. 我目前正在尝试将SQLite数据库与C#中的DataGridView一起使用。

I have seen an example program which does the following: 我看过一个执行以下操作的示例程序:

DataSet combinedDataSet = new DataSet();
DataTable ContactsTable = CombinedDataSet.Tables.Add("contacts");
DataTable FoodTable = CombinedDataSet.Tables.Add("food");

SQLiteDataAdapter ad = new SQLiteDataAdapter(command)

ad.Fill(FoodTable);
ad.Fill(ContactsTable);

This data table is then used as the source for a datagridview. 该数据表然后用作datagridview的源。

My Question is this: 我的问题是这样的:

Why use a DataSet? 为什么要使用数据集? Why not just go straight to a DataTable? 为什么不直接进入DataTable?

I am sure there is a very good reason, but having taken it out of the code, it still appears to run fine. 我敢肯定有一个很好的理由,但是从代码中删除它之后,它似乎仍然可以正常运行。

A DataSet is an in-memory representation of relational data. 数据集是关系数据的内存表示形式。 In particular, it may contain several DataTables, with relations between them. 特别是,它可能包含多个数据表,它们之间具有关系。


See " DataSets, DataTables, and DataViews (ADO.NET) " for a good set of overview articles. 有关大量概述文章请参见“ 数据集,数据表和数据视图(ADO.NET) ”。 You'll see how the DataSet maintains a collection of DataRelation objects, and how each DataTable maintains it's own collection of parent and child relations. 您将看到DataSet如何维护DataRelation对象的集合,以及每个DataTable如何维护其自己的父子关系的集合。

I agree that there is not particular reason here for putting the DataTables into a DataSet. 我同意这里没有将DataTables放入DataSet的特殊原因。 The DataSet is just a convenient container for passing the DataTables around, including potentially to Session. DataSet只是一个方便的容器,用于传递DataTable,包括可能传递给Session。

If you think it is simpler and cleaner to call for the DataTables separately, as needed, and not associate them with each other in a DataSet, that will work just fine. 如果您认为根据需要分别调用DataTable而不在DataSet中将它们彼此关联起来更简单,更简洁,则可以正常工作。

From a practical standpoint, it may be necessary to use DataSet objects if you are using a DataAdapter object to perform updates. 从实际的角度来看,如果您正在使用DataAdapter对象执行更新,则可能有必要使用DataSet对象。 For example, if you want to use the DataAdapter.Update method, you have to pass it a DataSet object. 例如,如果要使用DataAdapter.Update方法,则必须向其传递一个DataSet对象。

It is perfacly ok to have only DataTables without DataSets 完全可以只包含DataTables而没有DataSets

Beside what @John Saunders said about relations, i can think of two more benefits 除了@约翰·桑德斯(John Saunders)关于关系的说法外,我还能想到另外两个好处

When working with typed datasets (generated from xsl) you usally work on the level of datasets because Typed DataTables are nested inside the DataSet. 当使用类型化数据集(从xsl生成)时,由于类型化数据表嵌套在数据集内部,因此通常在数据集级别上工作。

The Dataset is useful for debugging. 数据集对于调试很有用。 In the inermediate-window you can call combinedDataSet.GetXml() to look into it or save it to a file for better analyse of the content. 在中间窗口中,您可以调用combinedDataSet.GetXml()进行查看或将其保存到文件中以更好地分析内容。 I am not shure if the new dotnet-4 allow Table.GetXml() but up to dotnet-3.5 table.GetXml() does not exist. 我不确定新的dotnet-4是否允许Table.GetXml()但最多dotnet-3.5 table.GetXml()不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM