The Command object is used to issue SQL commands to the database. Although that means any type of SQL command, ADO .NET was optimized to handle SQL commands differently. When issuing DDL (Data Definition Language) commands or invoking a stored procedure, you are safe using the Command object. DML (Data Manipulation Language) commands are usually handled by the objects inside ADO .NET’s DataAdapter component. This section is a complete reference to the generic Command object in ADO .NET.
Type: String
Attribute: Read/Write
Default: “”
Description: The CommandText property gets or sets the SQL command that is to be executed against the data source. If you wish to execute a stored procedure, simply assigning the name of the stored procedure to this property is enough to define a command.
Type: Integer
Attribute: Read/Write
Description: The CommandTimeout property is an integer value that specifies the number of seconds the Command object should wait for a command to be executed against the database before generating an error.
Type: System.Data.CommandType enumeration
Attribute: Read/Write
Default: Text
Possible values:
Stored Procedure: Interprets the CommandText property as a call to execute a stored procedure in the database
TableDirect: Interprets the CommandText property as the name of a table inside the database. When the Command object is executed, the entire table is retrieved—all its data, plus its schema!
If you intend to retrieve more than one table, use a comma-delimited list of tables without spaces as the CommandText property. All the tables and their schema are retrieved.
Text: Interprets the CommandText property as an SQL command
Description: The CommandType property gets or sets the manner through which the Command object will execute its CommandText property against the data source.
Type: System.Data.[.NET Data Provider].Connection
Attribute: Read/Write
Description: This property defines a valid ADO .NET Connection object that has an open connection to the data source against which you want to execute the command.
Type: System.ComponentModel.IContainer
Attribute: Read/Write
Default: Null
Description: Defines the component service of which the Command object is a member.
Type: System.Data.[.NET Data Provider].Parameter-
Collection
Attribute: Read-only
Default: Null
Description: This property returns the collection of items being passed to the command as parameters. A parameter is set using the CreateParameter() method.
Type: System.Data.[.NET Data Provider].Parameter-
Collection
Attribute: Read/Write
Default: Null
Description: This property gets or sets the valid Transaction object during which this command will execute.
This method attempts to cancel a command if it is currently executing. If the command is not executing, nothing happens. Similarly, if an attempt to halt execution fails, nothing happens.
Use this method when you no longer need the Command object and release the resources that it was occupying.
This method executes an SQL query through the Connection object property of the Command object.
InvalidOperationException: The connection to a data source does not exist, or it exists but is not open.
This is the ideal method to call whenever you issue a DDL command or update without the use of a DataSet. For such commands, the return value is –1.
When used with DML commands such as INSERT, UPDATE, and SELECT, the method returns the number of rows that were affected by the command.
This method executes the CommandText property of the Command object through the Connection object referred to by the Command’s Connection property. Then it builds a valid IDataReader object with the resulting row set.
This method executes the CommandText property of the Command object through its Connection object. Then it returns the value in the first column of the first row of the resulting row set.
Use this method when you need to obtain a single value from the database. A typical example would be to obtain a count of all records in the Customer table.
This method executes the CommandText property of the Command object through its Connection object. Then it returns data as XML and populates a valid XmlReader object.