Asp Command Object
Command Object Methods | Command Object Properties |
The Asp Command object
is used for executing process against Data store.These can be Commands that
is used to query a database and return records in a Recordset object, to execute
a bulk operation, or to manipulate the structure of a database.
To create a command
object use following syntax:
<%
Set objCommand = Server.CreateObject(“ADODB.Command”) %>
To
open a connection with data store set the ActiveConnection property to a valid
connection string. If you attempt to call the Execute method on a Command object
before setting this property to an open Connection object or valid connection
string, an error occurs.
<%
ObjCommand.ActiveConnection = ConnectionString %>
To
run the command use following syntax:
<%
ObjCommand.Execute RecordsAffected,Parameters,Options %>
All
arguments are optional.RecordsAffected is the number of records affected by
the query,Parameters allows you to write generic commands and pass in only the
information that changes,option is the type of command being run.
There
exists commands that return recordsets and that do not return any value.There
is nothing in the execute method specifies the command to run.The CommandText
property is used to set or return a relative URL that specifies a resource,
such as a file or directory. This could be an SQL statement, a table name,
a relative URL, or a stored procedure call.If the CommandText property specifies
a row-returning query, any results that the execution generates are stored in
a new Recordset object.
<%
objCommand.CommandText=”Person”
Set objRecordset = objCommand.Execute( ,
, adCmdTable )
%>
In
the above example the CommandText is a table name.There is another way to specify
the type of command with the CommandType property prior to execution to optimize
performance.This could be adCmdText, adCmdTable , adCmdStoredProcedure.If the
CommandType property value equals adCmdUnknown (the default value), you may
experience diminished performance because ADO must make calls to the provider
to determine if the CommandText property is an SQL statement, a stored procedure,
or a table name.If the CommandType property does not match the type of command
in the CommandText property, an error occurs when you call the Execute method.
<%
Set objCommand = Server.CreateObject(“ADODB.Command”)
ObjCommand.ActiveConnection = ConnectionString
objCommand.CommandText=”Person”
objCommand.CommandType= adCmdTable
Set objRecordset = objCommand.Execute
%>
The
above will perform the same task as open method
of recordset object. So we can open recordset as follows.
<%
Set objCommand = Server.CreateObject(“ADODB.Command”)
ObjCommand.ActiveConnection = ConnectionString
objCommand.CommandText=”Person”
objCommand.CommandType= adCmdTable
objRecordset.Open objCommand
%>
Asp Command Object Methods
Method | Description |
Execute | Executes
the query, SQL statement, or stored procedure specified in the CommandText property.We have seen that in the previous session. |
Cancel | Cancels execution
of a pending, asynchronous method call. |
Properties
Property | Description |
ActiveConnection | Indicates
to which Connection object the specified Command object currently belongs. |
CommandText | set or return
the text of a command represented by a Command object. This could be an SQL statement, a table name, a relative URL, or a stored procedure call. |
CommandType | specifies
the type of command object.This could be adCmdText ,adCmdTable , adCmdStoredProcedure.If the CommandType property value equals adCmdUnknown (the default value), you may experience diminished performance because ADO must make calls to the provider to determine if the CommandText property is an SQL statement, a stored procedure, or a table name. |
CommandTimeOut | Sets or returns
a Long value in seconds which indicates how long to wait for a command to execute. Default value is 30.If you set the property to zero, ADO will wait indefinitely until the execution is complete. |
State | Indicates
whether the state of the object is open or closed.The object’s State property can have a combination of values. For example, if a statement is executing, this property will have a combined value of adStateOpen and adStateExecuting. |