Accessing SqlServer Database through X++

Get our Free Newletter

Don't you have the time to be online here at DUG every day? So how do you follow what is going on in the Dynamics industry and community?
If you subscribe to the DUG Newsletter then you can relax! We will make sure that you don't miss the big news!

Check out our
Newsletter Subscription Center
page a complete list of our different newsletters.

Solution (Not Verified) This post has 0 verified solutions | 5 Replies | 3 Followers

6 Posts
60 Points
Joined: 2011-6-13
Last Online:
2011-7-14 8:50
Location: Noida
Pran posted on 2011-6-13 12:09

How can i read data from Sql Server Axapta Database tables from X++ without ODBC connection?
I don't want to create the ODBC in all the clients , so I want to access the Sql Server database from X++
without any other dependencies.

Please can anyone help me on this ?

All Replies

Male
89 Posts
1,070 Points
Joined: 2010-12-23
Last Online:
2013-5-20 13:51
Location: Pune,India
Arun Garg replied on 2011-6-13 15:17

//N by Arun Garg for  Connecting SQL SERVER
    static void DatabaseConnection(Args _args)
    {
    
        #CCADO
       CCADOConnection     connection;
        CCADOCommand        command;
        CCADORecordset      recordset ;
        CCADOFields         fields;
        str                 Database;
        str                 connectionStr;
        str                 statement;
        str                 CurrentCompanyCode;
        sqlSystem           sq =  new sqlSystem();
        CustTable          custTable;
        ;
            

        CurrentCompanyCode=curext();
        connectionStr =" Driver={SQL Native Client};Trusted_Connection=no;SERVER=  {" +  sq.loginServer() + "};DATABASE={master};User Id='';Password=''; 
        statement = "select AccountNum,Name";
        statement+=" from CustomerTable where AxaptaProcessDateTime = 0";
         info(strfmt("%1",connectionStr));
          info(strfmt("%1",statement));
         connection = new CCADOConnection();
         connection.open(connectionStr,#adConnectUnspecified);
         command = new CCADOCommand();
         command.activeConnection(connection);
         command.commandText(statement);
         try
        {
    
            recordset = command.execute();
            while(!recordset.EOF())
             {
                fields      = recordset.fields();
                CustTable.AccountNum        = fields.itemIdx(0).value();
                //entry in PartyTable
               CustTable.Name              = fields.itemIdx(1).value();
    
                CustTable.insert();
    
                recordset.recordSet().movenext();
    
             }
    
              //Update Table
            statement = "update CustomerTable set AxaptaProcessDateTime = getdate() where AxaptaProcessDateTime = 0 ";
            command.commandText(statement);
           command.execute();
            info("Records Inserted");
            connection.close();
         }
        catch
        {
           connection.close();
           throw error ( "Error ");
    
        }
        
    }

Male
89 Posts
1,070 Points
Joined: 2010-12-23
Last Online:
2013-5-20 13:51
Location: Pune,India
Arun Garg replied on 2011-6-13 15:17

See How to connect SQL SERVER with Dynamics Ax 2009

Male
89 Posts
1,070 Points
Joined: 2010-12-23
Last Online:
2013-5-20 13:51
Location: Pune,India
Arun Garg replied on 2011-6-14 9:46

you got ur  answer

Male
2,084 Posts
33,927 Points
Joined: 2003-1-21
Last Online:
2013-5-24 19:22
Location: Herts, England
DynamicsAXMVP
Moderator
Solution (Not Verified) Harish Mohanbabu replied on 2011-6-14 17:21
Suggested by Alex

Hi,

You may use Connection class. By default this will establish a connection to the current database session. Code snippet from Developers guide below.  Also search in this forum as this subject was already discussed.

server static void main(Args args)
{
    Connection con = new Connection();
    Statement stmt = con.createStatement();
    ResultSet r;
    str sql;
    SqlStatementExecutePermission perm;
    ;
 
    sql = strfmt('SELECT VALUE FROM SQLSYSTEMVARIABLES');
 
    // Set code access permission to help protect the use of
    // Statement.executeUpdate.
    perm = new SqlStatementExecutePermission(sql);
    perm.assert();
 
    try
    {
        r = stmt.executeQuery(sql);
        while (r.next())
        {
            print r.getString(1);
            pause;
        }
    }
    catch (exception::Error)
    {
        print "An error occured in the query.";
        pause;
    }
    // Code access permission scope ends here.
    CodeAccessPermission::revertAssert();
}

Harish Mohanbabu
Long way to go before I sleep ..

View Harish Mohanbabu's profile on LinkedIn

2 Posts
5 Points
Joined: 2012-9-27
Last Online:
2012-10-18 16:29
Location: Aalter, Belgium
Philippe replied on 2012-9-27 10:07

Arun Garg:

//N by Arun Garg for  Connecting SQL SERVER
    static void DatabaseConnection(Args _args)
    {
    
        #CCADO
       CCADOConnection     connection;
        CCADOCommand        command;
        CCADORecordset      recordset ;
        CCADOFields         fields;
        str                 Database;
        str                 connectionStr;
        str                 statement;
        str                 CurrentCompanyCode;
        sqlSystem           sq =  new sqlSystem();
        CustTable          custTable;
        ;

        CurrentCompanyCode=curext();
        connectionStr =" Driver={SQL Native Client};Trusted_Connection=no;SERVER=  {" +  sq.loginServer() + "};DATABASE={master};User Id='';Password=''; 
        statement = "select AccountNum,Name";
        statement+=" from CustomerTable where AxaptaProcessDateTime = 0";
         info(strfmt("%1",connectionStr));
          info(strfmt("%1",statement));
         connection = new CCADOConnection();
         connection.open(connectionStr,#adConnectUnspecified);
         command = new CCADOCommand();
         command.activeConnection(connection);
         command.commandText(statement);
         try
        {
    
            recordset = command.execute();
            while(!recordset.EOF())
             {
                fields      = recordset.fields();
                CustTable.AccountNum        = fields.itemIdx(0).value();
                //entry in PartyTable
               CustTable.Name              = fields.itemIdx(1).value();
    
                CustTable.insert();
    
                recordset.recordSet().movenext();
    
             }
    
              //Update Table
            statement = "update CustomerTable set AxaptaProcessDateTime = getdate() where AxaptaProcessDateTime = 0 ";
            command.commandText(statement);
           command.execute();
            info("Records Inserted");
            connection.close();
         }
        catch
        {
           connection.close();
           throw error ( "Error ");
    
        }
        
    }

 

When I try this code I get the following error message "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified."

 

Can anyone help me with this ?

Page 1 of 1 (6 items) | Get this RSS feed | Bookmark and Share