|
APGScript Object |
|
See Also: |
The APGScript.Values collection contains values associated with an APGScript object. It is of type Collection.
VBScript:
JScript:
| oAPGScript | An APGScript object. |
| oAPGScript.Values | A Collection object. |
There are several ways to access items in the APGScript.Values collection:
VBScript:
JScript:
| oAPGScript | An APGScript object, created using APGen.OpenScript(). |
| key | A unique string key to the collection. String keys are case insensitive, thus oAPGScript("val") and oAPGScript("Val") refer to the same object in the collection. |
| index | A zero-based index to the collection. |
| value | A variant value that is stored in the collection. |
See the Collection Object topic for the properties and methods exposed by the APGScript.Values collection.
This collection is useful for passing named values into an APGScript object, and then letting the script access the values using the intrinsic Script object. Similarly, this collection can be used to pass values out of an APG script: Script code modifies Script collection values, and after the script is finished executing, external code can extract the values from the APGScript object.
The collection returned by APGScript.Values is the same collection as Script.Values. The difference is, APGScript.Values allows access to Script.Values collection from outside of the script.
For additional tips on using this collection, see the Script.Values topic.
This example consists of two APG scripts. The first script, QueryToXml.apg, takes an ADO connection string and a query string, and creates an XML file from the results of the query. The connection string and query string are read from the Script.Values collection.
The second script, test.apg, runs QueryToXml.apg using programmatic execution. QueryToXml.apg is run twice, using two different connection strings and query strings. The connection strings and query strings are passed in using the APGScript.Values collection.
This example requires SQL Server and Microsoft ActiveX Data Objects (ADO). To run the second example in test.apg, ADO 2.0 or later must be installed, because the example uses the MSDataShape OLE DB provider.
QueryToXml.apg:
<%# Option Explicit
#%><?xml version="1.0"?>
<query>
<%#
' Define ADO constants
Const adChapter = 136
' Open the database connection
Dim oCn, oRS
Set oCn = Script.CreateObject("ADODB.Connection")
' The connection string is stored in Script("CnStr")
oCn.Open Script("CnStr")
' Run the query
' The query string is stored in Script("SQL")
Set oRS = oCn.Execute(Script("SQL"))
' Sub that renders a row into XML
Sub RenderRow (nDepth, oRS)
Dim nFields, nCol
nFields = oRS.Fields.Count
for nCol = 0 to nFields - 1
Dim oField, value
Set oField = oRS.Fields(nCol)
if oField.Type <> adChapter then
' Write prefix spaces
Output.Write Space(nDepth)
' Write tag
value = oField.Value
if not IsNull(value) then
Output.Write "<" & oField.Name & ">" & Util.XMLEncode(value) & _
"</" & oField.Name & ">" & vbCRLF
end if
else
' Handle subrecordsets from MSDataShape
Dim oRSSub
Set oRSSub = oField.Value
Do While Not oRSSub.EOF
Output.Write Space(nDepth) & "<" & oField.Name & _
">" & vbCRLF
' Recursively call RenderRow
RenderRow nDepth+1, oRSSub
Output.Write Space(nDepth) & "</" & oField.Name & _
">" & vbCRLF
oRSSub.MoveNext
Loop
end if
next
End Sub
' Render all the rows
Do While Not oRS.EOF
#%> <row>
<%#
RenderRow 3, oRS
#%> </row>
<%#
oRS.MoveNext
Loop
#%> </query>
<%#
Output.Close
#%>
test.apg:
<%# @Language=JScript #%>
<%#
// Open the QueryToXML script
var oScriptQueryToXml = APGen.OpenScript("QueryToXML.apg");
// Set Connection String
oScriptQueryToXml("CnStr")= "Provider=SQLOLEDB;Initial Catalog=pubs;" +
"UserID=sa;Password=''";
// Set query string
oScriptQueryToXml("SQL") = "SELECT * FROM authors";
// Set filename
oScriptQueryToXml.Output.Filename = "authors.xml";
// Run QueryToXML
oScriptQueryToXml.Run();
//
// Try it again with a hierarchical query
//
oScriptQueryToXml("CnStr") = "Provider=MSDataShape;Data Provider=SQLOLEDB;" +
"Initial Catalog= pubs;UserID=sa;Password=''";
oScriptQueryToXml("SQL") = "SHAPE ( " +
" SHAPE { " +
" SELECT *, au_id AS [auid] FROM authors" +
" } APPEND ( {" +
" SELECT titles.*, titleauthor.au_id " +
" AS [auid] FROM titles " +
" INNER JOIN titleauthor ON " +
" titles.title_id = titleauthor.title_id" +
" } RELATE [auid] TO [auid] " +
" ) AS title" +
") AS rsAuthorTitles";
oScriptQueryToXml.Output.Filename = "author_titles.xml";
oScriptQueryToXml.Run();
#%>