web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Get data from third party database and insert into AX.

(1) ShareShare
ReportReport
Posted on by 30

For several reasons we need to get the data from SQL db and insert it into AX table.

below the code snippet to do read data from db and insert into LedgerJournalTrans table:

public static server void GetDataandInsertToLedger()
{

str serverName;
str catalogName;
str ConnectionString;
str sqlQuery;

Table2 t;
real p;


System.Data.SqlClient.SqlConnectionStringBuilder connectionStringBuilder;
System.Data.SqlClient.SqlConnection connection;
System.Data.SqlClient.SqlCommand command;
System.Data.SqlClient.SqlParameterCollection parameterCollection;
System.Data.SqlClient.SqlDataReader dataReader;
;
new InteropPermission( InteropKind::ClrInterop ).assert();


sqlQuery = "SELECT TOP 10 T.Txt , T.Amount FROM TestTable T";


serverName = SysSQLSystemInfo::construct().getLoginServer();
catalogName = SysSQLSystemInfo::construct().getloginDatabase();
connectionStringBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
connectionStringBuilder.set_DataSource(serverName);

connectionStringBuilder.set_IntegratedSecurity(true);
connectionStringBuilder.set_InitialCatalog(catalogName);

ConnectionString = "Data Source=Essam-PC;Initial Catalog=Test;Integrated Security=True";

connection = new System.Data.SqlClient.SqlConnection(ConnectionString);
command = new System.Data.SqlClient.SqlCommand(sqlQuery);
command.set_Connection(connection);

parameterCollection = command.get_Parameters();

try
{
connection.Open();
try
{
dataReader = command.ExecuteReader();

while(dataReader.Read())
{
t.Txt= dataReader.get_Item("Txt");

p=dataReader.get_Item("Amount");
t.Amount=p;

t.insert();

EssamIntegration::InsertLedgerJournalTrans(dataReader.get_Item("Txt"),p,0);

}

dataReader.Dispose();
}
catch 
{
dataReader.Dispose();
}
catch(Exception::CLRError) 
{
dataReader.Dispose();
}
connection.Dispose();
}
catch
{
connection.Dispose(); 
}
catch(Exception::CLRError)
{
connection.Dispose();
}
command.Dispose();
CodeAccessPermission::revertAssert();
}

and the below job to insert into AX table

public static server void InsertLedgerJournalTrans(LedgerJournalTransTxt Txt,
AmountCurDebit AmountDebit,
AmountCurCredit AmountCredit
)
{
LedgerJournalTrans journalTrans;

;
journalTrans.clear();
journalTrans.initValue();
journalTrans.Txt = StrFmt(Txt);
journalTrans.Voucher = "VOU-000008815";
journalTrans.JournalNum = "JOU_001473";
journalTrans.LineNum = LedgerJournalTrans::lastLineNum(journalTrans.JournalNum)+1;
journalTrans.transDate = systemDateGet();
journalTrans.AccountType = LedgerJournalACType::Ledger;

journalTrans.AmountCurDebit = AmountDebit;
journaltrans.AmountCurCredit=AmountCredit;
journalTrans.OffsetAccountType = LedgerJournalACType::Ledger;
journalTrans.OffsetLedgerDimension = 0002; //Customized field
journalTrans.LedgerDimension = 5637172467;
journalTrans.CurrencyCode = "USD";


journalTrans.insert();

// info("done");
}


*This post is locked for comments

I have the same question (0)
  • Rene Volkmer Profile Picture
    326 on at
    RE: Get data from third party database and insert into AX.

    Hi Brandon, 

    can you please describe solution you propose in closer detail?

    BR

    RV

  • Brandon Wiese Profile Picture
    17,788 on at
    RE: Get data from third party database and insert into AX.

    There are other approaches to this problem, depending on the remote data source and how many records you need to copy.  When I have to bring in a million records into AX, a code loop that works field by field and record by record is pretty much out of the question.  You can use temporary tables to fetch the data by building an SQL statement directly, and then an insert_recordset into your permanent table, and get very good performance with set based operations.  This even works on linked server, which can get you to MySQL, DB2, Excel, really any data source with an OLEDB or ODBC driver.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Syed Haris Shah Profile Picture

Syed Haris Shah 9

#2
Mea_ Profile Picture

Mea_ 4

#3
Martin Dráb Profile Picture

Martin Dráb 2 Most Valuable Professional

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans