using System; using System.Collections.Generic; using System.Data; using System.Web; using System.Configuration; using SAP.Middleware.Connector; /// /// SapApi xuwei add 2020-07-15 /// public class SapApi { //SAP服务器配置参数 public static string appServerHost = ConfigurationManager.AppSettings["SapAppServerHost"].ToString(); public static string systemNumber = ConfigurationManager.AppSettings["SapSystemNumber"].ToString(); public static string user = ConfigurationManager.AppSettings["SapUser"].ToString(); public static string password = ConfigurationManager.AppSettings["SapPassword"].ToString(); public static string client = ConfigurationManager.AppSettings["SapClient"].ToString(); /// /// 物料主数据接口(同步最小包装数用) /// /// 物料凭证编号 /// 创建时间 /// 消息类型: S 成功,E 错误,W 警告,I 信息,A 中断 /// 消息文本 /// public static DataTable ZMMFM054(out string ZTYPE, out string ZMSG, string syncType, string MATNR, string MTART) { RfcConfigParameters rfcPara = new RfcConfigParameters(); rfcPara.Add(RfcConfigParameters.AppServerHost, appServerHost); rfcPara.Add(RfcConfigParameters.SystemNumber, systemNumber); rfcPara.Add(RfcConfigParameters.User, user); rfcPara.Add(RfcConfigParameters.Password, password); rfcPara.Add(RfcConfigParameters.Client, client); rfcPara.Add(RfcConfigParameters.Name, "CON"); rfcPara.Add(RfcConfigParameters.Language, "ZH"); rfcPara.Add(RfcConfigParameters.PoolSize, "5"); rfcPara.Add(RfcConfigParameters.ConnectionIdleTimeout, "60"); RfcDestination rfcDest = RfcDestinationManager.GetDestination(rfcPara); RfcRepository rfcRep = rfcDest.Repository; //周期性同步 DateTime dateMinusOneDay = DateTime.Now; ; //接口API IRfcFunction rfcApi = rfcRep.CreateFunction("ZMMFM054"); //输入参数 if (syncType == "Manual") { //单独获取某物料的主数据信息 //if (!string.IsNullOrEmpty(MATNR)) rfcApi.SetValue("IN_MATNR", MATNR); //if (!string.IsNullOrEmpty(MTART)) rfcApi.SetValue("IN_MTART", MTART); rfcApi.SetValue("IN_ALL", "X"); } else if (syncType == "TimeInterval") { //周期性同步 rfcApi.SetValue("IN_ERSDA", dateMinusOneDay.AddDays(-1).ToString("yyyyMMdd")); } else { //期初全量获取 rfcApi.SetValue("IN_ALL", "X"); } //rfcApi.SetValue("IN_ERSDA", dateMinusOneDay.ToString("yyyyMMdd")); rfcApi.SetValue("IN_WERKS", 5200); //调用接口 rfcApi.Invoke(rfcDest); //获取输出 ZTYPE = rfcApi.GetValue("ZTYPE").ToString(); ZMSG = rfcApi.GetValue("ZMSG").ToString(); IRfcTable table = rfcApi.GetTable("OUT_TABLE"); DataTable dt = GetDataTableFromRFCTable(table); rfcDest = null; rfcRep = null; return dt; } /// /// IRfcTable转DataTable /// /// /// private static DataTable GetDataTableFromRFCTable(IRfcTable myrfcTable) { DataTable loTable = new DataTable(); int liElement = 0; for (liElement = 0; liElement <= myrfcTable.ElementCount - 1; liElement++) { RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement); loTable.Columns.Add(metadata.Name); } foreach (IRfcStructure Row in myrfcTable) { DataRow ldr = loTable.NewRow(); for (liElement = 0; liElement <= myrfcTable.ElementCount - 1; liElement++) { RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement); ldr[metadata.Name] = Row.GetString(metadata.Name); } loTable.Rows.Add(ldr); } return loTable; } }