Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

Session 19—Using DataReaders

195

REVIEW

The ADO.NET DataReader objects, OleDbDataReader and SqlDataReader, are a collection of rows retrieved from a data source. The DataReader object provides forward-only navigation and must always be connection to your data source via a Connection object. While a DataReader object is in use, no other operations can be performed on its associated Connection object. To create a DataReader object, you must call the ExecuteReader method of the OleDbCommand or SqlCommand object rather than directly using a constructor.

QUIZ YOURSELF

1.What is the main function of a DataReader object? (See “Introducing DataReaders.”)

2.How is a DataReader object constructed? (See “Introducing DataReaders.”)

3.Can a DataReader object be disconnected from its Connection object? (See “Introducing DataReaders.”)

S E S S I O N

20

Introducing DataSets, Part I

Session Checklist

Understanding the function of the DataSet objects in ADO.NET

Creating a DataSet using the DataAdapter objects

Thus far in our discussion of ADO.NET, we have covered the Connection, Command, and DataReader objects. In this session, we will cover the DataSet object, probably the most innovative and exciting of the ADO.NET objects. The DataSet object, much like a

DataReader, is designed to handle the actual data from a database. The DataSet also provides access to multiple tables, rows, and columns. Figure 20-1 illustrates the hierarchy of DataSet objects.

Each DataSet object can contain multiple tables, DataTable objects, as well as the relationships, DataRelation objects, between these tables. A DataSet object is effectively an off-line copy of your data store. The major advantage of the dataset is that because it’s designed for disconnected data, you can pass multiple tables, along with their relationships, around the tiers of your Web application. Not only can you pass DataSet objects around your Web application, you can also pass them to other systems for processing in the form of XML, and then retrieve and update your data store with the updated DataSet. The DataSet object makes the promise of disconnected data a reality.

In our discussion of DataSet objects, we will introduce several other objects that are very closely related to the dataset, including the DataAdapter, DataTableMapping, DataView,

DataRelation, and DataTable objects.

In the past, data processing has been primarily connection-based. Now, in an effort to make multi-tier applications more efficient, data processing in general and particularly ASP.NET data processing is turning to a message-based approach that revolves around chunks of data in the form of datasets. At the center of this approach is the DataAdapter object, which provides a bridge to retrieve and update data between a DataSet and a data store. The DataAdapter object provides this bridge via the Fill method to load data from a data source into the DataSet and uses the Update method to send changes made in the DataSet to the data source.

198

Saturday Evening

Data Set

DataTableCollection

DataTable

DataColumnCollection

DataColumn

DataRowCollection

DataRow

ConstraintCollection

Constraint

DataRelationCollection

DataRelation

Figure 20-1 Dataset object model

Note

Again Microsoft has provided two flavors of the DataAdapter, SqlDataAdapter and OleDbDataAdapter. If you are connecting to a SQL Server database, you can use the SqlDataAdapter object in conjunction with the SqlConection and SqlCommand objects to increase overall performance. Throughout this session, however, we will use the OleDbDataAdapter object in the examples.