How to use .NET Business Connector
Business Connecter .NET(or BC .NET) is an API provided to access AX out of .NET code. This is one of the ways to use AX data from outside application. Others may be like using AIF. We will show you steps needed to access AX from .NET application using BC .NET. This is going to be a very simple BC .NET example.
We will start with creating a C# console project in Visual Studio.
Add reference to Business connector assembly. Path to assembly will probably be C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin. Find assembly Microsoft.Dynamics.BusinessConnecter.net.dll
The most commonly used class in BC .NET is Microsoft.Dynamics.BusinessConnectorNet.Axapta. We will have to create its object to logon to AX. This class provides access to AOS.
To read a buffer, we need another class, Microsoft.Dynamics.BusinessConnectorNet.AxaptaRecord. Its object is created using a factory method from Axapta class, Axapta.CreateAxaptaRecord(tableName).
Microsoft.Dynamics.BusinessConnectorNet.Axapta ax = new Microsoft.Dynamics.BusinessConnectorNet.Axapta();
//Default company, default language, default AOS and default configuration
ax.Logon(null, null, null, null);
Microsoft.Dynamics.BusinessConnectorNet.AxaptaRecord axRecord = ax.CreateAxaptaRecord("CustTable");
We are keeping it simple as a first BC .NET application. We will just instantiate a record buffer and select records into it. AxaptaRecord.ExecuteStmt() will be called along with the X++ query with a difference that we will not use table buffer name, instead, we will use “%1”. Then we will loop through all the records returned to fetch and print two fields, ‘AccountNum’ and ‘CashDisc’. Apart from these two fields, we also want to fetch customer’s name but there is no field in CustTable for customer name. Fortunately we have a method, name() in CustTable that gets customer’s name and also that AxaptaClass allows calling table methods and retrieving returned values. See below, the implementation of the method call:
axRecord.ExecuteStmt("select firstOnly10 %1");
while (axRecord.Found)
{
Console.WriteLine("{0}|{1}|{2}", axRecord.get_Field("AccountNum"), axRecord.get_Field("CashDisc"), axRecord.Call("name"));
axRecord.Next();
}
The output will be: