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
    {
        /// <summary>
        
/// Create Method
        
/// </summary>
        
/// <param name="tableName">表名</param>
        
/// <param name="dict">字段值字典</param>
        public static Boolean Create(String tableName, IDictionary<String, Object> 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<KeyValuePair<String, Object>> 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;
        }

        /// <summary>
        
/// Update Method
        
/// </summary>
        
/// <param name="tableName">表名</param>
        
/// <param name="pkey">键名称</param>
        
/// <param name="where">条件值</param>
        
/// <param name="dict">字段值字典</param>
        public static Boolean Update(String tableName,String pkey,String where,IDictionary<String, Object> dict)
        {
            return Update(tableName, pkey, wherenullnull, dict);
        }

        /// <summary>
        
/// Update Method
        
/// </summary>
        
/// <param name="tableName">表名</param>
        
/// <param name="pkey">键名称</param>
        
/// <param name="where">条件值</param>
        
/// <param name="dict">字段值字典</param>
        public static Boolean Update(String tableName, String pkey1, String where1, String pkey2, String where2, IDictionary<String, Object> dict)
        {
            return Update(tableName, pkey1, where1, pkey2, where2,null,null, dict);
        }

        /// <summary>
        
/// Update Method
        
/// </summary>
        
/// <param name="tableName">表名</param>
        
/// <param name="pkey">键名称</param>
        
/// <param name="where">条件值</param>
        
/// <param name="dict">字段值字典</param>
        public static Boolean Update(String tableName, String pkey1, String where1,String pkey2,String where2,String pkey3,String where3,IDictionary<String, Object> 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<KeyValuePair<String, Object>> 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;
        }

        /// <summary>
        
/// Select Method
        
/// </summary>
        
/// <param name="tableName">表名称</param>
        
/// <param name="pkey">键名称</param>
        
/// <param name="where">条件值</param>
        
/// <param name="fieldList">字段列表</param>
        
/// <returns></returns>
        public static IList<Object> Select(String tableName, String pkey, String where, IList<String> fieldList)
        {
            return Select(tableName, pkey, wherenullnull, fieldList);
        }

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

                ret = new List<Object>();

                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;
        }

        /// <summary>
        
/// Call Table Method
        
/// </summary>
        
/// <param name="tableName">表名称</param>
        
/// <param name="callName">方法名称</param>
        
/// <returns></returns>
        public static Object Call(String tableName, String callName)
        {
            return Call(tableName, callName, null);
        }

        /// <summary>
        
/// Call Table Method
        
/// </summary>
        
/// <param name="tableName">表名称</param>
        
/// <param name="callName">方法名称</param>
        
/// <param name="parmList">参数列表</param>
        
/// <returns></returns>
        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;
        }

        /// <summary>
        
/// Call Display Method 
        
/// </summary>
        
/// <param name="tableName">表名称</param>
        
/// <param name="methodName">方法名称</param>
        
/// <param name="findParam">参数列表</param>
        
/// <returns></returns>
        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;
        }

        /// <summary>
        
/// Call StaticClass Method
        
/// </summary>
        
/// <param name="className">类名称</param>
        
/// <param name="methodName">方法名称</param>
        
/// <returns></returns>
        public static Object CallStaticClass(String className, String methodName)
        {
            return CallStaticClass(className, methodName, null);
        }

        /// <summary>
        
/// Call StaticClass Method
        
/// </summary>
        
/// <param name="className">类名称</param>
        
/// <param name="methodName">方法名称</param>
        
/// <param name="paramList">参数列表</param>
        
/// <returns></returns>
        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;
        }

        /// <summary>
        
/// Call Job
        
/// </summary>
        
/// <param name="jobName">Job名称</param>
        public static void CallJob(String jobName)
        {
            CallJob(jobName, nullnull);
        }

        /// <summary>
        
/// Call Job
        
/// </summary>
        
/// <param name="jobName">Job名称</param>
        
/// <param name="className">类名称</param>
        public static void CallJob(String jobName, String className)
        {
            CallJob(jobName, className, null);
        }

        /// <summary>
        
/// Call Job
        
/// </summary>
        
/// <param name="jobName">Job名称</param>
        
/// <param name="className">类名称</param>
        
/// <param name="paramList">参数列表</param>
        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);
            }
        }
    }
}

 

posted @ 2012-01-02 09:50  Kurodo  阅读(1098)  评论(1编辑  收藏  举报