C# 实现调用 SAP Com组件

2/5/2009来源:C#应用人气:9567

 /// <summary>
        /// 登陆SAP系统
        /// </summary>
        /// <returns>登陆状态</returns>
        public LoginStatus LoginSAP()
        {
            try
            {
                SAPLogonCtrl.SAPLogonControlClass logon = new SAPLogonCtrl.SAPLogonControlClass();
                logon.applicationServer = ApplicationServer;     //SAP系统ip
                logon.Client = Client;                           //SAP客户端号
                logon.Language = Language;                       //SAP登陆语言
                logon.User = User;                               //用户帐号
                logon.PassWord = Password;                       //用户密码
                logon.SystemNumber = SystemNumber;               //SAP系统编号
                Conn = (SAPLogonCtrl.Connection)logon.NewConnection();

                if (Conn.Logon(0, true))
                {
                    Status = LoginStatus.Success;                //登陆成功
                }
                else
                {
                    Status = LoginStatus.Fail;                   //登陆失败
                }
                return Status;
            }
            catch (Exception exc)
            {
                throw(new Exception(exc.Message));
            }
        }

        /// <summary>
        /// 调用SAP系统函数模块
        /// </summary>
        /// <param name="strFunName">函数名称</param>
        /// <param name="strArgs">输入参数字典</param>
        /// <param name="strRetTabs">返回表结果字典</param>
        /// <param name="strResult">返回程序运行结果</param>
        /// <returns>返回表结果集</returns>
        public DataSet InvokSAPFun(string strFunName, ListDictionary strArgs, ListDictionary strRetTabs, ref ListDictionary strResult)
        {
            try
            {
                DataSet retDST = new DataSet();
                string[] array = new string[strResult.Count];
                strResult.Keys.CopyTo(array, 0);
                if (Status == LoginStatus.Success)
                {

                    SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsOCX.SAPFunctionsClass();
                    func.Connection = Conn;
                    //(1)
                    SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add(strFunName);               //调用函数模块
                    foreach (string arg in strArgs.Keys)
                    {
                        SAPFunctionsOCX.IParameter gclient = (SAPFunctionsOCX.IParameter)ifunc.get_Exports(arg);     //取得输入参数
                        gclient.Value = strArgs[arg];                                                                //设置参数值              
                    }
                    ifunc.Call(); //调用函数模块
                    //(2)
                    foreach (string ret in array)
                    {
                        SAPFunctionsOCX.IParameter NUMBER = (SAPFunctionsOCX.IParameter)ifunc.get_Imports(ret);      //返回程序运行结果
                        strResult[ret] = NUMBER.Value;
                    }
                    //(3)
                    SAPTableFactoryCtrl.Tables ENQs = (SAPTableFactoryCtrl.Tables)ifunc.Tables;                      //获取所有Tables
                    foreach (string tab in strRetTabs.Keys)
                    {
                        SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)ENQs.get_Item(tab);               //返回指定Tables
                        DataTable dat = ConvertTable(ENQ);
                        retDST.Tables.Add(dat);
                    }

                }
                return retDST;
            }
            catch (Exception exc)
            {
                throw (new Exception(exc.Message));
            }
        }