Modifying Data  «Prev  Next»

Lesson 6Direct access to the database with Data Environment
ObjectiveUse the Data Environment object to directly execute commands.

Direct Database Access

You have seen, so far, how to use the Recordset Design Time Control and data bound controls to include database access to theVirtualBookShelf.com project.
These controls are easy to implement, taking just a few moments to drag and drop them into a Web page.
You can also write script that creates database access in a more direct manner. Perhaps you want, or need, to design a custom interface rather than using that provided by the DTCs in Visual InterDev.
Also, by writing your own script, the size of the pages remain smaller and are, therefore, faster to load.
You can use the Data Environment (DE) object to create script-based database access. The Data Environment contains the information necessary to access a database and is defined in the Global.asa file. You will recall that you added a DE to a project in Module II and have been using it since then.
To script the DE, you need a database connection and one or more command objects. The command objects are used to access a database object. Additionally, the Scripting Object Model (SOM) must be enabled for the page. The SOM is a collection of objects that you use to script the application. Enabling the SOM gives you access to these objects.

The following MouseOver shows script that creates a DE object and uses it to create a recordset object. It is part of a file called DE.asp, which you can download from the Resources page for on-your-own experimenting.

Apply, Filter, Sort
  1. This statement creates a reference to the DE object. The PageObject's CreateDE method is used. The Data Environment is named DE when it is created. Now it can be referenced in this page via the name.
  2. This statement executes the CustTable command object. The CustTable command object returns all the rows in CustTable
  3. set rs = DE.rsCustTable creates a recordset object to reference the command objects result set, which is all the rows in the table.
  4. This statement uses the recordset object's moveFirst to make sure we're at the first row. Now, recordset object methods can be used to navigate in and manipulate the data. The file DE.asp includes script to display the recordset in an HTML table

Scripting Object
The next lesson will wrap up this module and include a quiz.