DataAdapter
DataAdapter
classes are provider-specific types
that act as a bridge between a data source (such as a SQL Server
database) and the ADO.NET System.Data.DataSet
.
DataAdapter
s have two responsibilities: to
retrieve the results of a query and place it in a
System.Data.DataSet
(when you call the
Fill( )
method), and to reconcile changes in the
System.Data.DataSet
and apply changes back to the
data source (when you call the Update( )
method.
An ADO.NET provider includes its own custom
DataAdapter
(such as
System.Data.SqlClient.SqlDataAdapter
).
Unfortunately, DataAdapter
classes have a complex
inheritance hierarchy. The base functionality is defined by two
interfaces: System.Data.IDataAdapter
(which
defines the Update( )
and Fill( )
methods) and, for relational database providers, a
System.Data.IDbDataAdapter
(which defines the
command properties used for interacting with the data source). The
relational database provider-specific DataAdapter
implements only one of these interfaces directly
(System.Data.IDbDataAdapter
). It implements the
other indirectly by deriving from DbDataAdapter
,
which in turn derives from this class. This situation is illustrated
more clearly in Figure 36-2. This allows some basic
DataAdapter
functionality to be implemented in
this class (so the provider-specific DataAdapter
doesn’t need to). It also allows the
provider-specific class to use strongly typed
Command
properties but still support generic
access to these properties as
System.Data.IDbCommand
objects (thanks to the
interface).
Generally, you won’t ever use the
DataAdapter
class directly. If you want to create
generic data-access code that can use different provider-specific
ADO.NET objects, you always cast the DataAdapter
to the System.Data.IDbDataAdapter
interface. Note
that the DataAdapter
class contains a mix of
abstract members the provider-specific class must override and
members that need no alteration (mainly, its properties). Some
properties that are implemented in this class include
TableMappings
(which maps data source table and
field names to table and field names in the
System.Data.DataSet
) and
ContinueUpdateOnError
(which, if
true
, continues updating even if some changes
can’t be committed and stores an error message in
the corresponding System.Data.DataRow.RowError
property). You can also set MissingMappingAction
to tell the DataAdapter
what to do if it finds a
column or table that doesn’t have a mapping (by
default, it adds it using the name used in the data source) and
MissingSchemaAction
to tell the
DataAdapter
what to do when adding tables to a
System.Data.DataSet
that doesn’t
have a schema (it can retrieve schema information automatically,
throw an exception, or simply add the table without schema
information, which is the default).
public abstract class DataAdapter : System.ComponentModel.Component, System.Data.IDataAdapter { // Protected Constructors protected DataAdapter( ); protected DataAdapter(DataAdapteradapter
); // Public Instance Properties public bool AcceptChangesDuringFill{set; get; } public bool ContinueUpdateOnError{set; get; } public MissingMappingAction MissingMappingAction{set; get; } // implements System.Data.IDataAdapter public MissingSchemaAction MissingSchemaAction{set; get; } // implements System.Data.IDataAdapter public DataTableMappingCollection TableMappings{get; } // Public Instance Methods public abstract int Fill(System.Data.DataSetdataSet
); // implements System.Data.IDataAdapter public abstract DataTable[ ] FillSchema(System.Data.DataSetdataSet
, System.Data.SchemaTypeschemaType
); // implements System.Data.IDataAdapter public abstract IDataParameter[ ] GetFillParameters( ); // implements System.Data.IDataAdapter public abstract int Update(System.Data.DataSetdataSet
); // implements System.Data.IDataAdapter // Protected Instance Methods protected virtual DataAdapter CloneInternals( ); protected virtual DataTableMappingCollection CreateTableMappings(); protected override void Dispose(booldisposing
); // overrides System.ComponentModel.Component protected virtual bool ShouldSerializeTableMappings( ); }