Database Reference
In-Depth Information
C H A P T E R 15
Using Data Sets and Data Adapters
In Chapter 14, you saw how to use data readers to access database data in a connected, forward-only,
read-only fashion. Often, this is all you want to do, and a data reader suits your purposes perfectly.
In this chapter, you'll look at a new object for accessing data, the
data set.
Unlike data readers,
which are objects of data provider-specific classes that implement the
System.Data.IDataReader
interface, data sets are objects of the class
System.Data.DataSet
, a distinct ADO.NET component used by
all data providers. Data sets are completely independent of and can be used either connected to or
disconnected from data sources. Their fundamental purpose is to provide a relational view of data
stored in an in-memory cache.
Note
In yet another somewhat confusing bit of terminology, the class is named
DataSet
, but the generic term
is spelled
data set
So, if a data set doesn't have to be connected to a database, how do you populate it with data and
save its data to the database? This is where
data adapters
come in. Think of data adapters as bridges
between data sets and data sources. Without a data adapter, a data set can't access any kind of data
source. The data adapter takes care of all connection details for the data set, populates it with data, and
updates the data source.
In this chapter, we'll cover the following:
• Understanding the object model
• Working with data sets and data adapters
• Propagating changes to a data source
• Concurrency
• Using data sets and XML
• Understanding typed and untyped data sets
Understanding the Object Model
We'll start this chapter with a quick presentation of all the new objects you'll need to understand in
order to work with data sets and data adapters. You'll start by looking at the difference between data sets