The Content object is included in Commerce Server 3.0 only for backward compatibility with sites created in Commerce Server 2.0. New sites should be constructed using ADO. Connection strings (not DSNs) should be stored in the ConnectionStringMap (part of the site dictionary) and queries should be stored in the QueryMap Dictionary object.
The Content object consists of a collection of variables that identify the DSNs and queries that a Commerce Server 2.0 site uses to read subsets of site inventory into site pages. It implements methods that make it possible to add query variables to the object, and that support the creation of the Datasource object, which you use to execute queries associated with Content cache elements.
In Commerce Server 3.0 and later, mapping of names to data sources is accomplished by the Site object. Connection to data sources is accomplished with ADO’s Command and Connection objects.
Because the pages in your site need to access a central collection of queries and DSNs, you should create the Content object to have Application scope. This means that this object must be created in the Application_OnStart event, which you define in your \Shop directory’s Global.asa file, and you must initialize the Application object’s MSCSContent variable to reference this object.
Additionally, if your site uses more than one DSN, identify one of the Datasource objects that you create as the site’s default data source. To do this, initialize the Application object’s MSCSDefaultDatasource variable to reference the string variable name of the data source. The following example, which contains a subset of the statements that appear in the Clocktower’s Global.asa file, illustrates this initialization:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
REM -- Create a content object and datasource for connection to the database
Set MSCSContent = Server.CreateObject("Commerce.Content")
Call MSCSContent.AddDatasource("Clocktower", MSCSDSN)
REM -- Set up the Application instrinsic object
Application.Lock
Set Application("MSCSContent") = MSCSContent
Application("MSCSDefaultDatasource") = "Clocktower"
Application.Unlock
End Sub
</SCRIPT>
The Datasource object, unlike the Content object, is created on a per-page basis. For example, in the Clocktower site, the Shop.asp file is included in every file that needs to retrieve a data source from the Content object, and includes the following example:
Set MSCSDatasource = Application("MSCSContent")("Clocktower")
When you need to connect to a database using one of the DSN variables you have added, or need to execute one of the added queries, you use the Content object to create a Datasource object based on the name of a given DSN. A Datasource object has access to all the queries stored in the Content object through which it was created. Additionally, the Datasource object can return query results to the caller either as a SimpleList object containing Dictionary objects, or as an ADO Recordset.
Use of the Content object begins with the creation of a DSN variable, a string variable that you associate with a DSN that you have created using the Microsoft® Open Database Connectivity (ODBC)32 Control Panel utility. The Content object’s AddDatasource method adds a DSN variable to the Content object. Because the Content object will contain all the DSNs and queries used by the site to retrieve site content, this object should have Application scope. This means that it should be created in the Application_OnStart subroutine in the site’s Global.asa file:
REM -- Create a content object and datasource for connection to the database
Set MSCSContent = Server.CreateObject("Commerce.Content")
Call MSCSContent.AddDatasource("Clocktower", MSCSDSN)
This example call to AddDatasource assigns the string name Clocktower
to the data source identified by MSCSDSN
. MSCSDSN
is a string variable that contains the database connection string necessary to access the database, and is defined in The Clocktower’s Dsn_include.asp file as follows:
MSCSDSN = "DSN=Stores;UID=sa;PWD=;APP=;DATABASE=Commerce"
In this string, Stores
identifies the DSN that you assigned to this data source when you configured it using the ODBC32 Control Panel utility, and Commerce
identifies a Microsoft® SQL Server™ database that you specified when you created the DSN. The preceding string is a sample database connection string, and that the actual string varies from one data source to another.
After adding a DSN variable, Clocktower
in this example, to the Content object, you use the AddQuery method to add your site’s queries to the object:
REM -- Add queries to the content object for use in our pages
Call MSCSContent.AddQuery("departments", "select * from Clocktower_dept")
Call MSCSContent.AddQuery("products-by-dept", "select sku, name,
dept_id, manufacturer, list_price, image_file, image_width,
image_height, description from Clocktower_product where dept_id =
convert(numeric,:1)")
Call MSCSContent.AddQuery("department", "select * from Clocktower_dept
where dept_id = convert(numeric,:1)")
Call MSCSContent.AddQuery("product", "select sku, name, dept_id,
manufacturer, list_price, image_file, image_width, image_height,
description from Clocktower_product where sku = :1")
The first parameter to AddQuery identifies the name by which you will reference this query when you execute it, and the second parameter contains the query’s SQL text. The values that follow the text of the query consist of parameters that you can use to enhance the speed and efficiency with which queries are executed.
For information about these parameters, see Content.AddQuery Method.
To execute one of the queries added, you need to use the Content object’s Datasource method to create a Datasource object, and then call its Execute method, referencing the string name that you have associated with the query. Here is an example of such a call:
Set MSCSDataSource = MSCSContent.Datasource("Clocktower")
After you have created a Datasource object, it has access to all the queries in the Content object cache. Consequently, you use the Datasource object’s Execute method to execute a query stored in the Content cache without directly referencing the Content object through which the Datasource object was originally created. For example, the following call, which appears in the Clocktower’s Default.asp file, calls the Execute method on the departments
query that was added to the Content object earlier in this section, and iterates through the SimpleList object containing the query’s results to display the Clocktower’s departments on the page:
<UL>
<% set depts = MSCSDataSource.Execute("departments") %>
<% for each dept in depts %>
<LI><A HREF="<% = mscsPage.URL("dept.asp", "dept_id",
dept.dept_id) %>"><% = mscsPage.Encode(dept.dept_name) %></A>
<% next %>
</UL>