Manage Data Easily
Use the DataSet class to manage data you retrieve from a database or an XML source and display it in a DataGrid.
by Fabio Claudio Ferracchiati
October 2003 Issue
Technology Toolbox: VB.NET, SQL Server 2000, ASP.NET, XML, ADO.NET
ADO.NET—the new version of the ADO library that the .NET Framework provides—introduces a revolutionary class called DataSet. The DataSet is an in-memory representation of one or more database entities, such as tables, columns, and relations. It's also much more: It manages XML data easily, allowing you to load an XML schema with related data and create an internal structure of tables, columns, and rows. You access data within the DataSet in the same way, whether you retrieve it from a database or an XML source. Moreover, the DataSet class provides a history state of its data that's useful for updating database tables and XML sources. This feature lets ADO.NET's DataAdapter object analyze the state of DataSet records to understand which data must be deleted from, added to, or changed in the data source. For example, you could show records "to be deleted" in a DataGrid and have the application ask for confirmation before removing them from the data source.
I'll give you all the essential information you need to use the DataSet object in your .NET applications ( download the sample code). You can also explore some external sources to deepen your understanding (see Additional Resources).
You must understand the DataSet class' structure before you can use it (see Figure 1). Whatever the data source is, the DataSet object always provides one or more table objects that the DataTable class manages. The DataTable class contains columns and records (rows) that the DataColumn and DataRow classes, respectively, manage. The other classes in Figure 1 allow you to define relations between tables and constraints between columns, and to give the DataSet extra properties.
You can use these classes to build a DataSet from scratch. You'd do this only in rare cases—you usually use the ADO.NET DataAdapter object instead to fill a DataSet—but it's useful for understanding the DataSet structure. This code defines two columns that compose the Authors table—the ID_AUTHOR and NAME columns:
Dim dcID As New DataColumn("ID_AUTHOR",_
Type.GetType("System.Int32"))
dcID.AutoIncrement = True
dcID.AutoIncrementSeed = 1
dcID.ReadOnly = True
Dim dcName As New DataColumn("NAME", _
Type.GetType("System.String"))
dcName.MaxLength = 255
You can use DataColumn properties to define ID_AUTHOR as an auto-incremental integer column and NAME as a 255-character maximum-length string column.
Back to top
|