ODBC connection for reading Data

Today I will demonstrate you that how to establish ODBC connection and use that for reading data in AX.

static void ReadOdbc(Args _args) 
{
OdbcConnection connection;
LoginProperty loginProp;
Statement statement;
ResultSet result;
str sqlStmt;
SqlStatementExecutePermission permission;
;
loginProp = new LoginProperty();

// Set the servername and database to the LoginProperty object
loginProp.setServer("SEA-DEV");
loginProp.setDatabase("MicrosoftDynamicsAX");
// Check to see if the executing user has access
try
{
// Create a new connection based on the information set in the LoginProperty object
connection = new OdbcConnection(loginProp);
}
catch
{
error ("You do not have access to the database specified");
return;
}
// Set the select statement to the string variable
sqlStmt = 'SELECT Dataareaid, AccountNum, Name from dbo.CustTable';

// Check if it's ok to use sql statement
permission = new SqlStatementExecutePermission(sqlStmt);
permission.assert();
// Create and prepare a new Statement object from the connection
statement = connection.createStatement();
// Execute the query and retrieve the resultset
result = statement.executeQuery(sqlStmt);
// Loop through the records in the resultset and print the information to the infolog
while (result.next())
{
info (strfmt("(%1) %2: %3", result.getString(1), result.getString(2), result.getString(3)));
}
// Revert the permission assert
CodeAccessPermission::revertAssert();
}

Comments