connection AX to the sql database ,DirectSql

Example : Connection AX 2009 to the sql database ,DirectSql

static void DirectSql2(Args _args)
{
    CCADOConnection     ccConnection;
    CCADOCommand        ccCommand;
    CCADORecordSet      ccRecordset;
    CCADOFields         ccFields;
    Com               Com;
    Str                 servername  = '10.0.1.10';
    Str                 database    = 'DataBaseName';
    Str                 user        = 'UserName';
    Str                 PWD         = 'Password';
    Str                 Cmd         = '';
    int                 cRow        = 0;
    str                 _id,BranchId,Cust;
    ;

    ttsbegin;
    ccConnection = new CCADOConnection();
    ccConnection.open(StrFmt("Provider=SqlOledb;server=%1;trusted_Connection=false;Initial  catalog=%2;user id=%3;password=%4;"
            , servername  // Server's IP or name
            , database    // Database
            , user        // Username
            , PWD         // Password
            ));

    try
    {
        Cmd    = 'SELECT BranchId ,TaxNumber,SaleDate,CutomerId,SaleType';
        Cmd   += ',ProductId,Quantity,UnitPrice,Amount,VatAmount';
        Cmd   += ',VatRate,CouponId,ShiftNo,VehicleID,UpdateDate';
        Cmd   += ',UpdateBy,TaxGroupNo,TaxGroupAmt,TaxGroupVat,ID';
        Cmd   += ',AxState,AxTransDate,AxUser,DisRate';
        Cmd   += ' FROM TableName';
        Cmd   += " where id = '3511978' ";

        if(cmd)
        {
            ccCommand   = new CCADOCommand();
            ccCommand.activeConnection(ccConnection);
            ccCommand.commandText(cmd);
            ccRecordset = new CCADORecordSet();
            ccRecordset = ccCommand.execute();
            Com         = ccRecordset.recordSet();
        }

        while (!ccRecordset.EOF())
        {

             ccFields     = ccRecordset.fields();
             _id          = ccFields.itemIdx(19).value();
             BranchId     = ccFields.itemIdx(1).value();
             Cust         = ccFields.itemIdx(4).value();
           
            info(strfmt('%1,%2,%3',_id,BranchId,Cust));
            Com.moveNext();
            cRow++;

        }
         info(strfmt('%1',cRow));

         ccRecordset.close();
         ccConnection.close();

    }
    catch (Exception::Error)
    {
        throw error('Fail');
    }

    ttscommit;

}

ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

เริ่ม ต้นเขียนโปรแกรมกับ กับ X++

date Functions Axapta X++