ADO.NET includes many objects you
can use to work with data. This section introduces some of the primary
objects you will use. Over the course of this tutorial, you'll be exposed
to many more ADO.NET objects from the perspective of how they are used in a
particular lesson. The objects below are the ones you must know.
Learning about them will give you an idea of the types of things you can do
with data when using ADO.NET.
The SqlConnection Object
To interact with a data base, you
must have a connection to it. The connection helps identify the data base
server, the data base name, user name, password, and other parameters that are
required for connecting to the data base. A connection object is used by
command objects so they will know which data base to execute the command on.
The SqlCommand Object
The process of interacting with a
data base means that you must specify the actions you want to occur. This
is done with a command object. You use a command object to send SQL
statements to the data base. A command object uses a connection object to
figure out which data base to communicate with. You can use a command
object alone, to execute a command directly, or assign a reference to a command
object to an SqlDataAdapter, which holds a set of commands that work on a group
of data as described below.
The SqlDataReader Object
Many data operations require that
you only get a stream of data for reading. The data reader object allows
you to obtain the results of a SELECT statement from a command object.
For performance reasons, the data returned from a data reader is a fast forward-only
stream of data. This means that you can only pull the data from the
stream in a sequential manner. This is good for speed, but if you need to
manipulate data, then a DataSet is a better object to work with.
The DataSet Object
DataSet objects are in-memory
representations of data. They contain multiple Datatable objects, which
contain columns and rows, just like normal data base tables. You can even
define relations between tables to create parent-child relationships. The
DataSet is specifically designed to help manage data in memory and to support
disconnected operations on data, when such a scenario make sense. The
DataSet is an object that is used by all of the Data Providers, which is why it
does not have a Data Provider specific prefix.
The SqlDataAdapter Object
Sometimes the data you work with is
primarily read-only and you rarely need to make changes to the underlying data
source. Some situations also call for caching data in memory to minimize
the number of data base calls for data that does not change. The data
adapter makes it easy for you to accomplish these things by helping to manage
data in a disconnected mode. The data adapter fills a DataSet object when
reading the data and writes in a single batch when persisting changes back to
the data base. A data adapter contains a reference to the connection
object and opens and closes the connection automatically when reading from or
writing to the data base. Additionally, the data adapter contains command
object references for SELECT, INSERT, UPDATE, and DELETE operations on the
data. You will have a data adapter defined for each table in a DataSet
and it will take care of all communication with the data base for you.
All you need to do is tell the data adapter when to load from or write to the
data base.