Databases Reference
In-Depth Information
•
The
DataReader
object is optimized for retrieving large amounts of data at a
fast speed. Data is read-only and can only be fetched in a forward-only order.
Memory usage is minimal.
•
The
DataSet
object is a cache of data that represents the complete set of data
results, including related tables, constraints, and relationships among the
tables. It is, in effect, a locally cached database. You can modify the data in
the
DataSet
and fetch data in any order. Because the
DataSet
is discon-
nected from the database, any changes you make to the data in the
DataSet
must be explicitly synchronized with the data in the database. You also can
create a
DataSet
from an XML stream or document, or you can serialize a
DataSet
to XML. Memory usage is high.
Performance Tip
If you need to retrieve large amounts of read-only data, a
DataReader
object always provides the best performance. Only use a
DataSet
object
if you need to insert, update, or delete data, fetch data in any order, or
work with XML. Although the flexibility of a
DataSet
can benefit your
application, it comes with the high cost in memory consumption.
The .NET API provides
the following methods of
fetching data from a
DataReader
:
•
Generic data type methods, such as
GetValue()
and
GetValues()
•
Specific data type methods, such as
GetDateTime()
,
GetDecimal()
, and
GetInt32()
When using a generic method such as
GetValue()
to fetch data from the
DataReader
, extra processing is required to convert the value data type to a ref-
erence data type, essentially wrapping the value data type with an object. This
process is called
boxing
. When boxing occurs, memory is allocated from the
managed heap on the database client to create an object for the reference data
type, which can force a garbage collection to occur. See “.NET CLR,” page 82, for
more information about the impact garbage collection has on performance.