Command objects are used to execute commands across a database connection
Command objects provide 3 methods that are used to exceute commands on the database
This can be used to execute stored procedures in the database.

Creating a Command Object

sSQLQuery = "SELECT Name FROM Clients WHERE ClientID=1" 

objCommand = New System.Data.OleDb.OleDbCommand
objCommand.Connection = objConnectionObject
objCommand.CommandText = sSQLQuery

lNoOfRecords = objCommand.ExecuteNonQuery()

Alternatively you could pass these two parameters to the constructor.

Dim objCommand As New System.Data.OleDb.OleDbCommand(sSQLQuery, objConnectionObject) 
lNoOfRecords = objCommand.ExecuteNonQuery()

sSQLQuery = "SELECT Name FROM Clients WHERE ClientID=1" 
sName = objCommand.ExecuteScalar().ToString


CommandTextThe SQL text for the query.
CommandTimeoutThe number of seconds after which the query times out. The default is 30 seconds.
CommandTypeAn enumerated value that specifies the type of the query: Text or StoredProcedure.
ConnectionThe connection object associated with this command.
ParamatersThe collection of parameters associated with this command.
TransactionThe transaction object corresponding to the transaction in which this command is executing.
UpdatedRowSourceSpecifies how command results are applied to the DataRow object. This is only relevant when a command is associated with a DataAdapter that performs an update.


ExecuteNonQueryExecutes commands that have no return value, eg INSERT, UPDATE, DELETE. This returns the number of rows affected.
CancelCancels the execution of the command object.
CreateParameterCreates a parameter object connected to this parameterized command.
ExecuteReaderExecutes a SELECT command that returns a resultset. This method returns a DataReader object. It is also possible to pass a CommandBehavior argument for extra control).
ExecuteScalarExecutes a SELECT command and returns the scalar value in the first column of the first row.
PrepareCreates a compiled version of the command on the data source. Can only be used with stored procedures.
ResetCommandTimeoutResets the CommandTimeOut property to its default of 30 seconds.

Changing the Timeout

The CommandTimeout is not inherited from the ConnectionTimeout.
Setting the CommandTimeout equal to zero (0) indicates no timeout.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext