Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
INTRODUCTION TO ADO.NET |
Chapter 13 |
451 |
|
|
|
|
|
connection is terminated. The use of disconnected data architecture ensures optimum utilization of system resources because ADO.NET does not retain database locks or active connections for extended time periods.
Provides datasets: Datasets provided by ADO.NET are similar to record sets available in ADO. However, unlike a record set, a dataset is a virtual database. This is because in a disconnected data architecture an application cannot access the data source after processing each record and thus the retrieved data needs to be stored somewhere. The dataset is used to store this retrieved data for the data source. It can contain one or more tables. In addition to tables, a dataset can also store additional information about relationships and constraints between tables in a dataset. Therefore, disconnected data architecture is implemented by using datasets.
Supports XML: ADO.NET uses XML to transfer data from the data source to the dataset and from the dataset to other components. Applications created in ADO.NET are interoperable with other applications since any application or component that understands XML can access data from an ADO.NET application.
Benefits of ADO.NET
As compared to other data access technologies, ADO.NET provides various benefits, which are listed as follows:
Scalability: ADO.NET makes applications scalable by enabling applications to effectively manage multiple users. This is because disconnected data architecture, supported by ADO.NET, enables applications to service more users efficiently since the connection with a data source is terminated as soon as data is retrieved from the data source making the data source available for other users.
Performance: ADO.NET applications provide better performance as compared to ADO applications. This is because in ADO, COM marshalling is used to transfer data between applications. In this case, data transfer requires the conversion of the data type used in an application to data types that are supported by COM. On the other hand, in case of ADO.NET, no conversion of data types is required since ADO.NET uses XML to transfer data between applications.
454 Project 3 ADO.NET IN MANAGED C++
.NET data
Dataset
provider
FIGURE 13-2 ADO.NET components
In such a situation, if the application uses disconnected data architecture, it is not feasible to connect to the database for processing each record. ADO.NET provides a solution in the form of a dataset, which is especially designed to support disconnected, distributed data scenarios with ADO.NET. A dataset refers to a collection of one or more tables or records from a data source and information about the relationship that exists between them. In other words, it contains tables, rows, columns, constraints, such as primary key and foreign key constraints, and relationships that exist between the tables. A dataset is used for temporary storage of records that the application retrieves from the database as a cache in the memory. The application can then work with the records in the dataset without having to connect to the database again and again.
As mentioned earlier, a dataset is a virtual and local relational database that contains required data from the database. It is more flexible than the ADO recordset. The ADO recordset uses SQL commands to retrieve records from multiple tables. It contains records, which contain data from multiple tables, in the form of rows. However, the dataset can store entire tables and maintain relationships between them, allowing you to work with the data in the same way as in the original database.
The DataSet class represents the ADO.NET dataset and is stored in the System.Data namespace. The DataSet class consists of a collection of one or more DataTable objects that represent the tables. These DataTable objects consist of rows and columns that contain data. In addition to DataTable objects, a DataSet object comprises the information related to the data in DataTable objects, such as the primary key, the foreign key, and the relationships between columns of tables.
You can store additional information in a DataSet object, which could be the query used to generate the result set stored in the DataSet object. To store additional infor-
mation, you use the property called ExtendedProperties. The ExtendedProperties
456 Project 3 ADO.NET IN MANAGED C++
The various components of a DataSet object are listed as follows:
DataTableCollection
DataRowCollection
DataColumnCollection
DataRelationCollection
ConstraintCollection
The following sections discuss each of these components.
DataTableCollection
The DataTableCollection object represents the set of DataTable objects in a DataSet object. You can add or remove a specific DataTable object from the DataTableCollection class by using the Add() and Remove() methods, respectively.
You can remove all the DataTable objects stored in the DataTableCollection object by using the Clear() method. The Contains() method returns a boolean value that determines whether a table exists in the DataTableCollection object.
Each DataTable object derives data from a single data source. You can use DataTable either as an independent object or from within a DataSet object. A DataTable object can also be used from within other .NET Framework objects, such as DataView. While creating a DataTable object, you must first define the schema or structure of the table. The schema of a DataTable object is defined by the columns and foreign key and unique constraints in the table. Therefore, to define the schema of a table, you need to add columns and foreign key and unique constraints to the table.
NOTE
DataView is a .NET Framework object that enables you to create different views of the data stored in a DataTable object. You will learn more about the DataView object in the section called “Managing a Data Source by Using Datasets.”
Just like an ordinary table, a DataTable object consists of rows and columns of data. The rows and columns present in a DataTable object are identical to those
INTRODUCTION TO ADO.NET |
Chapter 13 |
457 |
|
|
|
|
|
present in the original data source. In addition, the data in a DataTable object retains all relations within the table as in the data source.
You can obtain information about a DataTable object by using the properties and methods available in DataTable and DataSet classes. In a collection of DataTable objects, the names of DataTable objects are case-sensitive when multiple instances of the same name exist. When you search for a specific table in the TableCollection object, the search might return unexpected results based on the name of the DataTable object specified. However, when only one instance of a DataTable object name exists in a collection of DataTable objects, a search is not case-sensitive.
DataRowCollection
The collection of all rows in a DataTable object constitutes the DataRowCollection object. Each row in a DataTable object is represented by a DataRow object. You can add a maximum of 16,777,216 rows to a DataTable object. You can add a row to the DataTable object by using the NewRow() method. This method returns a DataRow object that conforms to the schema of the DataTable object as defined in DataColumnCollection or collection of columns in a table.
An important characteristic of a DataRow object is that the object tracks the changes made to its data. To track changes, DataRow maintains two versions of data, the current version and the original version. You can determine if changes have been made to the data in DataTable by using data-related events supported
by the DataTable class. Some of these events are RowChanged, RowChanging, RowDeleting, and RowDeleted.
A DataRow object can be used by the DataSet and DataView objects. An important property of the DataRow class is the RowState property. The RowState property displays information about how data in a row has been modified. The property can include values, such as Deleted, Modified, New, and Unchanged.
The DataRow class provides methods that you can use to retrieve, calculate, and manipulate data in a row. The DataRowCollection class provides methods such as Add() and Remove() that you can use to add and remove a DataRow object from the collection of DataRow objects, respectively. The Contains() method of the DataRowCollection class allows you to search for words or phrases in characterbased data. You can also locate a DataRow object with specific values in the primary key column by using the Find() method.
458 Project 3 ADO.NET IN MANAGED C++
DataColumnCollection
The DataColumnCollection class consists of a collection of DataColumn objects. An object of the DataColumnCollection class defines the structure of a DataTable object and determines the type of data in each column of a table.
You can use properties of the DataColumnCollection class to obtain information about the DataColumnCollection object. Some of the important properties of the
DataColumnCollection class are Count, Item, and IsReadOnly. In addition, you
can add or remove DataColumn objects from the DataColumnCollection object by using the Add() and Remove() methods, respectively. You can also determine whether a column belongs to a collection by using the Contains() method.
DataRelationCollection
Relations between columns of DataTable objects in a DataSet object are stored in the DataRelationCollection object. Each relationship between DataTable objects is represented by a DataRelation object. The purpose of a DataRelation object is similar to that of a relation in a relational database, where relationships are created to help locate matching rows between two tables and other purposes, such as referential integrity.
You can create parent-child relations between tables by using one or more related columns in the tables. In a parent-child relationship, for each column data in the parent table, there exists data in the child table. In other words, for each related column value in the parent table, there is a matching column value in the child table. In fact, the very first step in the creation of a relation is the verification that matching columns exist. If matching columns exist in the two DataTable objects, the relation is created between a parent and a child DataTable object.
During the creation of relations, if the data in the parent or child rows is modified, an exception might be raised when the change in the data makes the relationship invalid. Later, the relationship is added to the DataRelationCollection object.
After a parent-child relation is created, the DataRelationCollection object does not allow any changes to the data, which might invalidate the relationship. To do
this, the DataRelationCollection object might create the ForeignKeyConstraint
and UniqueKeyConstraint objects. These objects ensure data integrity by determining the action that will occur when a value in a parent table is deleted or updated.
The DataRelationCollection class includes methods that you can use to manipulate the contents of the DataRelationCollection object. For instance, you can
INTRODUCTION TO ADO.NET |
Chapter 13 |
459 |
|
|
|
|
|
add and remove DataRelation objects from the collection by using the Add() and Remove() methods, respectively. You can also remove all the DataRelation objects in the collection by using the Clear() method.
Certain properties of the DataTable class can be used to obtain information about relations between DataTable objects. For instance, you can use the ChildRelations property to view the set of child relations for a particular DataTable object. You can view the list of parent relations for a particular DataTable object by using
the ParentRelations property.
ConstraintCollection
In a dataset, all the constraints on data are stored in the ConstraintCollection object. Constraints comprise of the UniqueConstraint and ForeignKeyConstraint objects. These objects are created automatically when a DataRelation
object is added to the DataRelationCollection object. The UniqueConstraint
and ForeignKeyConstraint objects ensure the integrity of data in a dataset. In other words, these objects ensure that changes to related columns do not invalidate a relationship between DataTable objects.
A UniqueConstraint object validates the uniqueness of new values added to a column in a table. ForeignKeyConstraint determines the repercussions of any change to the primary key value in the parent table or the related child tables.
You have learned about the various components of a DataSet object. Table 13-1 summarizes the classes involved in a dataset.
Table 13-1 Classes Involved in a Dataset
Class |
Description |
DataSet |
Represents a virtual, local relational database in the memory that is |
|
used for temporary storage of data. |
DataTable |
Represents one table of in-memory data in a dataset. A DataTable |
|
object contains data in the form of rows and columns. |
DataRow |
Represents a row containing data in a DataTable object. |
DataColumn |
Represents a column schema in a DataTable object. |
DataRelation |
Represents the relationship that exists between two DataTable |
|
objects. |
Constraint |
Represents a constraint for one or more DataColumn objects. |
|
|
