SetSQLCmdEx

Execute a SQL query against a database.

Function

void SetSQLCmdEx(
    db: Database, 
    commandId: Integer, 
    sqlQuery: String, 
    callbackFunction: Object, 
    errorHandlerFunction: Object)

Parameters

Name Type Description
db Database The connection to the database object. Usually we could configure the database object in the project and use GetDatabaseX function to get the object
commandId Integer The command id, which will be passed to the callback function to execute the command which is associated with the result
sqlQuery String The SQL query string
callbackFunction Object The callback function object (can be a string or function name)
errorHandlerFunction Object Optional: The error handler function object (can be a string or function name)

Return Value

None

Callback Function

void callback(id: Integer, result: String)
Name Type Description
id Integer The command id, which is set in the corresponding call to SetSQLCmdEx
result String Contains the result of the query in CSV format, see Example

Error Handler Function

void errorHandler(id: Integer, error: String)
Name Type Description
id Integer The command id, which is set in the corresponding call to SetSQLCmdEx
error String Contains the error message text

Example

var db1 = GetDatabaseById(1);

SetSQLCmdEx(db1, 1, "SELECT Datanumber,Location * FROM [AF_BCS_C].[dbo].[DatabaseConnection]", OnQueryExecuted, ErrorHandler);

function OnQueryExecuted(id, result)
{
    var selectData=result.split("\n");

    for (var i=0; i<=selectData.length; i++)
    {
        var fetchdata = selectData[i].split(",");
        var datano = fetchdata[0];
        var location = fetchdata[1];
    }
}

function ErrorHandler(id, errorMessage)
{
    LogError(errorMessage);
}