Import from Database

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Programmers' Reference > Application API > Overview > Programming Languages > JScript > Import and Export of Data >

Import from Database

Given below are the steps to establish a connection to an existing database for import:

 

1.Use a DatabaseConnection object and set the properties:

The method Application.GetDatabaseSettings returns a new object for a database connection:

 

 objImpSettings = objSpy.GetDatabaseSettings();

 

You have to set either an ADO connection string,

objImpSettings.ADOConnection = strADOConnection

 

or the path to an existing database file:

objImpSettings.File = strExampleFolder + "Tutorial\\Company.mdb";

 

To complete the settings you create a SQL select statement to define the data to be queried:

objImpSettings.SQLSelect = "SELECT * FROM Address";

 

2.Call Application.GetDatabaseImportElementList to get a collection of the resulting columns of the SQL query:

 

                                         objElementList = objSpy.GetDatabaseImportElementList(objImpSettings);

 

This collection gives you the opportunity to control which columns should be imported and specify the datatype of the new elements. Each item of the collection represents one column to import. If you remove an item, the corresponding column will not be imported. You can additionally modify the ElementListItem.ElementKind property to set the datatype of the XML elements for each column.

 

Please consider that GetDatabaseImportElementList() executes the SQL query and could initiate a time-consuming call. To avoid this, it is possible to pass a null-pointer as the second parameter to ImportFromDatabase(); this imports all columns as plain XML elements.

 

3.Start the import with Application.ImportFromDatabase:

 

                                         objImpDocFromDB = objSpy.ImportFromDatabase(objImpSettings,objElementList);

 

// Locate examples via USERPROFILE shell variable.
objWshShell = WScript.CreateObject("WScript.Shell");
majorVersionYear = objSpy.MajorVersion + 1998
strExampleFolder = objWshShell.ExpandEnvironmentStrings("%USERPROFILE%") + "\\My Documents\\Altova\\XMLSpy" + majorVersionYear + "\\Examples\\";
 
try
{
  // specify the source of data import
  objImpSettings = objSpy.GetDatabaseSettings();
  objImpSettings.File = strExampleFolder + "Tutorial\\Company.mdb";
  objImpSettings.SQLSelect = "SELECT * FROM Address";
 
  // column filter
  objElementList = objSpy.GetDatabaseImportElementList(objImpSettings);
 
  // import into a new XML file
  objImpDocFromDB = objSpy.ImportFromDatabase(objImpSettings,objElementList);
}
catch(err)
{
  WScript.Echo("Error importing from database.\n\n" +
                    "Error: " + (err.number & 0xffff) + "\n" +
                    "Description: " + err.description);
}

 

The JScript code listed above is available in the sample file ImportExport.js (see Example Files).


© 2019 Altova GmbH