DekGenius.com
Team LiB
Previous Section Next Section

The Command Object

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.

Command Object Properties

CommandText

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.

CommandTimeout

Type: Integer

Attribute: Read/Write

Default: 30

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.

CommandType

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.

Connection

Type: System.Data.[.NET Data Provider].Connection

Attribute: Read/Write

Default: Null

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.

Container

Type: System.ComponentModel.IContainer

Attribute: Read/Write

Default: Null

Description: Defines the component service of which the Command object is a member.

Parameters

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.

Transaction

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.

Command Object Methods

Cancel()

Returns

Void

Parameters

None

Description

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.

Usage

Use this method when you wish to cancel a command that is executing.

CreateParameter()

Returns

System.Data.IDbDataParameter

Parameters

None

Description

This method returns a valid parameter for the Command object.

Usage

Use this method whenever you wish to create a new parameter for a Command object.

Dispose()

Returns

Void

Parameters

None

Description

This method destroys the Command object.

Usage

Use this method when you no longer need the Command object and release the resources that it was occupying.

ExecuteNonQuery()

Returns

Integer: The number of rows affected

Parameters

None

Description

This method executes an SQL query through the Connection object property of the Command object.

Exception

InvalidOperationException: The connection to a data source does not exist, or it exists but is not open.

Usage

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.

ExecuteReader()

Returns

System.Data.IDataReader

Parameters

None

Description

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.

Usage

Use this method when you need to create and populate a DataReader object.

ExecuteScalar()

Returns

System.Object: A data value

Parameters

None

Description

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.

Usage

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.

ExecuteXmlReader()

Returns

System.Xml.XmlReader: A data value

Parameters

None

Description

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.

Usage

Use this method when you need to retrieve data as XML.

GetType()

Returns

System.Type: A value indicating the type of the Command object

Parameters

None

Description

This method returns a valid type for the Command object.

ToString()

Returns

String: A valid representation of the Command object as a string

Parameters

None

Team LiB
Previous Section Next Section