|
|
|
|
Connection and Command Object
The Connection Object allows ADO to work in a stateless environment. It stores the information required to connect to the data
source. The Connection Object is instantiated using the Server . CreateObject statement. As with all objects, you have to use the Set keyword when assigning it to an object variable.
Once the object has been instantiated, you can use this to create connections to the database using the Open method. You need to provide it the necessary Connection String or
DSN. This attaches the information required to connect to the database (the database, user Id and password) with the Connection Object.
Once the connection is open, you can start communicating with the database. When you are finished, you should close the connection using the Close method.
Closing the connection does not destroy the Connection Object and it can be opened again., Even if you do not close the connection, ODBC will close the connection after 60 seconds of inactivity. If you are sure that you will not require the Connection Object any more, you can destroy it by setting it to Nothing. The Connection Object can be used to retrieve a recordset. However, this would be a very low level' cursor. The cursor is read-
only and allows only forward movement through it, which means that you can move from the current record to those after it, not before. This kind of a cursor can be used when you don't wish to update the database and simply display a series of records. CommandText is a string containing the command to be executed on the database. The optional RecordsAffected is a long variable to which the provider returns the
number of records that the operation affected. Options, again optional, is a long value that indicates how the provider should evaluate
CommandText.
<% The Command Object
optimizes performance of parameterised queries and stored
procedures. The Command Object is instantiated with the CreateObject method. You can connect to a data source using the Command Object with or without a Connection Object. To set its connection over a Connection object, assign the Connection Object to ActiveConnection property of the Command Object: Set objMyCommand.ActiveConnection = objMyConn Assigning a DSN: Assigning a connection string: The Command Object can return read-only cursors, much like the Connection Object. The main use of the Command Object is to access queries and stored procedures within the database. For executing a command that does not return a recordset, use the following syntax:
The Command object has a parameters collection made up of Parameter objects. Using the Refresh method on the Parameters collection retrieves parameter information for the stored procedure specified in the Command Object from the provider. However, this method does not work with all databases (Microsoft Access, for example).
To create a parameter, user the following syntax: All arguments are optional. Name is a string representing the name of the Parameter object. Type is a long value specifying the data type of the Parameter object. Direction is a long value that indicates whether the parameter represents an input parameter, an output parameter, both, or if the parameter is the return value from a stored procedure. Size is a long value specifying the maximum length for the parameter value in characters or bytes. Value is a variant specifying the value for the Parameter object. |