Core Classes in the ADO.NET System.Data Namespace

written by: Mikulas Gelodik; article published: year 2007, month 03;



In: Categories » Computers and technology » .NET apps » Core Classes in the ADO.NET System.Data Namespace

The core classes that make up the ADO.NET technology are found in the .NET Framework’s System.Data namespace. The following  article presents an overview of the functionality of the most important classes found in the System.Data namespace.

DataSet

At the heart of the new ADO.NET architecture is the DataSet. The DataSet class is located in the .NET Framework at System.Data.DataSet. The DataSet is essentially a cache of records that have been retrieved from the database. You can think of the DataSet as a miniature database. It contains tables, columns, constraints, rows, and relations. These DataSet objects are called DataTables, DataColumns, DataRows, Constraints, and Relations. The DataSet essentially allows a disconnected application to function as if it were actively connected to a database. Applications typically need to access multiple pieces of related database information in order to present useful information to the end user. For example, to work with an order an application would typically need to access a number of different database tables, including product tables, customer tables, inventory tables, and shipping tables. All of the related information from this set of tables can be grouped together in the DataSet, providing the disconnected application with the capability to work with all of the related order information that it needs.

In the disconnected model, going back to the data source to get each different piece of related information would be inefficient, so the DataSet is typically populated all at once via the active Connection object and DataAdapter from the appropriate .NET Data Provider. A database connection is briefly opened to fill the DataSet and then closed. Afterward the DataSet operates independently of the back-end database. The client application then accesses the Table, DataRow, Data Column, and DataView objects that are contained within the DataSet. Any changes made to the data contained in the DataSet can be posted back to the database via the DataAdapter object. In a multitier environment, a clone of the DataSet containing any changed data is created using the GetChanges method. Then the cloned DataSet is used as an argument of the DataAdapter’s Update method to post the changes to the target database. If any changes were made to the data in the cloned DataSet, these changes can be posted to the original DataSet using the DataSet’s Merge method.

DataTable

The DataTable class is located in the .NET Framework at System.Data.DataTable. The DataTable class represents a table of in-memory data that is contained with a DataSet object. The DataTable object can be created automatically by returning result sets from the DataAdapter to the DataSet object. DataTable objects can also be created programmatically by adding DataColumns objects to the DataTable’s DataColumns collection. Each DataTable object in a DataSet is bindable to data-aware user interface objects found in the .NET Framework’s WinForm and WebForm classes.

When changes are made to the data contained in a DataTable object, the ColumnChanging, ColumnChanged, RowChanging, and RowChanged events are fired. When data is deleted from a DataTable object, the RowDeleting and RowDeleted events are fired. New rows are added to a DataTable by calling the DataTable’s NewRow method and passing it a DataRow object. The maximum number of rows that can be stored in a DataTable is 16,777,216. The DataTable is also used as a basis to create DataView objects.

DataColumn

The DataColumn class is located in the .NET Framework at System.Data.DataColumn. The DataColumn class represents the schema of a column in a DataTable object. The DataColumn class contains several properties that are used to define the type of data contained in the DataColumn object. For example, the DataType property controls the type of data that can be stored in the DataColumn object, the DataValue property contains the DataColumn’s value, the AllowDBNull property specifies whether the DataColumn can contain NULL values, the MaxLength property sets the maximum length of a Text DataType, and the Table property specifies the DataTable object that the DataColumn belongs to. DataColumns can be made to contain unique values by associating a UniqueConstraint object with the DataColumn object. In addition, you can relate a DataColumn object to another DataColumn object by creating a DataRelation object and adding it to the DataSet’s DataRelationCollection.

DataRow

Found in the .NET Framework at System.Data.DataRow, the DataRow class represents a row of data in the DataTable object. The DataRow class and the DataColumn class represent the primary objects that make up the DataTable class. The DataRow object is used to insert, update, and delete rows from a DataTable. Rows can be added to a DataTable by either creating a new DataRow object using the NewRow method or by Adding a DataRow object to the DataSet’s DataRowCollection. DataRow objects are updated by simply changing the DataRow object’s DataValue property. You delete a DataRow object by executing the DataRow object’s Delete method or by calling the DataSet’s DataRowCollection object’s Remove method.

DataView

Found in the .NET Framework at System.Data.DataView, the DataView class offers a customized view of a subset of rows in a DataTable object. Like the DataTable object, DataView objects can be bound to both WinForm and WebForm controls. The DataView classes’s RowFilter and Sort properties can allow the data presented by the DataView to be displayed in a different order than the data presented by the base DataTable object. Like the DataTable object, the data contained in a DataView object is updatable. You can add new rows by using the AddNew method, and you can delete rows by using the Delete method.

DataViewManager

The DataViewManager class is located in the .NET Framework at SystemData.Data- ViewManager. The DataViewManager class is a bit different than the other classes in the System.Data namespace. Essentially, the DataViewManager class tracks the Data- ViewSetting objects for each DataTable in the DataSet in its DataViewSettingsCollection. The DataViewSettingsCollection is a group of DataViewSetting objects where each DataViewSetting object contains properties like the RowFilter, RowStateFilter, and Sort that define each DataView object.

DataRelation

The DataRelation class is located in the .NET Framework at System.Data. DataRelation. The DataRelation class is used to represent parent-child relationships between two DataTable objects contained in a DataSet. For example, you could create a DataRelation object between an OrderID DataColumn in an Order Header table to the corresponding OrderID DataColumn in an Order Detail table. The basic function of the DataRelation object is to facilitate navigation and data retrieval from related DataTables. In order to create a relationship between two DataTable objects, the two DataTables must contain DataColumn objects that have matching attributes. When a DataRelation is first created, the .NET Framework checks to make sure that the relationship is valid and then adds the DataRelation object to the DataRelationCollection, which tracks all of the data relations for the DataSet. The DataRelation class supports cascading changes from the parent table to the child table, and this is controlled through the ForeignKeyConstraint class.

Constraint

Found in the .NET Framework at System.Data.Constraint, the Constraint class represents a set of data integrity rules that can be applied to a DataColumn object. There is no base constructor for the Constraint class. Instead, constraint objects are created using either the ForeignKeyConstraint constructor or the UniqueConstraint constructor.

ForeignKeyConstraint

The ForeignKeyConstraint class is located in the .NET Framework at SystemData. ForeignKeyConstraint. The ForeignKeyConstraint class governs how changes in a parent table affect rows in the child table when a DataRelation exists between the two tables. For example, when you delete a value that is used in one or more related tables, a ForeignKeyConstraint class’s DeleteRule property determines whether the values in the related tables are also deleted. Deleting a value from the parent table can delete the child rows; set the values in the child table’s rows to null values; set the values in the child table’s rows to default values; or throw an exception.

UniqueConstraint

The UniqueConstraint class is located in the .NET Framework at SystemData. UniqueConstraint. The UniqueConstraint class ensures that all values entered into a DataColumn object have a unique value.

DataException

Found in the .NET Framework at System.Data.DataException, the DataException class represents an error that is thrown by one of the System.Data classes. For example, code that violates a UniqueConstraint on a DataColumn by attempting to add a duplicate value to the DataColumn will cause a DataException object to be created and added to the DataExceptionCollection. You can use the DataException objects to report error conditions in your ADO.NET applications.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.