ObjectStore Active Toolkit Tutorial

Chapter 3

Accessing ATK OLE DB Provider from Active Server Page Applications

Introduction
The OLE DB provider uses a standard interface between an application and a data source that allows you to specify a query using SQL syntax. The queries return tables of data in rows and columns.

With Active Server Pages, you can write server side scripts in VBScript or JavaScript that create and manipulate ActiveX objects, which implement the application's customized behavior. These scripts instantiate COM objects and access the ObjectStore Active Toolkit OLE DB provider through ADO (ActiveX Data Objects), which is a simple, standard object model. The scripts then return data from the ObjectStore Active Toolkit OLE DB provider in standard HTML. Any web browser can process the data from Active Server Pages that use ADOs.

You might prefer to use the ObjectStore Active Toolkit OLE DB provider instead of ATK ActiveX server - because it provides a standard interface, there is no need to become familiar with another object model. The Active Toolkit OLE DB data source is accessible through the standard ADO interface and provides essentially the same functionality as the ATK ActiveX server (that is, it generates a table of data from an ObjectStore database). The ObjectStore Active Toolkit OLE DB source's ability to provide nested row sets from ADO 1.5 gives you flexibility in accessing the underlying ObjectStore database.

By integrating ASP with the ObjectStore Active Toolkit OLE DB provider, you can leverage the strengths of both technologies. ASP lets you access an object-oriented database through a standard interface.

Software requirements
To complete the exercises in this chapter, you need these software resources:
ResourceWhere to Find It
Database

\odi\ATK6.0\Examples\demodbs\
carsdemo.db


Active Server Page source code

\odi\ATK6.0\Examples\Tutorial3\

Active Server Pages

Microsoft Internet Information Server 3.0 or later, or Microsoft Peer Web Services 3.0 or later

Although these sample applications were developed with Microsoft Visual InterDev 1.0, you do not need it to run, build, or modify the source code.

In this chapter
In this chapter, you create Active Server Page applications that use the ATK OLE DB data source to retrieve tabular information from an ObjectStore database. This chapter contains the following exercises:
ExerciseDescription
Build an Application with ATK, ASP, and ADO

Query an ObjectStore database, build a table of data, and display the table in HTML.

Customize a Data View in ADO

Display specific fields from a data view, and let the user navigate among data items in a web browser.

Implement Explicit Navigation in ADO

Query the database and build a table of data that specifically names the classes among which a user can navigate.

Display Multimedia Object Managers Using ADO

Retrieve multimedia objects from an ObjectStore database for display in a web browser.

Write the ASP-ADO Code to Show Multimedia Object Managers

Scan the database for an image and store it. Use the application to retrieve and display the image.

Build an Application with ATK, ASP, and ADO

Overview

This application uses ATK, ASP, and ADO to query an ObjectStore database, build a table, and display the table in HTML. ATK contains the ObjectStore Active Toolkit OLE DB provider, which ADO or any other OLE DB consumer can access. Even if VBScript and JavaScript cannot directly access the OLE DB interface, they can still access OLE DB sources through ADO.

Process

