Connecting MSSQL DataBase with X++ Code.

As part of Customer requirements , sometimes we need to connect to DataBase otherthan Axapta DataBase. So let us explore how it can be done with X++ code.
In Ax using ODBCConnection and LoginProp its possible to connect to Database on remote server, as illustrated in the following example.I tried the following code on MSSQL Server.

static void test_ODBCConnection(Args _args)
{

LoginProperty loginProp;
ODBCConnection conn;
Resultset resultSet, resultSetCount; // get record
Statement statement1, statement2; // Create SQL Statement
ResultSetMetaData metaData ; // get Record metadate like columnname.
;

// Set Server Database
loginProp = new LoginProperty();
loginProp.setServer('SON15092');
loginProp.setDatabase('AdventureWorksDW');

// Create Connection and SQL Statement
conn = new ODBCConnection(loginProp);
statement1 = conn.createStatement();
resultSet = statement1.executeQuery("SELECT * from DimTime");

while (resultSet.next())
{
metaData = resultSet.getMetaData();
info("Column Name :"+metaData.getColumnName(1)+" Value ="+resultSet.getString(1));
}
}

Enjoy, X++!!!!!!

3 comments:

  1. Hi Nagaraj,

    The above post is really helpful.

    I have a task in which i need to connect my Database as above and create a new table i have the table scriptbut how to do that....

    do u have any idea?

    ReplyDelete
  2. Hi Sangeeta,

    If its Axapta DataBase then you should need to create table only throught AOT->Table node.
    if you are creating the table through SQL Queries like above job, then that table will not be accessible in AOT and also during syncronization this table will be deleted from dataBase.

    So please let me know if you have different scenario than above.

    Thanks

    ReplyDelete
  3. Hi Nagaraj,

    i tried that code for execution but it shows 2 errors.one is ODBC operation faild and 2nd is ODBC couldnot be connected.

    ReplyDelete

Note: Only a member of this blog may post a comment.