The command objects are used to connect to the Datareader or dataset objects with the help of the following methods:
ExecuteNonQuery:
This method executes the command defined in the CommandText property.
The connection used is defined in the Connection property for a query.
It returns an Integer indicating the number of rows affected by the query.
ExecuteReader:
This method executes the command defined in the CommandText property.
The connection used is defined in the Connection property.
It returns a reader object that is connected to the resulting rowset within the database, allowing the rows to be retrieved.
ExecuteScalar:
This method executes the command defined in the CommandText property.
The connection used is defined in the Connection property.
It returns a single value which is the first column of the first row of the resulting rowset.
The rows of the rest of the result are discarded.
It is fast and efficient in cases where a singleton value is required.
Command objects are used to execute the queries, procedures. Sql statements etc. It can execute stored procedures or queries that use parameters as well.
It works on the basis of certain properties like ActiveConnection, CommandText, CommandType, Name etc.
Command object has three methods:
a) Execute: executes the queries, stored procedures etc.
b) Cancel: stops the method execution
c) CreateParameter: to create a parameter object