To build an application with ATK, ASP, and ADO, follow these steps:

  • Create a new data view to use in the application.
  • Create the Active Server Page.
  • Access the data source through ADO.
  • Test the application.

    1 Create a new data view to use in the application.

  • Start Inspector.
  • Open the sample database \odi\ATK6.0\Examples\demodbs\carsdemo.db.
  • Click on the service root name in the Database Roots pane of Inspector.
  • Select Data View | Create. An untitled Data View window appears.

  • Select File | Save All. The Save Data View dialog box appears.

  • Name the new data view my_tut3_table1 and click OK. The metaknowledge for the cardsdemo.db database now contains the definition of the new table.

    2 Create the Active Server Page.

    Using Visual InterDev

    You can create an Active Server Page using Visual InterDev or a simple text editor. Procedures for using Visual InterDev are shown here.

  • If you are using Visual InterDev, start that application and select File | New.
  • On the Files sheet of the New dialog box, select Active Server Page and click OK.

    A new window appears; it displays the Active Server Page.

    Tip: If you are using a text editor to create an Active Server Page, create a file with an .asp extension that contains the text and format shown in the preceding window.

    3 Access the data source through ADO.

    You can write VBScript or JavaScript code to create and access ATK ActiveX objects.

    In VBScript

    This VBScript example creates an ADO RecordSet object based on the my_tut3_table1 data view and displays it in HTML.

    <%
    
          On Error Resume Next
    
    'Create the ADO connection
    
          Set adoConnection = Server.CreateObject("ADODB.Connection")
    
    'Open the ADO - OLE DB connection; here you must specify the name
    
    'of the ATK OLE DB Provider and the name of the database
    
          Call adoConnection.Open( _
    
                "provider=ObjectStore Active Toolkit OLE DB Provider;" & _
    
                "data source=c:\odi\ATK6.0\Examples\demodbs\carsdemo.db","","")
    
          If Err.Number<>0 Then  'Something went wrong
    
                Response.Write("Error: " & Err.Description)
    
          Else  'Connection has been correctly opened
    
    'Create an ADO RecordSet
    
          Set adoRS = Server.CreateObject("ADODB.RecordSet")
    
    'And open data view "my_tut3_table1" using the previously opened ADO connection
    
          Call adoRS.Open("my_tut3_table1", adoConnection)
    
          If Err.Number<>0 Then  'Something went wrong
    
                Response.Write("Error: " & Err.Description)
    
          Else 'RecordSet has been correctly opened
    
    'Generate the HTML output for the opened RecordSet
    
                WriteRecordSetTable(adoRS)
    
                End If
    
          End If
    
    %>
    
    The WriteRecordSetTable procedure displays any ADO RecordSet object in HTML:

    <SCRIPT LANGUAGE=VBScript RUNAT=Server> 
    
    Sub WriteRecordSetTable(anADORS)
    
          On Error Resume Next
    
    'Initialize the HTML table
    
          Response.Write("<TABLE BORDER=1>")
    
    'Generate the column headings
    
          For Each colHeading in anADORS.fields
    
                Response.Write("<TH><B><PRE>" & colHeading.Name & "</PRE></B></TH>")
    
          Next
    
    'Scan all the records in the ADO RecordSet
    
          Do While Not anADORS.EOF
    
                Response.Write("<TR>")
    
    'Retrieve all the fields in the current row
    
          For Each aField in anADORS.fields
    
    'Output the cell content
    
                Response.Write("<TD>" & aField.Value & "</TD>")
    
          Next
    
                Response.Write("</TR>")
    
    'Move to the next row in the ADO RecordSet
    
                anADORS.MoveNext
    
          Loop
    
          Response.Write("</TABLE>")
    
    End Sub 
    
    </SCRIPT>
    

    In JavaScript

    You can also access an Active Toolkit OLE DB data source through ADO using JavaScript. Refer to odi\Atk\Examples\Tutorial3\tut3_2.asp for an example that uses the same data view.

    4 Test the application.

  • Save the generated Active Server Page in the INetPub\ASPSamp\ATK\Tutorial3 directory as tut3_1.asp.
  • In a web browser, open the URL http://localhost/aspsamp/atk/tutorial3/tut3_1.asp.

    Optimization

    You can optimize this application by using ASP sessions to cache the ADO connection and RecordSet objects. Refer to odi\ATK6.0\Examples\WebADODemo for an example.

    Customize a Data View in ADO

    Overview

    You can customize a data view in ADO using the SQL syntax accepted by the ObjectStore Active Toolkit OLE DB provider. For example, instead of displaying the fields contained in the data view my_tut3_table1, you can display the itemNumber and description data members of the ServiceItem class, and navigate the orders relation and display the orderNumber of the related WorkOrder instance.

    For more information
    Refer to Chapter 3, Active Toolkit OLE DB Provider, in the ObjectStore Active Toolkit Reference, for more information about how ATK works with SQL code.

    Process

    To customize a data view in ADO, follow these steps:

    1. Select the data members to display.

    2. Specify the column headings and the order of the data.

    1 Select the data members to display.

    To select the data members to display, modify the VBScript line that opens the ADO RecordSet object in tut3_1.asp as shown here:

    Call adoRS.Open( _
    
    "SELECT itemNumber, description, orders#orderNumber FROM my_tut3_table1", _ 
    adoConnection)
    
    This SQL command instructs ATK to display three data members from the items contained in the my_tut3_tab1 data view: itemNumber, description, and orders#orderNumber. The last item contains the navigation of the orders relation.

    Because orders is a one-to-many relationship, this SQL command might generate multiple lines for every ServiceItem contained in the source collection. For example, the ServiceItem whose itemNumber is 7 has two associated WorkOrder instances, as shown above in the generated table.

    2 Specify the column headings and the order of the data.

    In this step, you specify a more complex SQL command to

    You use the following SQL command:

    SELECT itemNumber as itemID, description, orders#orderNumber AS `related order ID' FROM my_
    tut3_table1 ORDER BY description
    
    This is the result of the query:

    Implement Explicit Navigation in ADO

    Overview

    You can build a table that allows the user of an application to navigate from one item to another explicitly. That is, instead of using implicit navigation to access one class through another class, the application accesses each class directly by naming the class. When you navigate with ADO, do not use Field.Value as an integer value. Instead, use it to retrieve a RecordSet that represents the elements related to the object displayed in the current row.

    When you extract HTML tables from a database, you usually want to display the data at many levels of abstraction, and let the user navigate to additional data.

    The application you build in this section demonstrates how you can use ADO 1.5 and later and the ObjectStore Active Toolkit OLE DB provider to create a table that displays all the service items in a database. The user can click on a particular service item to display all of its work orders; this is explicit navigation. To navigate explicitly using ADO, you must use nested RecordSets.

    Using ADO 1.5
    ADO 1.5 and later support chaptered row sets. In this case, the Field.Value objects from the columns of data that provide navigation are themselves RecordSet objects representing the elements related to the object displayed in the current row. When an ADO 1.5 or later consumer accesses the ObjectStore Active Toolkit OLE DB provider, the provider automatically implements explicit navigation through chaptered row sets.

    Process

    To implement explicit navigation, follow these steps:

  • Modify the SQL statement that opens the RecordSet.
  • Modify the procedure that displays the RecordSet.
  • Write a new page that displays the navigated RecordSets.
  • Test the application.

    1 Modify the SQL statement that opens the RecordSet.

    The SELECT commands you have used so far included only implicit navigation among classes. (That is, they handle data members of related classes as if they are data members of the displayed class.) ATK accepts additional SQL syntax, with which you can specify nested SELECT commands. Every nested SQL command is translated into nested RecordSet objects.

    Modifying the SQL
    Suppose you want to display the ServiceItem instances of the data view my_tut3_table1 and provide a link for each row that describes the related WorkOrder instances. In tut3_1.asp, use this SQL command:

    SELECT itemNumber, description, {SELECT orderNumber, car#model FROM orders} FROM tut3_
    table1
    
    The last column specified in the first SELECT command is another nested SELECT directive, in which you specify the data members of the WorkOrder class you want to display (orderNumber and the implicit navigation car#model) and the name of the ServiceItem data member that implements the relation with the WorkOrder class (orders).

    2 Modify the procedure that displays the RecordSet.

    Before running this SQL command, modify the WriteRecordSetTable procedure that you created previously.

  • Copy the tut3_1.asp file to tut3_nav1.asp.
  • To handle the links between the main table and the related subtables, use the Session ASP object. This is the modified and commented code for ADO 1.5 or later:

    Sub WriteRecordSetTable(anADORS)
    
          On Error Resume Next
    
    'Remove any previous navigation information
    
          Session("Navigations")=Empty
    
    'Store the main RecordSet
    
          Set Session("MainRS")=anADORS
    
    'Create an array that will store the navigated RecordSets
    
          Dim  navigatedRS()
    
          ReDim  navigatedRS(anADORS.RecordCount)
    
    'Initialize the HTML table
    
                Response.Write("<TABLE BORDER=1>")
    
    'Generate the column headings
    
          For Each colHeading in anADORS.fields
    
          Response.Write("<TH><B><PRE>" & colHeading.Name & "</PRE></B></TH>")
    
          Next
    
    'Scan all the records in the ADO RecordSet and keep track of the row number
    
          rowNumber=0
    
          anADORS.MoveFirst
    
          Do While Not anADORS.EOF
    
                Response.Write("<TR>")
    
    'Retrieve all the fields in the current row
    
          For Each aField in anADORS.fields
    
    if aField.Type=136, this is a chaptered rowset
    
                If aField.Type=136 Then
    
    'Store the navigated RecordSet
    
                      Set navigatedRS(rowNumber)=aField.Value
    
    'Retrieve the number of records in the navigated RecordSet
    
                      recordCount=navigatedRS(rowNumber).RecordCount
    
                      If recordCount>0 Then
    
    'Create the link to the second page iff recordCount>0
    
                      Response.Write("<TD><A HREF=""tut3_nav2.asp?navPosition=" _
    
                            & rowNumber & """>" & recordCount & " related items</A>")
    
                      Else
    
                            Response.Write("<TD>No Related Items</TD>")
    
                      End If
    
                Else  'The usual cell content
    
                      Response.Write("<TD>" & aField.Value & "</TD>")
    
                End If
    
          Next
    
                Response.Write("</TR>")
    
    'Move to the next row in the ADO RecordSet
    
          anADORS.MoveNext
    
          rowNumber=rowNumber+1
    
          Loop
    
                Response.Write("</TABLE>")
    
    'Store the array of navigated RecordSets in the Session
    
          Session("Navigations")=navigatedRS
    
    End Sub 
    

    3 Write a new page that displays the navigated RecordSets.

  • In a text editor, create a new file that contains this code:
    <%
    
          On Error Resume Next
    
    'Retrieve the position of the navigated RS in the array
    
          navPosition=Request.QueryString("navPosition")
    
    'Retrieve the array of RecordSets
    
          navArray=Session("Navigations")
    
    'Get the adoRS located at the navPosition index in the array
    
          Set adoRS=navArray(navPosition)
    
    'Write the navigated adoRS
    
          WriteRecordSetTable(adoRS)
    
          Set adoRS=Nothing
    
    %>
    
    WriteRecordSetTable is the simple procedure you already wrote in tut3_1.asp; you can copy it to tut3_nav2.asp.

  • Save the file in the InetPub\ASPSamp\ATK\Tutorial3 directory as tut3_nav2.asp.

    4 Test the application.

  • With a web browser, open the Active Server Page http://localhost/ASPSamp/ATK/Tutorial3/tut3_nav1.asp. Your browser should display information similar to that shown here:

  • To navigate, click an orders link that displays one or more related items: The browser content changes to reflect the order you clicked.

    Display Multimedia Object Managers Using ADO

    Overview

    The ObjectStore Active Toolkit OLE DB provider fully supports multimedia Object Managers. From an Active Server Page, you can use ADO and ATK to access multimedia Object Managers stored in an ObjectStore database.

    This sample application is an ASP page that displays a data view consisting of multimedia Object Managers that store images.

    Process

    To display multimedia Object Managers using ADO, check the data view that contains the multimedia images:

  • Start Inspector.
  • Open the sample database c:\odi\ATK6.0\Examples\demodbs\extrademo.db.
  • Select Data View | Open. The Open Data View dialog box appears.

  • Select the allimages data view and click the OK button. The Data View window appears with the images data view displayed.

    The data view contains a collection of instances of the Image Object Manager, osmmVirageImage.

  • To check the content of the data view, double-click on an element to display it:

    The extrademo.db database contains a data view called allimages that contains Object Managers. Use this data view to display images on an ASP page.

    Write the ASP-ADO Code to Show Multimedia Object Managers

    Overview

    This example is similar to the explicit navigation example described in Implement Explicit Navigation in ADO. The code sample described in this section scans the RecordSet containing the images, stores the image data in an array referenced by a Session ASP object, and then uses this object in the referred page to draw the image.

    Process

    To show multimedia object managers, follow these steps:

  • Create a new ASP page to retrieve the data.
  • Create another ASP page to display the data.
  • Test the application.

    1 Create a new ASP page to retrieve the data.

  • Using a text editor, create a document that contains the following ASP page code. This code creates the ADO connection and opens the proper RecordSet.
    <%
    
          On Error Resume Next
    
    'Create the ADO connection
    
          Set adoConnection = Server.CreateObject("ADODB.Connection")
    
    'Open the ADO - OLE DB connection; here you must specify the name
    
    'of the ATK OLE DB Provider and the name of the database
    
          Call adoConnection.Open( _
    
                "provider=ObjectStore Active Toolkit OLE DB Provider;" & _
    
                "data source=C:\odi\ATK6.0\Examples\demodbs\extrademo.db","","")
    
          If Err.Number<>0 Then  'Something went wrong
    
                Response.Write("Error: " & Err.Description)
    
          Else  'Connection has been correctly opened
    
    'Create an ADO RecordSet
    
          Set adoRS = Server.CreateObject("ADODB.RecordSet")
    
    'And open data view "tut3_table1" using the previously opened ADO connection
    
          Call adoRS.Open("allimages", adoConnection, 1)  '1 is adOpenKeySet
    
                If Err.Number<>0 Then  'Something went wrong
    
                      Response.Write("Error: " & Err.Description)
    
                Else 'RecordSet has been correctly opened
    
    'Generate the HTML output for the opened RecordSet
    
                      WriteObjectManagerTable(adoRS)
    
                End If
    
          End If
    
    %>
    
    The WriteObjectManagerTable procedure scans the RecordSet and builds the HTML table that contains the images:

    <SCRIPT LANGUAGE=VBScript RUNAT=Server> 
    
    Sub WriteObjectManagerTable(anADORS)
    
          On Error Resume Next
    
    'Remove any previous navigation information
    
          Session("ObjectManagers")=Empty
    
    'Create an array that will store the array of bytes
    
    'representing the images
    
          Dim  ObjectManagers()
    
          ReDim  ObjectManagers(anADORS.RecordCount)
    
    'Initialize the HTML table
    
          Response.Write("<TABLE BORDER=1>")
    
    'Generate the column heading
    
          Response.Write("<TH><B> --- Images --- </B></TH>")
    
    'Scan all the records in the ADO RecordSet
    
    'Keep track of the row number
    
          rowNumber=0
    
          anADORS.MoveFirst
    
          Do While Not anADORS.EOF
    
          Response.Write("<TR>")
    
    'Retrieve the array of bytes that is the content of the image
    
    'Object Manager; store it in the array in the proper position
    
          ObjectManagers(rowNumber)=anADORS(0).GetChunk(anADORS(0).actualsize)
    
    'Create the link to the second page that actually displays the image
    
          Response.Write("<TD><CENTER><IMG SRC=""tut3_om2.asp?omPosition=" & _
    
                rowNumber & """></CENTER></TD></TR>")
    
    'Move to the next row in the ADO RecordSet
    
          anADORS.MoveNext
    
          rowNumber=rowNumber+1
    
          Loop
    
          Response.Write("</TABLE>")
    
    'Store the array of images in the Session
    
          Session("ObjectManagers")=ObjectManagers
    
    End Sub 
    
    </SCRIPT>
    
  • Save this code in the InetPub\ASPSamp\ATK\Tutorial3 directory as tut3_om1.asp.

    2 Create another ASP page to display the data.

  • Create another ASP page to write the image data to the table.
  • Remove the default lines created by Visual InterDev. The output of the Active Server Page must match the type and the actual data that the multimedia Object Manager returns.
    <%@ LANGUAGE="VBSCRIPT" %>
    
    <%
    
    'Set the content type of this block; we know they are jpeg images
    
          Response.ContentType="image/jpeg"
    
    'Retrieve the index in the array where the image is
    
          omIndex=Request.QueryString("omPosition")
    
    'Retrieve the array of images
    
          ObjectManagers=Session("ObjectManagers")
    
    'Write the image bits
    
          Response.BinaryWrite(ObjectManagers(omIndex))
    
    %>
    
  • Save this code in the InetPub\ASPSamp\ATK\Tutorial3 directory as tut3_om2.asp.

    3 Test the application.

    To test the application, browse the tut3_om1.asp page. Open http://localhost/ASPSamp/ATK/Tutorial3/tut3_om1.asp

    :

    Summary

    You also wrote ASP code that can navigate nested RecordSet objects in ADO.

    Finally, you wrote a sample ASP/ADO application that accesses multimedia data stored in an ObjectStore database and displays it as multimedia Object Manager instances.



    [previous] [next]

    Copyright © 1998 Object Design, Inc. All rights reserved.

    Updated: 05/11/99 11:59:44