Overview
AdvantageBuilder macros can access databases using three main approaches:
- .NET
System.Dataclasses (all scripting engines) - ADODB COM objects (
ADODB.Connection,ADODB.Recordset) in PowerShell and JScript - AdvantageBuilder Database API (preferred for most macros)
The AdvantageBuilder Database API provides four core operations:
ExecuteNonQuery(cmd),ExecuteNonQuery(cmd, connType, connString)— returns number of affected records (INSERT, UPDATE, DELETE, EXEC).ExecuteDataset(cmd),ExecuteDataset(cmd, connType, connString)— returns a .NETDataSet(SELECT, stored procedures with result sets).ExecuteDatasetXML(cmd),ExecuteDatasetXML(cmd, connType, connString)— returns XML representing aDataSet, used mainly in GUI macros.ExecuteBatchTran(commands),ExecuteBatchTran(commands, connType, connString)— executes multiple statements in a single transaction, returns number of affected records (INSERT, UPDATE, DELETE, EXEC).
The single-parameter versions use the current connection - the current connection as selected in the Data tab (e.g. SQL Server tab or Generic Builder tab). The three-parameter versions additonally accept
connType and connString, allowing you to connect to SQL Server, Oracle, OleDb, ODBC, MySQL, PostgreSQL, and more.
Current Connection Examples
These examples use the current SQL Server connection configured in AdvantageBuilder (no explicit connection string). They call the AdvantageBuilder Database API directly from macros.
PowerShell - SELECT with ExecuteDataset
try {
$sql = @"
select Id, Name, CreatedDate
from dbo.Customers
order by Name
"@
$ds = ExecuteDataset $sql
if ($ds -ne $null -and $ds.Tables.Count -gt 0) {
$table = $ds.Tables[0]
foreach ($row in $table.Rows) {
$name = $row["Name"]
$created = $row["CreatedDate"]
# use $name, $created
}
}
$ds = $null
}
catch {
alert("Database error: " + $_.Exception.Message)
}
PowerShell - INSERT with ExecuteNonQuery
try {
$sql = @"
insert into dbo.Customers (Name, CreatedDate)
values ('New Customer', getdate())
"@
$recordsAffected = ExecuteNonQuery $sql
alert("Records affected: " + $recordsAffected)
}
catch {
alert("Database error: " + $_.Exception.Message)
}
JavaScriptV8 - SELECT with ExecuteDataset
try {
let sql = `
select Id, Name, CreatedDate
from dbo.Customers
order by Name
`;
let ds = ExecuteDataset(sql);
if (ds != null && ds.Tables.Count > 0) {
let table = ds.Tables.Item(0);
for (let row of table.Rows) {
let name = row.Item("Name");
let created = row.Item("CreatedDate");
// use name, created
}
}
ds = null;
}
catch (ex) {
alert("Database error: " + ex.Message);
}
JavaScriptV8 - UPDATE with ExecuteNonQuery
try {
let sql = `
update dbo.Customers
set Name = 'Updated Name'
where Id = 123
`;
let recordsAffected = ExecuteNonQuery(sql);
alert("Records affected: " + recordsAffected);
}
catch (ex) {
alert("Database error: " + ex.Message);
}
JScript - SELECT with ExecuteDataset
try {
var sql = "select Id, Name, CreatedDate from dbo.Customers order by Name ";
var ds = ExecuteDataset(sql);
if (ds != null && ds.Tables.Count() > 0) {
var table = ds.Tables[0];
for (var row in table.Rows) {
var name = table.Rows[row]["Name"];
var created = table.Rows[row]["CreatedDate"];
// use name, created
}
}
ds = null;
}
catch(e) {
alert("Database error: " + e.message);
}
JScript - DELETE with ExecuteNonQuery
try {
var sql = "delete from dbo.Customers where Id = 123 ";
var recordsAffected = ExecuteNonQuery(sql);
alert("Records affected: " + recordsAffected);
}
catch(e) {
alert("Database error: " + e.message);
}
Specified Connection Examples (Any Database)
Use the ExecuteNonQuery, ExecuteDataset, ExecuteDatasetXML, and ExecuteBatchTran functions to connect to
any supported database type by specifying connType and connString.
Common connection types:
0— SQLClient1— OracleClient2— OleDb3— ODBC4— MySQL5— PostgreSQL
PowerShell - MySQL SELECT with ExecuteDataset2
try {
$connectionString = @"
Server=localhost;Database=MyDb;User Id=myuser;Password=mypassword;
"@
$connectionType = 4 # MySQL
$sql = @"
select Id, Name
from Customers
order by Name
"@
$ds = ExecuteDataset $sql $connectionType $connectionString
if ($ds -ne $null -and $ds.Tables.Count -gt 0) {
$table = $ds.Tables[0]
foreach ($row in $table.Rows) {
$name = $row["Name"]
# use $name
}
}
$ds = $null
}
catch {
alert("Database error: " + $_.Exception.Message)
}
JavaScriptV8 - PostgreSQL INSERT with ExecuteNonQuery2
try {
let connectionString = `
Host=localhost;Database=MyDb;Username=myuser;Password=mypassword;
`;
let connectionType = 5; // PostgreSQL
let sql = `
insert into Customers (Name)
values ('New Customer')
`;
let recordsAffected = ExecuteNonQuery(sql, connectionType, connectionString);
alert("Records affected: " + recordsAffected);
}
catch (ex) {
alert("Database error: " + ex.Message);
}
JScript - Oracle SELECT with ExecuteDataset2
try {
var connectionString = "Data Source=MyOracle;User Id=myuser;Password=mypassword;";
var connectionType = 1; // OracleClient
var sql = "select Id, Name from Customers order by Name ";
var ds = ExecuteDataset(sql, connectionType, connectionString);
if (ds != null && ds.Tables.Count() > 0) {
var table = ds.Tables[0];
for (var row in table.Rows) {
var name = table.Rows[row]["Name"];
// use name
}
}
ds = null;
}
catch(e) {
alert("Database error: " + e.message);
}
Using Dataset XML in GUI Macros
GUI macros often run inside an IE browser context, which cannot automate .NET DataSet objects directly.
In these cases, use ExecuteDatasetXML or ExecuteDatasetXML2 to return XML representing the DataSet,
then pass that XML into a JavaScript DataSet object that mimics the .NET DataSet model.
JScript - Using ExecuteDatasetXML with a JavaScript DataSet
var sql = "select * from tblMarketType ";
var dsXML = ExecuteDatasetXML(sql);
// DataSet is a JavaScript class that parses the XML and exposes
// Tables, Columns, Rows, and ItemArray similar to .NET DataSet.
var ds = new DataSet(dsXML);
if (ds != null && ds.Tables.Count() > 0) {
var table = ds.Tables[0];
for (var row in table.Rows) {
// Access by index
writeln(table.Rows[row][0]);
// Or by column name
// var value = table.Rows[row]["ColumnName"];
}
}
ds = null;
The JavaScript DataSet implementation handles:
- Parsing XML using MSXML (
MSXML2.DOMDocument) - Building
DataTable,DataColumn, andDataRowobjects - Exposing
Tables,Rows,Columns, andItemArray - Detecting date strings and converting them to JavaScript
Dateobjects
This pattern is ideal for Macro GUI scripts that need to display or process database data inside the browser.
Using the DB Helper (Recommended)
AdvantageBuilder includes a DB Helper macro that generates database access code snippets for you. It is available in the Smart, Extensible Macro Editor under:
- AdvantageBuilder API → Database Access → @M-DB Helper
DB Helper lets you:
- Select scripting engine: PowerShell, JavaScriptV8, JScript
- Select method:
ExecuteNonQuery,ExecuteDataset,ExecuteDatasetXML,ExecuteBatchTran - Check/un-check the "Use Current Connection" checkbox, if you wich to use the Current connection, or specify you own connection string/type
- Select connection type: SQLClient, OracleClient, OleDb, ODBC, MySQL, PostgreSQL
- Enter connection string and SQL text
- Generate ready-to-use code snippets with DataSet loops and variable placeholders
Example - PowerShell snippet generated by DB Helper
$connectionString = @"
Server=localhost;Database=MyDb;User Id=myuser;Password=mypassword;
"@
$connectionType = 4
$sql = @"
select Id, Name
from Customers
order by Name
"@
$ds = ExecuteDataset $sql $connectionType $connectionString
if ($ds -ne $null -and $ds.Tables.Count -gt 0)
{
$table = $ds.Tables[0]
foreach ($row in $table.Rows) {
$varName1 = $row["ColumnName"]
$varName2 = $row[columnIndex]
}
}
$ds = $null
Example - JavaScriptV8 snippet generated by DB Helper
let connectionString = `
Server=localhost;Database=MyDb;User Id=myuser;Password=mypassword;
`;
let connectionType = 4;
let sql = `
select Id, Name
from Customers
order by Name
`;
let ds = ExecuteDataset(sql, connectionType, connectionString);
if (ds != null && ds.Tables.Count > 0)
{
let table = ds.Tables.Item(0);
for (row of table.Rows)
{
let varName1 = row.Item("ColumnName");
let varName2 = row.Item(columnIndex);
}
}
ds = null;
Example - JScript snippet generated by DB Helper
var connectionString = "Server=localhost;Database=MyDb;User Id=myuser;Password=mypassword;";
var connectionType = 4;
var sql = "select Id, Name from Customers order by Name ";
var ds = ExecuteDataset(sql, connectionType, connectionString);
if (ds != null && ds.Tables.Count() > 0)
{
var table = ds.Tables[0];
for (var row in table.Rows)
{
var varName1 = row.Item("columnName");
var varName2 = row.Item(columnIndex);
var varName3 = row["columnName"];
}
}
ds = null;
Using DB Helper is the fastest way to generate correct, engine-specific database access code that follows AdvantageBuilder best practices.
Putting It All Together
Use the AdvantageBuilder Database API to run SQL against SQL Server and other databases, returning either affected record counts or DataSets (or XML DataSets for GUI macros). PowerShell, JavaScriptV8, and JScript all support these functions, and the DB Helper macro can generate ready-to-use snippets for you.
For most macros:
- Use
ExecuteNonQueryfor INSERT, UPDATE, DELETE, and non-query stored procedures. - Use
ExecuteDatasetfor SELECT and result-set stored procedures. - Use
ExecuteDatasetXMLfor GUI macros that need XML DataSets. - Use
ExecuteBatchTranfor multiple commands in a single transaction, such as INSERT, UPDATE, DELETE, and non-query stored procedures. - Use SQL Server Builder for built-in SQL Server connections, and Generic Builder for other database types.