Command Object Overview
The Command
object is the heart of data processing
with ADO.NET. Typically, the Command
object wraps
a SQL statement or a call to a stored procedure. For example, you
might use a Command
object to execute a SQL
UPDATE, DELETE, INSERT, or SELECT statement. However, ADO.NET
providers that don’t represent databases may use
their own nomenclature. The only rule is that the
Command.CommandText
property, which defines the command,
must be a string.
As with the Connection
object, the
Command
object is specific to the data provider.
Two examples are:
System.Data.SqlClient.SqlCommand
executes commands against SQL Server Version 7.0 or later.System.Data.OleDb.OleDbCommand
executes commands against an OLE DB data provider.
Each Command
object implements the
System.Data.IDbCommand
interface. That means it is
guaranteed to support the members shown in Tables 4-1 and 4-2. At a
minimum, you must set the CommandText
and a
reference to a valid Connection
before using a
Command
. In addition, you must modify the
CommandType
default value if you wish to invoke a
stored procedure.
Member |
Description |
CommandText |
Contains the SQL statement, stored procedure name, or table name. For an unusual provider (one that doesn’t work with a database), this can contain something entirely different and proprietary; the only requirement is that is must be formatted as a string. |
CommandTimeout |
The amount of time (in seconds) to wait for a command to complete before giving up and throwing an exception. The default is 30 seconds. |
CommandType |
Indicates the format used for the |
Connection |
References the |
Parameters |
A collection of input, output, or bidirectional parameter objects. This is used only for parameterized queries or stored procedure calls. |
Transaction |
Gets or sets the transaction that this command is part of. Transactions are examined in Chapter 16. |
UpdatedRowSource |
Specifies how this command updates a data source when it is used with
a |
Get ADO.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.