Contents Index Topic Contents |
Command Object
A Command object is a definition of a specific command that you intend to execute against a data source.
Collections
Methods
Properties
ActiveConnection, CommandText, CommandTimeout, CommandType, Name, Prepared, State
Remarks
Use a Command object to query a database and return records in a Recordset object, to execute a bulk operation, or to manipulate the structure of a database. Depending on the functionality of the provider, some Command collections, methods, or properties may generate an error when referenced.
With the collections, methods, and properties of a Command object, you can do the following:
- Define the executable text of the command (for example, an SQL statement) with the CommandText property.
- Define parameterized queries or stored-procedure arguments with Parameter objects and the Parameters collection.
- Execute a command and return a Recordset object if appropriate with the Execute method.
- Specify the type of command with the CommandType property prior to execution to optimize performance.
- Control whether or not the provider saves a prepared (or compiled) version of the command prior to execution with the Prepared property.
- Set the number of seconds a provider will wait for a command to execute with the CommandTimeout property.
- Associate an open connection with a Command object by setting its ActiveConnection property.
- Set the Name property to identify the Command object as a method on the associated Connection object.
- Pass a Command object to the Source property of a Recordset in order to obtain data.
Note To execute a query without using a Command object, pass a query string to the Execute method of a Connection object or to the Open method of a Recordset object. However, a Command object is required when you want to persist the command text and re-execute it, or use query parameters.
To create a Command object independently of a previously defined Connection object, set its ActiveConnection property to a valid connection string. ADO still creates a Connection object, but it doesn't assign that object to an object variable. However, if you are associating multiple Command objects with the same connection, you should explicitly create and open a Connection object; this assigns the Connection object to an object variable. If you do not set the Command objects' ActiveConnection property to this object variable, ADO creates a new Connection object for each Command object, even if you use the same connection string.
To execute a Command, simply call it by its Name property on the associated Connection object. The Command must have its ActiveConnection property set to the Connection object. If the Command has parameters, pass values for them as arguments to the method.
See Also
Using OLE DB Providers with ADO
Top of Page
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.