zoukankan      html  css  js  c++  java
  • [AX]AX2012 使用.net Business connector

    通过.Net Business connector可以在.net的工程中访问AX的数据及功能,要使用.Net Business connector首先需要安装它的组件,可以脱离client单独安装使用,在连接到AX系统时使用windows验证。在.net工程比如C#工程中添加对程序集Microsoft.Dynamics.BusinessConnectorNet.dll的引用,一般安装在目录C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin\下。C#工程的Target framework必须选择为.NET Framework 4.0,即使是.NET Framework 4.0 client profile也是不行的,因为会间接使用到不包含在client profile中的System.Web程序集,还需要更新工程的app.config为:

      <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>

    否则会提示类似“Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.”的错误。

    Microsoft.Dynamics.BusinessConnectorNet命名空间下包含了登陆AX、AX纪录等众多的类型,访问AX纪录数据的一个典型用法是这样的:

    Microsoft.Dynamics.BusinessConnectorNet.Axapta DynAx = new
        Microsoft.Dynamics.BusinessConnectorNet.Axapta();
                Microsoft.Dynamics.BusinessConnectorNet.AxaptaRecord DynRec;
                try
                {
                    // Authenticate the user and establish a session.
                    DynAx.Logon(null, null, null, null);
    
                    // Define the record as the CustTrans table.
                    DynRec = DynAx.CreateAxaptaRecord("CustTrans");
    
                    // Define the query that will run on the CustTrans records.
                    // This will return all the data in the CustTrans table with 
                    // the cursor positioned at the first record in the table.
                    string fieldAccountNum = ("AccountNum");
                    DynRec.ExecuteStmt(string.Format(@"select * from %1 where %1.{0} ==
        '1102'", fieldAccountNum));
    
                    // Check if the query returned any data.
                    if (DynRec.Found)
                    {
                        // Display the record on the form that was retrieved from the query.
                        textBox1.Text = (string)DynRec.get_Field("AccountNum");
                    }
                }
                // In a finally block, log the user off.
                finally
                {
                    DynAx.Logoff();
                }
            }

    首先创建一个Axapta类型的对象,从它登陆连接到AX系统,在Axapta.Logon()参数中可以指定要登陆到公司、语言、AOS服务器等信息,再创建AxaptaRecord类型的对象,使用这个对象来访问AX表的数据。如果得到的结果集不只一条纪录,可以使用while (axRecord.Found) {.... axRecord.Next();}来循环读取所有纪录。

    AxaptaRecord对象也可以用来向AX的表中插入纪录,比如:

                // Create the .NET Business Connector objects.
                Axapta ax;
                AxaptaRecord axRecord;
                string tableName = "CustStatisticsGroup";
    
                try
                {
                    // Login to Microsoft Dynamics AX.
                    ax = new Axapta();
                    ax.Logon(null, null, null, null);
    
                    // Create a new CustStatisticsGroup table record.
                    using (axRecord = ax.CreateAxaptaRecord(tableName))
                    {
                        // Provide values for each of the CustStatisticsGroup record fields.
                        axRecord.set_Field("CustStatisticsGroup", "04");
                        axRecord.set_Field("StatGroupName", "No Priority Customer");
    
                        // Commit the record to the database.
                        axRecord.Insert();
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(string.Format("Error encountered: {0}", exception.Message), 
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    // Take other error action as needed.
                }

    同样AxaptaRecord也可以更新纪录数据:

           Axapta ax;
                AxaptaRecord axRecord;
                string tableName = "CustStatisticsGroup";
                try
                {
                    // Login to Microsoft Dynamics AX.
                    ax = new Axapta();
                    ax.Logon(null, null, null, null);
                    // Update the ‘High Priority Customer’    table record.
                    using (axRecord = ax.CreateAxaptaRecord(tableName))
                    {
    
                        // Execute a query to retrieve an editable record where the StatGroupName is “High Priority Customer”.
                        axRecord.ExecuteStmt("select forupdate * from %1 where %1.CustStatisticsGroup =='01'");
                        // If the record is found then update the record.
                        if (axRecord.Found)
                        {
                            // Start a transaction that can be committed.
                            ax.TTSBegin();
                            axRecord.set_Field("StatGroupName", "Priority Customer");
                            axRecord.Update();
                            // Commit the transaction.
                            ax.TTSCommit();
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error encountered: {0}", e.Message);
                    // Take other error action as needed.
                }

     和删除纪录

    Axapta ax;
    AxaptaRecord axRecord;
    string tableName = "CustStatisticsGroup";
    try
    {
        // Login to Microsoft Dynamics AX.
        ax = new Axapta();
        ax.Logon(null, null, null, null);
        // Delete the ‘Priority Customer’ table record.
        using (axRecord = ax.CreateAxaptaRecord(tableName))
        {
     
            // Execute a query to retrieve an editable record where the StatGroupName is “High Priority Customer”.
            axRecord.ExecuteStmt("select forupdate * from %1 where %1.CustStatisticsGroup =='01'");
            // If the record is found then delete the record.
            if (axRecord.Found)
            {
                // Start a transaction that can be committed.
                ax.TTSBegin();
                axRecord.Delete();
                // Commit the transaction.
                ax.TTSCommit();
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error encountered: {0}", e.Message);
        // Take other error action as needed.
    }

    注意在做更新和删除纪录时需要使用select forupdate锁定纪录,并使用Axapta.TTSBegin()和Axapta.TTSCommit()开启事务支持。

    除了使用AxaptaRecord访问表纪录,也可以使用Axapta调用类的静态方法

    Axapta ax;
                string sID = "@SYS21669";
                object o;
                bool b;
    
                try
                {
                    // Login to Microsoft Dynamics Ax.
                    ax = new Axapta();
                    ax.Logon(null, null, null, null);
    
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred in object creation or Axapta logon: {0}", e.Message);
                    return;
                }
    
                // Logon was successful.
                try
                {
                    // Call a static class method.
                    // In this example, call SysLabel::labelId2String2 
                    // to determine the label string for a particular label ID.
                    o = ax.CallStaticClassMethod("SysLabel", "labelId2String2", sID);
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error has been encountered during  CallStaticClassMethod: {0}", e.Message);
                    b = ax.Logoff();
                    return;
                }
    
                // Display the returned string.
                Console.WriteLine("The label string for {0} is {1}.", sID, o.ToString());
                Console.WriteLine("Press any key to continue.");
                Console.ReadLine();
    
                // Log off from Microsoft Dynamics AX.
                b = ax.Logoff();

    总体来说这些操作纪录和Class方法和早先版本的AX com接口是很类似的。

     在上面的例子中都使用了Axapta.Logon()方法使用当前Windows登陆用户来连接AX,Axapta对象还提供了LogonAs()方法,可以指定登陆AX的用户账户,其用法是:

    Microsoft.Dynamics.BusinessConnectorNet.Axapta DynAx;
    DynAx = new Microsoft.Dynamics.BusinessConnectorNet.Axapta();
    System.Net.NetworkCredential nc = new 
    System.Net.NetworkCredential("ProxyUserId", "password");
    DynAx.LogonAs(Environment.UserName,"FullyQualifiedDomainName",nc,"dat",
    "en-us"," company1@AOS:2713","AXClient");
    DynAx.LogonAs(Environment.UserName,"FullyQualifiedDomainName",nc,"dat",
    "en-us","company1@AOS:2713","c:\\Client.axc");

    这里使用了NetworkCredetial传入了一个代理账号,就是说登陆用户可以和.net bussiness connector代理用户不是一个账号,这个账号必须在System administrator->Setup->System service account中定义,一些AX的组件比如Enterpsie portal使用这个代理账号连接到AOS。

    只有满足以下条件中一条的用户才可以使用.NET Bussiness connector:

    • 用户被授予系统管理员角色
    • 用户被授予Bussiness connector角色
    • 用户被授予.Net business connector 操作特权,包括SysCom、SysComExecution、SysComDataCreate等。

    更多有关.NET bussiness connector的内容见http://msdn.microsoft.com/en-us/library/aa659581

  • 相关阅读:
    P2832 行路难
    P2634 [国家集训队]聪聪可可
    模拟退火算法
    洛谷 P2986 [USACO10MAR]Great Cow Gat…(树形dp+容斥原理)
    bzoj1040: [ZJOI2008]骑士(基环树dp)
    洛谷P2014 选课(树形dp)
    洛谷P3047 [USACO12FEB]Nearby Cows(树形dp)
    bzoj1026: [SCOI2009]windy数(数位dp)
    hdu3555Bomb(数位dp)
    hdu3652B-number(数位dp)
  • 原文地址:https://www.cnblogs.com/duanshuiliu/p/2681642.html
Copyright © 2011-2022 走看看