A command object is used for executing a query against the database. ADO command objects are equivalent to DataAdapters that hold the actual query. It needs an established connection, it's command type and commandtext properties to be set before calling its Execute method.
The SQLCommand object is analogous to the command object in ADO. We also need a connection for execution. The DataAdapter supports read, add, update and delete operations and for these it supports following properties:
- SelectCommand
- UpdateCommand
- InsertCommand
- DeleteCommand
Actually, these properties are objects. To be mores specific, these are instances of the SQLCommand object. Unless otherwise specified, the appropriate objects are generated at runtime automatically. The SelectCommand can be edited at design time while configuring the DataAdapter. The command objects can be executed independently by using their execute methods. Important properties of the object are CommandType and CommandText. If the CommandType property is set to StoredProcedure, then CommandText property should be set to the name of the stored procedure, otherwise the CommandText property contains the actual query.
To see a command working, let's use following piece of code:
Dim sqlSttg As String = "SELECT * FROM Publishers" Dim cmd As New OleDBCommand (cmd.CommandText = sqlSttg cmd.ActiveConnection = Conn |
[Note] This code assumes that you have a connection to data (Conn open as discussed in the previous example). [End Note]