SAP接口的调用

最近做一个专案用到的SAO接口的调用,用到的上传参数获取回传的IRfcTable,以及以IRfcTable作为参数上传SAP,通过查阅很多资料,发现资料说明的也多是鱼龙混杂,许多没有实现就直接贴在上面,有的还是值得借鉴的,专案完成后,记录下,好记性不如烂笔头,也分享给大伙借阅。

1.SAP连接

  这里先设置SAP连接

  public static RfcDestination GetRfcDest()
        {
            RfcConfigParameters configParams = GetConfigParams();
            RfcDestination dest = RfcDestinationManager.GetDestination(configParams);
            return dest;
        }
        private static RfcConfigParameters GetConfigParams()
        {
            RfcConfigParameters configParams = new RfcConfigParameters();
            //正式服务器
            configParams.Add(RfcConfigParameters.Name, "SAP_CONN");
            configParams.Add(RfcConfigParameters.AppServerHost, "xxx.xxx.xxx.xxx");//正式机
            configParams.Add(RfcConfigParameters.SystemNumber, "00");
            configParams.Add(RfcConfigParameters.User, "B2BACL");
            configParams.Add(RfcConfigParameters.Password, "ACLACL");
            configParams.Add(RfcConfigParameters.Client, "168");
            configParams.Add(RfcConfigParameters.Language, "EN");
            configParams.Add(RfcConfigParameters.PoolSize, "5");
            configParams.Add(RfcConfigParameters.MaxPoolSize, "10");
            configParams.Add(RfcConfigParameters.IdleTimeout, "600");
            return configParams;
        }

         2.通过参数获取对应的IRfcTable

      

 /// <summary>
        /// 通过参数获取SAP中Table返回值(Table)
        /// </summary>
        /// <param name="ASN"></param>
        /// <returns></returns>
        public static DataTable method(string ASN)
        {
          
            DataTable result = null;
            IRfcTable value = null;
            try
            {
                RfcDestination rfcdes = SapConn.GetRfcDest();
                RfcRepository repos = rfcdes.Repository;
                IRfcFunction func = repos.CreateFunction("方法名"); //RFC函数名
                func.SetValue("参数名", ASN); //测量点
                func.Invoke(rfcdes);
                value = func.GetTable("需要获取的表明"); //输出参数      
                DataTable dt = new DataTable();
                //新建列
                dt.Columns.Add("VBELN");
                dt.Columns.Add("POSNR");
                dt.Columns.Add("VGBEL");
                dt.Columns.Add("VGPOS");
                for (int i = 0; i < value.Count; i++)
                {
                    value.CurrentIndex = i;
                    DataRow dr = dt.NewRow();//新建行
                    dr["VBELN"] = value.GetString("VBELN");
                    dr["POSNR"] = value.GetString("POSNR");
                    dr["VGBEL"] = value.GetString("VGBEL");
                    dr["VGPOS"] = value.GetString("VGPOS");
                    dt.Rows.Add(dr);
                }
                System.Threading.Thread.Sleep(100);
                dt.DefaultView.Sort = "LFIMG ASC";//按Id倒序
                dt = dt.DefaultView.ToTable();//返回一个新的DataTable
                result = dt; 
            }
            catch (Exception ex)
            {
               
            }
            return result;
        }

    3.将参数以IRfcTable形式上传SAP

     

  public static void PostTableToASap(string StaffNO)
        {

            RfcDestination rfcdes = SapConn.GetRfcDest();
            RfcRepository repos = rfcdes.Repository;
            IRfcFunction func = null;
            string ASN = null;
            func= repos.CreateFunction("RFC函数名"); //RFC函数名  
            try
            {
                //表头部分
                IRfcStructure GOODSMVT_HEADER = func.GetStructure("GOODSMVT_HEADER");
                string time = DateTime.Now.ToString("yyyyMMdd");
                GOODSMVT_HEADER.SetValue("参数1", time); //测量点
                GOODSMVT_HEADER.SetValue("参数2", time); //测量点
                GOODSMVT_HEADER.SetValue("参数3", ""); //测量点
                func.SetValue("GOODSMVT_HEADER", GOODSMVT_HEADER);
                //Get需要操作的表T_PURORDER_IN
                IRfcTable itb = func.GetTable("表名");
 for (int i = 0; i < DT.Rows.Count-1; i++)
                            {
                                itb.Insert();
                                itb.CurrentRow.SetValue("列1", DT.Rows[i]["VGBEL"].ToString());
                                itb.CurrentRow.SetValue("列2", DT.Rows[i]["VGPOS"].ToString());
                                itb.CurrentRow.SetValue("列3", DT.Rows[i]["MATNR"].ToString());
                                itb.CurrentRow.SetValue("列4", DT.Rows[i]["LFIMG"].ToString());
                               
                            }
               func.SetValue("表名", itb);
                func.Invoke(rfcdes);
           }
            catch (Exception ex)
            {

                LogHelper.WriteLog(typeof(SapConn), ex);
               
            }
}
原文地址:https://www.cnblogs.com/TechnologyDictionary/p/10788642.html