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. Software requirements
To complete the exercises in this chapter, you need these software resources:
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:
 
 
 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: 1	 Create a new data view to use in the application.
 
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.
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.
 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: 
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.
SELECT itemNumber as itemID, description, orders#orderNumber AS `related order ID' FROM my_ tut3_table1 ORDER BY descriptionThis is the result of the query:
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.
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. 
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.
<%
      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.
4	 Test the application. 
 
 
 
 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. Process
To display multimedia Object Managers using ADO, check the data view that contains the multimedia images:
The data view contains a collection of instances of the Image Object Manager, osmmVirageImage.
 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: 1	 Create a new ASP page to retrieve the data.
<%
      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>
2	 Create another ASP page to display the data. 
<%@ 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))
%>
3	 Test the application. 
To test the application, browse the tut3_om1.asp page. Open http://localhost/ASPSamp/ATK/Tutorial3/tut3_om1.asp
:
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.
Updated: 05/11/99 11:59:44