Thursday, 17 November 2016

ODBC connection in AX 2012

ODBC is to access data of any database mentioned. We use X++ code to access.
1. Start -> admministrative tools ->Datasources(ODBC) -> Add ->Sql Server->Finish.
Next, Next,Finish.

2.Write following job to set ODBC connection.
static void Job19(Args _args)
{
    OdbcConnection oDBC;
    LoginProperty  login;
    Statement      stmt;
    ResultSet      rs;
    ResultSetMetaData rsmd;
    FCCAirports    fCCAirports;
    CustTable      CustTable;
   
    login = new LoginProperty();
    login.setDSN("AVinashDSN");  
    login.setDatabase("DynamicsAXDemo");
   
    oDBC = new OdbcConnection(login);
   
    try
    {
        stmt = oDBC.createStatement();
        rs   = stmt.executeQuery("select AccountNum,currency from custtable");
   
        while(rs.next())
        {
            info(rs.getString(1));
            info(rs.getString(2));
        }
    }
    catch
    {
        error('Unexpected error');
    }
}