The Command Object
The Command object represents a command. The ResultSet
and the Connection objects execute SQL commands which are
command strings. Instead of the Command string a Command
object can be used. The Command string can be anything from
a simple SQL command to a complex stored procedure. But this
does not make the Command object anything special as the RecordSet
can do all this too. The Command object is best used for
complex stored procedures with in parameters and out parameters.
The Command object can be used to execute bulk operations and
manipulate the structure of the database.
Click here for code example 5.
Command object methods and properties:
The first step is to specify the ActiveConnection we want to
use as specified in line 3. Once we have established the active
connection we can use the Command object in the same way as the
Connection object. We don't need to explicitly open the data
source or close it after use, we can use the execute method
directly as in line 6.
The different properties of the Command object help us
specify some parameters outside the Execute method to get more
control over the operation; like in line 4 we have specified
the Command text and in line 5 we have indicated the Command
Type as a stored Procedure. We can also specify the
CommandTimeout property. This determines how long ADO has
to wait for the command to execute. The default value is 30.
If the value is set to 0 ADO will wait indefinitely until the
command executes.
The command object has methods like CreateParameter which
creates a new parameter object in the Parameters collection and
an Execute method that runs the SQL statement or stored
procedure specified in the CommandText.