Get table names .net business connector [AXAPTA]

2k Views Asked by At

I would like to know if there is a way to get all table names from AOT by using .NET business connector without calling X++ code in AX?

According to MSDN, we have to provide table name. [Link]

Axapta ax;
AxaptaRecord axRecord;
using (axRecord = ax.CreateAxaptaRecord(tableName));
{
    // Perform actions
}

Axapta Class has only "GetRecordCount" but nothing to get record names.

Kindly help

3

There are 3 best solutions below

4
On

You could make a list from AX then access that!

Or you could use the Dictionary class:

static void findTables(Args _args)
{
    Dictionary dictionary = new Dictionary();
    TableId tableId;
    TableName tableName;
    for (tableId = dictionary.tableNext(0); tableId; tableId = dictionary.tableNext(tableId))
    {
        tableName = dictionary.tableName(tableId);
        info(strfmt('%1 - %2', tableId, tableName));        
    }
}

This is X++ code but the translation to C# is trivial.

0
On
Axapta ax = new Axapta();
ax.LogonAs(name, domain, null,null,null,null,null);
AxaptaObject dictable = ax.CreateAxaptaObject("Dictionary");
int index = (int)dictable.Call("tableNext", 0);

while(index > 0) {
    AxaptaObject table = (AxaptaObject)dictable.Call("tableObject", index);
    string tableName = (string)table.Call("name");
    int next = (int)dictable.Call("tableNext", index);
    if (next <= index) 
        break;
    index = next;
}
0
On

Contrary to what the previous answer states, it is possible to return a list of table names with the .Net Business Connector.

This method uses the ExecuteStmt method on AxaptaRecord to execute X++ code dynamically.

From your client code

Axapta ax = new Axapta();
using (AxaptaRecord r = ax.CreateAxaptaRecord("SQLDICTIONARY")) 
{
    r.ExecuteStmt("SELECT NAME, TABID FROM %1 WHERE %1.NAME LIKE 'CUST*' && %1.FIELDID == 0");

    while (r.Found) 
    {
        Console.WriteLine((string)r.get_Field("NAME"));
    }
}

Ref:
http://msdn.microsoft.com/en-us/library/aa594213(v=ax.50).aspx http://www.stoneridgesoftware.com/using-like-in-x/
http://msdn.microsoft.com/EN-US/library/aa848113.aspx