Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать

450 Project 3 ADO.NET IN MANAGED C++

You have been introduced to the various data access models discussed above. You might feel that ADO is the most preferred and suitable data access model. However, the latest and the enhanced version of ADO, ADO.NET, is a more suitable data access model than ADO. This is primarily because in the present scenario, applications focus more on Web and ADO.NET has been designed, primarily for use as an efficient data access model for the distributed applications on the Web.

ADO.NET provides support for disconnected applications that are based on the

 

Y

n-tier programming environment. To understand ADO.NET, you need to first

know about the .NET Framework.

 

Introducing ADO.NET L

A

 

The introduction of the .NET FrameworkFhas brought with it a new program-

E

 

ming model that supports disconnected data architecture. In a disconnected data

architecture, an application doesMnot remain connected to a data source through-

T

 

out a series of transactions. In fact, an application connects to a data source as and when required. his architecture is not supported by various data access technologies such as DAO and RDO. However, you can work with distributed Web applications by using ADO with RDS.

When the .NET Framework was introduced, Microsoft redesigned ADO to create ADO.NET that resolves the limitations of the earlier data access technologies. Unlike DAO and RDO, ADO.NET supports disconnected data architecture. ADO.NET includes a set of classes that enable you to create distributed Web applications. Using ADO.NET, you can also create distributed multi-tier and data-sharing applications.

The next section discusses the various features of ADO.NET.

Features of ADO.NET

ADO.NET is an enhanced version of ADO and thus provides features that were lacking in other data access technologies. The main features of ADO.NET are as follows:

Supports disconnected data architecture: An application created in ADO.NET connects to the data source only to access or update data. If you want to access or update the data source, the application connects to the data source and after the application retrieves the required data, the

Team-Fly®

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.

452Project 3 ADO.NET IN MANAGED C++

Programmability: ADO.NET enables quick and easy programming with minimum errors. ADO.NET enables you to use typed programming, which makes it easier to read and write code. In addition, typed programming checks for errors at compile time, which increases the safety of the code.

Interoperability: Applications created in ADO.NET are interoperable with other applications. This is because ADO.NET supports XML to transfer data and any application that interprets XML can exchange data with an ADO.NET application.

Maintainability: You can use ADO.NET to create applications that are logically divided into layers. This division simplifies the maintenance of the application. In addition, you can add additional layers to an ADO.NET application as and when required. For example, in case you need to modify an existing application, created in ADO.NET, to improve its performance, you can simply modify an existing layer or add a layer to the application.

The next section discusses ADO.NET architecture.

ADO.NET Architecture

ADO.NET architecture enables you to develop components that handle data from several sources. Since ADO.NET is used to access data, it requires accessing various data-related classes for which it makes use of data-related namespaces. The namespaces of the .NET Framework use a hierarchical, dot syntax naming scheme to logically group types making it easy to search for and refer to these types in the application.

NOTE

A namespace refers to a naming scheme that logically groups related types, such as classes and structures.

System.Data is the main data-related namespace. This namespace stores the classes that make up the ADO.NET architecture. The classes in the System.Data namespace are used for accessing and managing data from various data sources.

INTRODUCTION TO ADO.NET

Chapter 13

453

 

 

 

 

While using ADO.NET, you need to reference the System.Data namespace in your applications. The System.Data namespace further contains two more data-

related namespaces, System.Data.OleDb and System.Data.SqlClient. While the

System.Data.OleDb namespace contains classes used by the OLE DB .NET data provider, the System.Data.SqlClient namespace contains classes used by the SQL Server .NET data provider.

NOTE

A .NET data provider enables you to connect to a data source, execute commands against the data source, and retrieve results based on the commands executed. In the .NET Framework, the two main .NET data providers that are available are OLE DB and SQL Server .NET. The OLE DB .NET data provider enables you to establish a connection to an OLE DB data source, execute commands, and retrieve results from the data source, whereas the SQL Server .NET data provider enables you to establish a connection to Microsoft SQL Server 7.0 or later, execute commands, and retrieve results from the data source.

After having discussed data-related namespaces, it is time for you to learn about the components of ADO.NET.

ADO.NET Components

There are two main components of ADO.NET, which are as follows:

Datasets

.NET data providers

These components are described in the following sections. Figure 13-2 illustrates the ADO.NET components.

Datasets

There might be a situation where your application needs to work with a set of records from the database and these records are contained in multiple tables. After accessing the required data, you might need to group the data for working with it.

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

INTRODUCTION TO ADO.NET

Chapter 13

455

 

 

 

 

property is available for use in DataSet, DataTable, and DataColumn objects, where a DataColumn object represents a column in a DataTable object.

Figure 13-3 illustrates the components of a DataSet object.

 

 

 

 

 

 

 

 

 

 

Data table

 

 

 

 

 

 

 

 

 

Data table

 

 

 

Data table

 

 

 

 

 

 

 

collection

 

 

 

 

 

 

 

 

 

 

Data table

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DataRow

 

 

 

 

 

 

 

 

 

DataRow

 

 

 

DataRow

 

 

 

 

 

 

 

collection

 

 

 

 

 

 

 

 

 

 

DataRow

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DataColumn

 

 

 

 

 

 

 

 

 

DataColumn

 

 

 

DataColumn

 

 

 

 

 

 

 

collection

 

 

 

 

 

 

 

 

 

 

DataColumn

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

DataRelation

 

 

 

 

 

 

 

 

 

DataRelation

 

 

 

DataRelation

 

 

 

 

 

 

 

collection

 

 

 

 

 

 

 

 

 

 

DataRelation

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

UniqueConstraint

DataConstraint

collection

ForeignKeyConstraint

FIGURE 13-3 Components of a DataSet object

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

Data-

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.