AX 2009 外部调用

外部调用 AX 的表方法 类方法

外部使用存储过程,或是SQL语句最多只能进行查询和删除的动作,不能增加和修改。

因为AX,每一行都有一个Recid字段,是系统生成的,为了标识系统唯一行数据。

通过BC连接来访问AX,调用AX的类,调用Job,使用AX的表,进行增,删,改,查。使用表的方法,Display等。

引用Microsoft.Dynamics.BusinessConnectorNet,使用域票据,来访问AX。

这新的一年共享一个,我瞎写的AXHelper,愿与君共勉。

/* ***********************************************
*
* Copyright(c) Kurodo
*
* CLR : 3.5
*
* FileName : AXHelper.cs
*
* Author : Kurodo
*
* CreatTime : 2011/11/29 15:28:43
*           
************************************************
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using Microsoft.Dynamics.BusinessConnectorNet;

namespace AXLibrary
{
     public  class AXHelper
    {
         ///  
        
///  Create Method
        
///  

        
///   表名
        
///   字段值字典
         public  static Boolean Create(String tableName, IDictionary dict)
        {
            Boolean ret =  false;
            Axapta ax =  new Axapta();
            NetworkCredential nc =  new NetworkCredential( " kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo ""kurodo ", nc,  nullnullnullnull);

                IEnumerator> dem = dict.GetEnumerator();
                 using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
                {
                    ax.TTSBegin();
                    axRecord.Clear();
                    axRecord.InitValue();
                    
                     while (dem.MoveNext())
                    {
                        String key = dem.Current.Key;
                        Object value = dem.Current.Value;

                        axRecord.set_Field(key,value);
                    }
                     if (axRecord.ValidateWrite())
                    {
                        axRecord.Insert();
                        ax.TTSCommit();
                        ret =  true;
          
                    }
                     else
                    {
                        ax.CallStaticClassMethod( " CAMErrorInfo "" throwInfo ");
                        ax.TTSAbort();
                    }
                }
                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                MessageBox.Show(ex.Message);
                 // throw new Exception(ex.Message);
            }

             return ret;
        }

         ///  
        
///  Update Method
        
///  

        
///   表名
        
///   键名称
        
///   条件值
        
///   字段值字典
         public  static Boolean Update(String tableName,String pkey,String  where,IDictionary dict)
        {
             return Update(tableName, pkey,  wherenullnull, dict);
        }

         ///  
        
///  Update Method
        
///  

        
///   表名
        
///   键名称
        
///   条件值
        
///   字段值字典
         public  static Boolean Update(String tableName, String pkey1, String where1, String pkey2, String where2, IDictionary dict)
        {
             return Update(tableName, pkey1, where1, pkey2, where2, null, null, dict);
        }

         ///  
        
///  Update Method
        
///  

        
///   表名
        
///   键名称
        
///   条件值
        
///   字段值字典
         public  static Boolean Update(String tableName, String pkey1, String where1,String pkey2,String where2,String pkey3,String where3,IDictionary dict)
        {
            Boolean ret =  false;
            Axapta ax =  new Axapta();
            NetworkCredential nc =  new NetworkCredential( "kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo ""kurodo ", nc,  nullnullnullnull);

                IEnumerator> dem = dict.GetEnumerator();
                 using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
                {
                    String sql = String.Empty;
                    ax.TTSBegin();
                     if (pkey3 !=  null)
                    {
                        sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' && %1.{2} == {3} && %1.{4} == {5} ", pkey1, where1,pkey2,where2,pkey3,where3);
                    }
                     else  if (pkey2 !=  null)
                    {
                        sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' && %1.{2} == {3} ", pkey1, where1, pkey2, where2);
                    }
                     else
                    {
                        sql = String.Format( " select forupdate * from %1 where %1.{0} == '{1}' ", pkey1, where1);
                    }
                    axRecord.ExecuteStmt(sql);

                     if (axRecord.Found)
                    {

                         while (dem.MoveNext())
                        {
                             string key = dem.Current.Key;
                            Object value = dem.Current.Value;

                            axRecord.set_Field(key, value);
                        }

                         // if (axRecord.ValidateWrite())
                        
// {
                            axRecord.Update();
                            ax.TTSCommit();
                            ret =  true;
                         // }
                    }
                     else
                        ax.TTSAbort();
                }
                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                 throw  new Exception(ex.Message);
            }

             return ret;
        }

         ///  
        
///  Select Method
        
///  

        
///   表名称
        
///   键名称
        
///   条件值
        
///   字段列表
        
///  
         public  static IList Select(String tableName, String pkey, String  where, IList fieldList)
        {
             return Select(tableName, pkey,  wherenullnull, fieldList);
        }

         ///  
        
///  Select Method
        
///  

        
///   表名称
        
///   键名称1
        
///   条件值1
        
///   键名称2
        
///   条件值2
        
///   字段列表
        
///  
         public  static IList Select(String tableName, String pkey1, String where1,String pkey2,String where2, IList fieldList)
        {
            IList ret;
            Axapta ax =  new Axapta();
            NetworkCredential nc =  new NetworkCredential( "kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo ""kurodo ", nc,  nullnullnullnull);

                ret =  new List();

                 using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
                {
                    String sql = String.Empty;
                     if (pkey2 !=  null)
                    {
                        sql = String.Format( " select * from %1 where %1.{0} == '{1}' && %1.{2} == {3} ", pkey1, where1, pkey2, where2);
                    }
                     else
                    {
                        sql = String.Format( " select * from %1 where %1.{0} == '{1}' ", pkey1, where1);
                    }
                    axRecord.ExecuteStmt(sql);

                     if (axRecord.Found)
                    {
                         foreach (String field  in fieldList)
                        {
                            ret.Add(axRecord.get_Field(field));
                        }
                    }

                }
                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                 throw  new Exception(ex.Message);
            }

             return ret;
        }

         ///  
        
///  Call Table Method
        
///  

        
///   表名称
        
///   方法名称
        
///  
         public  static Object Call(String tableName, String callName)
        {
             return Call(tableName, callName,  null);
        }

         ///  
        
///  Call Table Method
        
///  

        
///   表名称
        
///   方法名称
        
///   参数列表
        
///  
         public  static Object Call(String tableName,String callName, params Object[] parmList)
        {
            Object ret =  null;
            Axapta ax =  new Axapta();
            NetworkCredential nc =  new NetworkCredential( "kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo ""kurodo ", nc,  nullnullnullnull);

                 using (AxaptaRecord axRecord = ax.CreateAxaptaRecord(tableName))
                {
                    ret = axRecord.Call(callName, parmList);
                }
                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                 throw  new Exception(ex.Message);
            }

             return ret;
        }

         ///  
        
///  Call Display Method 
        
///  

        
///   表名称
        
///   方法名称
        
///   参数列表
        
///  
         public  static Object CallDisplay(String tableName, String methodName,  params Object[] findParam)
        {
            Object ret =  null;
            Axapta ax =  new Axapta();
            NetworkCredential nc =  new NetworkCredential( "kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo ""kurodo ", nc,  nullnullnullnull);
                 using (AxaptaRecord axRecord = ax.CallStaticRecordMethod(tableName,  " find ", findParam)  as AxaptaRecord)
                {
                    ret = axRecord.Call(methodName);
                }
                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                 throw  new Exception(ex.Message);
            }

             return ret;
        }

         ///  
        
///  Call StaticClass Method
        
///  

        
///   类名称
        
///   方法名称
        
///  
         public  static Object CallStaticClass(String className, String methodName)
        {
             return CallStaticClass(className, methodName,  null);
        }

         ///  
        
///  Call StaticClass Method
        
///  

        
///   类名称
        
///   方法名称
        
///   参数列表
        
///  
         public  static Object CallStaticClass(String className, String methodName,  params Object[] paramList)
        {
            Object ret =  null;
            Axapta ax =  new Axapta();
            NetworkCredential nc =  new NetworkCredential( "kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo "" kurodo ", nc,  nullnullnullnull);

                ret = ax.CallStaticClassMethod(className, methodName, paramList);

                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                 throw  new Exception(ex.Message);
            }

             return ret;
        }

         ///  
        
///  Call Job
        
///  

        
///   Job名称
         public  static  void CallJob(String jobName)
        {
            CallJob(jobName,  nullnull);
        }

         ///  
        
///  Call Job
        
///  

        
///   Job名称
        
///   类名称
         public  static  void CallJob(String jobName, String className)
        {
            CallJob(jobName, className,  null);
        }

         ///  
        
///  Call Job
        
///  

        
///   Job名称
        
///   类名称
        
///   参数列表
         public  static  void CallJob(String jobName,String className, params Object[] paramList)
        {
            Axapta ax =  new Axapta();
            AxaptaObject ao =  null;

             if (className !=  null)
            {
                ao = ax.CreateAxaptaObject(className,paramList);
            }

            NetworkCredential nc =  new NetworkCredential( "kurodo "" abc@123 ""kurodo .cn ");
             try
            {
                ax.LogonAs( "kurodo ""kurodo ", nc,  nullnullnullnull);

                ax.CallJob(jobName,ao);

                ax.Logoff();
            }
             catch (Exception ex)
            {
                ax.Logoff();
                 throw  new Exception(ex.Message);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/Kurodo/archive/2012/01/02/2309808.html

你可能感兴趣的:(AX 2009 外部调用)