ADO.NET is an
evolution of the ADO
data access model that directly addresses user requirements for developing
scalable applications. It was designed specifically for the web with
scalability, statelessness, and XML in mind.
ADO.NET uses some ADO objects, such as the Connection
and Command objects, and also introduces new objects. Key new ADO.NET
objects include the DataSet, DataReader, and DataAdapter.
The important
distinction between this evolved stage of ADO.NET and previous data
architectures is that there exists an object -- the DataSet -- that is
separate and distinct from any data stores. Because of that, the DataSet
functions as a standalone entity. You can think of the DataSet as an always
disconnected recordset that knows nothing about the source or destination of
the data it contains. Inside a DataSet, much like in a database, there
are tables, columns, relationships, constraints, views, and so forth.
A DataAdapter
is the object that connects to the database to fill the DataSet. Then,
it connects back to the database to update the data there, based on operations
performed while the DataSet held the data. In the past, data processing
has been primarily connection-based. Now, in an effort to make multi-tiered
apps more efficient, data processing is turning to a message-based approach
that revolves around chunks of information. At the center of this approach is
the DataAdapter, which provides a bridge to retrieve and save data
between a DataSet and its source data store. It accomplishes this by
means of requests to the appropriate SQL commands made against the data store.
The XML-based DataSet
object provides a consistent programming model that works with all models of data
storage: flat, relational, and hierarchical. It does this by having no
'knowledge' of the source of its data, and by representing the data that it
holds as collections and data types. No matter what the source of the data
within the DataSet is, it is manipulated through the same set of
standard APIs exposed through the DataSet and its subordinate objects.
While the DataSet has no
knowledge of the source of its data, the managed provider has detailed and
specific information. The role of the managed provider is to connect, fill, and
persist the DataSet to and from data stores. The OLE DB and SQL Server
.NET Data Providers (System.Data.OleDb and System.Data.SqlClient) that are part
of the .Net Framework provide four basic objects: the Command, Connection,
DataReader and DataAdapter. In the remaining sections of this
document, we'll walk through each part of the DataSet and the OLE DB/SQL
Server .NET Data Providers explaining what they are, and how to program against
them.
The following sections will introduce
you to some objects that have evolved, and some that are new. These objects
are:
·
Connections. For connection to and managing
transactions against a database.
·
Commands. For issuing SQL commands against a
database.
·
DataReaders. For reading a forward-only stream of
data records from a SQL Server data source.
·
DataSets. For storing, Remoting and programming
against flat data, XML data and relational data.
·
DataAdapters. For pushing data into a DataSet,
and reconciling data against a database.
When dealing
with connections to a database, there are two different options: SQL Server
.NET Data Provider (System.Data.SqlClient) and OLE DB .NET Data Provider
(System.Data.OleDb). In these samples we will use the SQL Server .NET Data
Provider. These are written to talk directly to Microsoft SQL Server. The OLE
DB .NET Data Provider is used to talk to any OLE DB provider (as it uses OLE DB
underneath).
Connections:
Connections are used to 'talk to'
databases, and are represented by provider-specific classes such as SqlConnection.
Commands travel over connections and resultsets are returned in the form of
streams which can be read by a DataReader object, or pushed into a DataSet
object.
Commands:
Commands contain the information that is submitted to a
database, and are represented by provider-specific classes such as SqlCommand.
A command can be a stored procedure call, an UPDATE statement, or a statement
that returns results. You can also use input and output parameters, and return
values as part of your command syntax. The example below shows how to issue an
INSERT statement against the Northwind database.
DataReaders:
The
DataReader object is somewhat synonymous with a read-only/forward-only
cursor over data. The DataReader API supports flat as well as hierarchical
data. A DataReader object is returned after executing a command against
a database. The format of the returned DataReader object is different
from a recordset. For example, you might use the DataReader to show the
results of a search list in a web page.
No comments:
Post a Comment