Much of Microsoft's data technology, ADO.NET, is provided by the DataSet object. This object reads the database and creates an in-memory copy of that part of the database that your program needs. A DataSet object usually corresponds to a real database table or view, but DataSet is a disconnected view of the database. After ADO.NET creates a DataSet, there is no need for an active connection to the database, which helps in scalability because the program only has to connect with a database server for microseconds when reading or writing. In addition to being reliable and easy to use, DataSet supports both a hierarchical view of the data as XML and a relational view that you can manage after your program disconnects.
Microsoft的许多数据技术ADO.NET由DataSet对象提供。 该对象读取数据库并创建程序需要的数据库那部分的内存副本。 DataSet对象通常对应于真实的数据库表或视图,但是DataSet是数据库的断开连接的视图。 ADO.NET创建数据集之后,就不需要与数据库的活动连接,这有助于扩展性,因为在读取或写入程序时,该程序仅需与数据库服务器连接数微秒。 除了可靠和易于使用之外,DataSet还支持数据的XML分层视图和关系断开视图,您可以在程序断开连接后对其进行管理。
You can create your own unique views of a database using DataSet. Relate DataTable objects to each other with DataRelation objects. You can even enforce data integrity using the UniqueConstraint and ForeignKeyConstraint objects. The simple example below uses only one table, but you can use multiple tables from different sources if you need them.
您可以使用DataSet创建自己的数据库唯一视图。 将DataTable对象与DataRelation对象相互关联。 您甚至可以使用UniqueConstraint和ForeignKeyConstraint对象来强制数据完整性。 下面的简单示例仅使用一个表,但是如果需要,您可以使用来自不同来源的多个表。
编码VB.NET数据集 ( Coding a VB.NET DataSet )
This code creates a DataSet with one table, one column, and two rows:
此代码创建一个具有一个表,一列和两行的DataSet:
The most common way to create a DataSet is to use the Fill method of the DataAdapter object. Here's a tested program example:
创建数据集的最常见方法是使用DataAdapter对象的Fill方法。 这是一个经过测试的程序示例:
The DataSet can then be treated as a database in your program code. The syntax doesn't require it, but you will normally provide the name of the DataTable to load the data into. Here's an example showing how to display a field.
然后可以将DataSet视为程序代码中的数据库。 语法不需要它,但是通常您将提供DataTable的名称以将数据加载到其中。 这是显示如何显示字段的示例。
Although the DataSet is easy to use, if raw performance is the goal, you might be better off writing more code and using the DataReader instead.
尽管DataSet易于使用,但如果以原始性能为目标,则最好编写更多代码并改用DataReader。
If you need to update the database after changing the DataSet, you can use the Update method of the DataAdapter object, but you have to make sure that the DataAdapter properties are set correctly with SqlCommand objects. SqlCommandBuilder is usually used to do this.
如果需要在更改DataSet之后更新数据库,则可以使用DataAdapter对象的Update方法,但是必须确保已使用SqlCommand对象正确设置了DataAdapter属性。 SqlCommandBuilder通常用于执行此操作。
DataAdapter figures out what has changed and then executes an INSERT, UPDATE, or DELETE command, but as with all database operations, updates to the database can run into problems when the database is being updated by other users, so you often need to include code to anticipate and solve problems when changing the database.
DataAdapter会找出已更改的内容,然后执行INSERT,UPDATE或DELETE命令,但是与所有数据库操作一样,当其他用户更新数据库时,对数据库的更新可能会遇到问题,因此您通常需要包含代码在更改数据库时预期并解决问题。
Sometimes, only a DataSet does what you need. If you need a collection and you're serializing the data, a DataSet is the tool to use. You can quickly serialize a DataSet to XML by calling the WriteXML method.
有时,只有DataSet可以满足您的需求。 如果您需要收集并且要序列化数据,则可以使用DataSet工具。 您可以通过调用WriteXML方法将DataSet快速序列化为XML。
DataSet is the most likely object you will use for programs that reference a database. It's the core object used by ADO.NET, and it is designed to be used in a disconnected mode.
DataSet是最可能用于引用数据库的程序的对象 。 它是ADO.NET使用的核心对象,并且设计为在断开连接模式下使用。
翻译自: https://www.thoughtco.com/an-introduction-to-dataset-in-vbnet-3424224