[摘要]本文是对C#调用sap接口及返回数据到sap的讲解,对学习C#编程技术有所帮助,与大家分享。
public class SapClass { /// <summary> /// /// </summary> /// <param name="fphm">发票号码</param> /// <param name="fprq">发票开票日期</param> /// <param name="fkf">付款方</param> /// <param name="fxqd">分销渠道 </param> public DataTable nco(string fphm, DateTime fprq, string fkf, char fxqd) { //MyBackendConfig config = new MyBackendConfig(); IDestinationConfiguration ID = new MyBackendConfig(); RfcDestinationManager.RegisterDestinationConfiguration(ID); RfcDestination prd = RfcDestinationManager.GetDestination(PlatRunning.Global.AppSettings.GetValue("SapName"));//SAP系统标识 DataTable dt = nco(prd, fphm, fprq, fkf, fxqd); RfcDestinationManager.UnregisterDestinationConfiguration(ID); return dt; } /// <summary> /// SAPTOJS /// </summary> /// <param name="prd"></param> /// <param name="fphm"></param> /// <param name="fprq"></param> /// <param name="fkf"></param> /// <param name="fxqd"></param> private DataTable nco(RfcDestination prd, string fphm, DateTime fprq, string fkf, char fxqd) { RfcRepository repo = null; try { repo = prd.Repository; } catch (Exception ex) { throw (ex); } IRfcFunction companyBapi = repo.CreateFunction("ZSD_SY_FUC_SAPTOJS"); //调用函数名 //设置Import的参数 companyBapi.SetValue("VBELN", fphm); companyBapi.SetValue("ERDAT", fprq); companyBapi.SetValue("KUNRG", fkf); companyBapi.SetValue("ZBJ", fxqd); companyBapi.Invoke(prd); //执行函数 IRfcTable table = companyBapi.GetTable("IT_JS_EXPORT"); //获取相应的内表IT_JS_EXPORT //提前实例化一个空的表结构处理 DataTable dt = CreateSapToJSTable(); for (int i = 0; i < table.Count; i++) { table.CurrentIndex = i; DataRow dr = dt.NewRow(); dr["fxqd"] = table[i].GetString("VTWEG"); dr["fphm"] = table[i].GetString("VBELN"); dr["fphh"] = table[i].GetString("POSNR"); dr["fpcjsj"] = table[i].GetString("ERDAT"); dr["khdm"] = table[i].GetString("KUNRG"); dr["khmc"] = table[i].GetString("NAME"); dr["khdzch"] = table[i].GetString("STRAS"); dr["sapddh"] = table[i].GetString("AUBEL"); dr["khddh"] = table[i].GetString("BSTKD"); dr["sapwlh"] = table[i].GetString("MATNR"); dr["khwlh"] = table[i].GetString("KDMAT"); dr["spmc"] = table[i].GetString("ARKTX"); dr["kpm"] = table[i].GetString("TEXT"); dr["jldw"] = table[i].GetString("VRKME"); dr["sl"] = table[i].GetString("FKIMG"); dr["bb"] = table[i].GetString("WAERK"); dr["dj"] = table[i].GetString("KBETR"); dr["jgdw"] = table[i].GetString("KPEIN"); dr["zj"] = table[i].GetString("NETWR"); dr["shl"] = table[i].GetString("MWSK1"); dr["se"] = table[i].GetString("MWSBP"); dr["myxz"] = table[i].GetString("KTGRM"); dt.Rows.Add(dr); } return dt; } /// <summary> /// /// </summary> /// <param name="sapfph">sap发票号</param> /// <param name="qtfph">生成发票号</param> public DataTable nco(string sapfph, string qtfph) { //MyBackendConfig config = new MyBackendConfig(); IDestinationConfiguration ID = new MyBackendConfig(); RfcDestinationManager.RegisterDestinationConfiguration(ID); RfcDestination prd = RfcDestinationManager.GetDestination(PlatRunning.Global.AppSettings.GetValue("SapName"));//SAP系统标识 DataTable dt = nco(prd, sapfph, qtfph); RfcDestinationManager.UnregisterDestinationConfiguration(ID); return dt; } /// <summary> /// JSTOSAP /// </summary> /// <param name="prd"></param> /// <param name="sapfph"></param> /// <param name="qtfph"></param> private DataTable nco(RfcDestination prd, string sapfph, string qtfph) { DataTable dt = WriteTableToSAP(prd, sapfph, qtfph); return dt; } #region 写回数据到SAP private DataTable WriteTableToSAP(RfcDestination rfcDest, string sapfph, string qtfph) { RfcRepository repo = rfcDest.Repository; IRfcFunction funBapi = repo.CreateFunction("ZSD_SY_FUC_JSTOSAP"); //调用函数名 IRfcTable tblROF = funBapi.GetTable("IT_JSTOSAP"); tblROF.Clear(); tblROF.Insert(); tblROF.CurrentRow.SetValue("VBELN", sapfph); tblROF.CurrentRow.SetValue("JSVBELN", qtfph); funBapi.SetValue("IT_JSTOSAP", tblROF); funBapi.Invoke(rfcDest); //引用回传结果 IRfcTable ReturnTable = funBapi.GetTable("IT_RETURN"); DataTable dtReturn = ReturnMessage(); for (int i = 0; i < ReturnTable.Count; i++) { ReturnTable.CurrentIndex = i; DataRow dr = dtReturn.NewRow(); dr["isSuccess"] = ReturnTable[i].GetString("TYPE"); dr["ReturnMessage"] = ReturnTable[i].GetString("MESSAGE"); dtReturn.Rows.Add(dr); } return dtReturn; } #endregion #region 接口参数 public class MyBackendConfig : IDestinationConfiguration { public RfcConfigParameters GetParameters(String destinationName) { if ("DEV".Equals(destinationName)) { string strtemp = string.Empty; RfcConfigParameters parms = new RfcConfigParameters(); parms.Add(RfcConfigParameters.AppServerHost, "192.168.0.122");//SAP主机IP parms.Add(RfcConfigParameters.SystemNumber, "00");//SAP实例 parms.Add(RfcConfigParameters.User, "SYRFC");//用户名 parms.Add(RfcConfigParameters.Password, "123456");//密码 parms.Add(RfcConfigParameters.Client, "710");// Client parms.Add(RfcConfigParameters.Language, "ZH");//登陆语言 parms.Add(RfcConfigParameters.PoolSize, "5"); parms.Add(RfcConfigParameters.MaxPoolSize, "10"); parms.Add(RfcConfigParameters.IdleTimeout, "60"); return parms; } else return null; } public bool ChangeEventsSupported() { return false; } public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged; } #endregion public DataTable CreateSapToJSTable() { DataTable dt = new DataTable(); dt.Columns.Add("fxqd"); //分销渠道 dt.Columns.Add("fphm");//发票号码 dt.Columns.Add("fphh");//发票行号 dt.Columns.Add("fpcjsj");//发票创建时间 dt.Columns.Add("khdm");//客户代码 dt.Columns.Add("khmc");//客户名称 dt.Columns.Add("khdzch");//客户地址(中文) dt.Columns.Add("sapddh");//sap订单号 dt.Columns.Add("khddh");//客户订单号 dt.Columns.Add("sapwlh");//sap物料号 dt.Columns.Add("khwlh");//客户物料号 dt.Columns.Add("spmc");//商品名称 dt.Columns.Add("kpm");//开票名 dt.Columns.Add("jldw");//计量单位 dt.Columns.Add("sl");//数量 dt.Columns.Add("bb");//币别 dt.Columns.Add("dj");//含税单价 dt.Columns.Add("jgdw");//价格单位 dt.Columns.Add("zj");//总价 dt.Columns.Add("shl");//税率 dt.Columns.Add("se");//税额 dt.Columns.Add("myxz");//贸易性质 return dt; } public DataTable ReturnMessage() { DataTable dt = new DataTable(); dt.Columns.Add("isSuccess"); //成功与否 dt.Columns.Add("ReturnMessage");//Message return dt; } }