c# – 使用Dynamics AX 2009业务连接器创建表和检索查询结果

前端之家收集整理的这篇文章主要介绍了c# – 使用Dynamics AX 2009业务连接器创建表和检索查询结果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个C#命令行工具来从AX获取数据并向AX添加数据(创建新表).

从AX表中获取数据很简单,并在此处记录:http://msdn.microsoft.com/en-us/library/cc197126.aspx

将数据添加到现有表格也很简单:http://msdn.microsoft.com/en-us/library/aa868997.aspx

但我无法弄清楚如何做两件事:

>创建一个新的AX表
>从AX查询中检索数据

有人可以分享一些示例代码或指出从哪里开始寻找.我在谷歌和MSDN上的搜索没有透露太多.

注意:我不是经验丰富的AX或ERP开发人员.

解决方法

这是一种从C#创建新AX表的方法(这是使用扩展方法):
public static bool CreateAXTable(this Axapta ax)
{
    string TableName = "MyCustomTable"; 
    string size = "255"; //You could load this from a setting
    bool val = false;
    if (!ax.TableExists(TableName))
    {
        AxaptaObject TablesNode = (AxaptaObject)ax.CallStaticClassMethod("TreeNode","findNode",@"\Data Dictionary\Tables");
        AxaptaObject node;
        AxaptaObject fields;
        AxaptaObject fieldNode;

        TablesNode.Call("AOTadd",TableName); 
        node = (AxaptaObject)ax.CallStaticClassMethod("TreeNode","\\Data dictionary\\Tables\\" + TableName);        
        fields = (AxaptaObject)ax.CallStaticClassMethod("TreeNode","\\Data dictionary\\Tables\\" + TableName + "\\Fields");

        fields.Call("addString","String1"); //add a string field
        fieldNode = (AxaptaObject)fields.Call("AOTfindChild","String1"); //grab a reference to the field
        fieldNode.Call("AOTsetProperty","StringSize",size);
        fieldNode.Call("AOTsave");

        fields.Call("addString","String2"); //add a string field
        fieldNode = (AxaptaObject)fields.Call("AOTfindChild","String2"); //grab a reference to the field
        fieldNode.Call("AOTsetProperty","String3"); //add a string field
        fieldNode = (AxaptaObject)fields.Call("AOTfindChild","String3"); //grab a reference to the field
        fieldNode.Call("AOTsetProperty","String4"); //add a string field
        fieldNode = (AxaptaObject)fields.Call("AOTfindChild","String4"); //grab a reference to the field
        fieldNode.Call("AOTsetProperty",size);
        fieldNode.Call("AOTsave");

        fields.Call("addReal","Real1");
        fields.Call("addReal","Real2");
        fields.Call("addReal","Real3");
        fields.Call("addReal","Real4");

        fields.Call("addDate","Date1");
        fields.Call("addDate","Date2");
        fields.Call("addDate","Date3");
        fields.Call("addDate","Date4");

        fields.Call("AOTsave");
        node.Call("AOTsave");

        AxaptaObject appl = ax.GetObject("appl");
        appl.Call("dbSynchronize",Convert.ToInt32(node.Call("applObjectId")),false);

        val = true;
    }
    else //Table already exists
    {
        val = true;
    }
    return val;
}


public static bool TableExists(this Axapta ax,string tableName)
{
    return ((int)ax.CallStaticClassMethod("Global","tableName2Id",tableName) > 0);
}

猜你在找的C#相关文章