The Command
class is
provided by all standard ADO.NET providers, and it almost always
encapsulates a SQL statement or a stored procedure call that can be
executed against a data source. Command
objects
can retrieve rows; directly insert, delete, or modify records;
calculate totals and averages; alter the structure of a database; or
fill a disconnected DataSet
when used with a
DataAdapter
.
At a bare minimum, every Command
must reference a
Connection
object, which it uses to communicate
with the data source and define a few key properties, such as
CommandText
(the stored procedure or embedded SQL
command) and CommandType
. The
Command
class is provided in several
provider-specific varieties, including SqlCommand
and OleDbCommand
.
To execute a Command
, you use one of the
Command
object methods, including
ExecuteNonQuery( )
, ExecuteReader( )
, and ExecuteScalar( )
, depending on
the type of Command
. Occasionally, a provider may
define an additional method, such as the ExecuteXmlReader( )
method offered by the SQL Server provider, which
retrieves query results as an XML document. Other than this
discrepancy, the Command
objects provided by the
core set of ADO.NET providers are virtually identical.