The ADO Object Model

 < Day Day Up > 



There are three main objects in the ADO object model. (See Figure 23-2.) The Connection object represents the information needed to establish the connection between the data consumer and the data provider. The Command object contains information about an SQL statement or other database command that will be executed by the database server. The Recordset object contains the rows from the virtual table that was created by the Command object.

click to expand
Figure 23-2: A few key objects comprise the ADO object model.

In addition to these objects, there are also a few other objects that support these main objects. Those supporting objects are the Error object, which contains information about any errors that occur, and the Parameter object, which contains information about any parameters associated with the Command object. The Field object contains information about a column returned in the Recordset object.

Using the ADO Object Model

There are two basic scenarios for using ADO depending on the type of command you want to execute. If the command doesn’t return any rows, the following scenario works well.

  1. Create a Connection object to connect to the desired database.

  2. Open the Connection object.

  3. Prepare a Command object that contains the database command to be executed.

  4. Add any necessary Parameter objects to the Command object, including the values to be passed to the command. Remember that not all commands require parameters.

  5. Set the ActiveConnection property to the Connection object.

  6. Execute the Command object.

  7. Close the Connection object.

If the command returns rows from the database, you should use this approach:

  1. Create a Connection object to connect to the desired database.

  2. Open the Connection object.

  3. Prepare a Command object that contains the database command to be executed.

  4. Add any necessary Parameter objects to the Command object, if the Command object has parameters.

  5. Set the ActiveConnection property to the Connection object.

  6. Create a new Recordset object with all the options that you plan to use.

  7. Set the ActiveCommand property in the Recordset to the Command object that you created.

  8. Open the Recordset object.

  9. Retrieve the rows from the Recordset object.

  10. Close the Recordset object.

  11. Close the Connection object.



 < Day Day Up > 



Microsoft Excel 2003 Programming Inside Out
Microsoft Office Excel 2003 Programming Inside Out (Inside Out (Microsoft))
ISBN: 0735619859
EAN: 2147483647
Year: 2006
Pages: 161

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net