C#与SAP接口对接

tech2022-09-30  79

1、传入传出都是表

try { RfcDestination destination = RfcDestinationManager.GetDestination("DEV"); RfcFunctionMetadata BAPI_COMPANYCODE_GETDETAIL_MD = destination.Repository.GetFunctionMetadata("ZMM_PR_CHANGE"); //RFC函数名称 myfun = BAPI_COMPANYCODE_GETDETAIL_MD.CreateFunction(); //定义输入函数 IRfcTable tSAP = myfun.GetTable("IT_PR"); for (int i = 0; i < dt.Rows.Count; i++) { IRfcStructure struSAP = tSAP.Metadata.LineType.CreateStructure(); #region struSAP.SetValue("PREQ_NO", dt.Rows[i]["采购申请编号"].ToString()); struSAP.SetValue("PREQ_ITEM", dt.Rows[i]["采购申请的项目编号"].ToString()); struSAP.SetValue("DELETE_IND", dt.Rows[i]["采购凭证删除标识"].ToString()); #endregion tSAP.Append(struSAP); } myfun.SetValue("IT_PR", tSAP); //table 参数 myfun.Invoke(destination); IRfcTable dtSAPData = myfun.GetTable("IT_PR"); string errRow = "", errMessage = ""; for (int i = 0; i < dtSAPData.RowCount; i++) { IRfcStructure stru = dtSAPData[i]; #region sql = "update MM_purPurchaseApplyDetail set MTYPE = '" + stru.GetValue("MTYPE").ToString().Trim() + "',MESSAGE = '" + stru.GetValue("MESSAGE").ToString().Trim() + "' where BillNo = '" + txt_BillNo.Text.Trim() + "' and RowCode = '" + stru.GetValue("PREQ_ITEM").ToString().Trim() + "' "; if (!DB.AF.sqlExec(sql, sqlconnSAP, out errMsg)) { XtraMessageBox.Show("更新PR明细失败!"); } #endregion if (stru.GetValue("MTYPE").ToString().Trim() != "S") { errRow += stru.GetValue("PREQ_ITEM").ToString().Trim() + " "; errMessage += "第[" + stru.GetValue("PREQ_ITEM").ToString().Trim() + "]行:" + stru.GetValue("MESSAGE").ToString() + "\n"; } } if (errRow.Length > 0) { XtraMessageBox.Show("第[" + errRow + "]行Sap单据修改失败!失败原因为:" + "\n" + errMessage); } else XtraMessageBox.Show("SAP单据修改成功!"); } catch (Exception ex) { XtraMessageBox.Show(ex.Message); }

2、传入表、返回结构

RfcDestination destination = RfcDestinationManager.GetDestination("DEV"); RfcFunctionMetadata BAPI_COMPANYCODE_GETDETAIL_MD = destination.Repository.GetFunctionMetadata("ZMM_BAPI_GOODSMVT_CREATE_N"); //RFC函数名称 myfun = BAPI_COMPANYCODE_GETDETAIL_MD.CreateFunction(); //定义输入函数 IRfcTable tSAP = myfun.GetTable("IT_RES"); for (int i = 0; i < GV.RowCount; i++) { IRfcStructure struSAP = tSAP.Metadata.LineType.CreateStructure(); #region struSAP.SetValue("RSNUM", ""); //预留/相关需求的编号 struSAP.SetValue("RSPOS", ""); //预留 / 相关需求的项目编号 struSAP.SetValue("AUFNR", GV.GetRowCellValue(i, "订单号").ToString()); //订单号 #endregion tSAP.Append(struSAP); } myfun.SetValue("IT_RES", tSAP); //table 参数 try { myfun.Invoke(destination); } catch (Exception ex) { XtraMessageBox.Show("调用接口失败!"+ex.Message); } IRfcStructure getstu = myfun.GetStructure("O_RETURN"); DOCNO = getstu.GetValue("DOCNO").ToString(); MTYPE = getstu.GetValue("MSGTP").ToString(); MESSAGE = getstu.GetValue("MSTXT").ToString();

3、传入表、返回字段

try { RfcDestination destination = RfcDestinationManager.GetDestination("DEV"); RfcFunctionMetadata BAPI_COMPANYCODE_GETDETAIL_MD = destination.Repository.GetFunctionMetadata("ZMM_PR_CREATE"); //RFC函数名称 myfun = BAPI_COMPANYCODE_GETDETAIL_MD.CreateFunction(); //定义输入函数 //IRfcFunction func = rfcRep.CreateFunction("ZMM_IS_PR_CREATE"); IRfcTable tSAP = myfun.GetTable("IT_PR"); for (int i = 0; i < dt.Rows.Count; i++) { IRfcStructure struSAP = tSAP.Metadata.LineType.CreateStructure(); #region struSAP.SetValue("PREQ_ITEM", dt.Rows[i]["采购申请的项目编号"].ToString()); struSAP.SetValue("PR_TYPE", dt.Rows[i]["订单类型"].ToString()); struSAP.SetValue("PR_TEXT", dt.Rows[i]["备注"].ToString()); struSAP.SetValue("PLANT", dt.Rows[i]["工厂"].ToString()); struSAP.SetValue("PUR_GROUP", dt.Rows[i]["采购组"].ToString()); #endregion tSAP.Append(struSAP); } myfun.SetValue("IT_PR", tSAP); //table 参数 myfun.Invoke(destination); EV_NUMBER = myfun.GetString("EV_NUMBER"); MTYPE = myfun.GetString("MTYPE"); MESSAGE = myfun.GetString("MESSAGE");

4、传入字段、返回表

sesstab.Clear(); try { RfcDestination destination = RfcDestinationManager.GetDestination("DEV"); RfcFunctionMetadata BAPI_COMPANYCODE_GETDETAIL_MD = destination.Repository.GetFunctionMetadata("ZMM_GET_STOCK_QUANTITY_MUL"); //RFC函数名称 myfun = BAPI_COMPANYCODE_GETDETAIL_MD.CreateFunction(); // myfun.SetValue("IV_MATERIAL", "" + txt_MaterialID.Text.Trim() + ""); //物料编号 myfun.SetValue("IV_PLANT", "" + ddl_Company.EditValue.ToString().Trim() + ""); //工厂 myfun.SetValue("IV_STGE_LOC", "" + ddl_Warehouse.EditValue.ToString().Trim() + ""); //库存地点 myfun.SetValue("IV_BATCH", "" + txt_BatchNo.Text.Trim() + ""); //批号 myfun.Invoke(destination); MTYPE = myfun.GetString("MTYPE"); MESSAGE = myfun.GetString("MESSAGE"); IRfcTable tSAP = myfun.GetTable("ET_STOCK"); XtraMessageBox.Show(MTYPE+":"+ MESSAGE); for (int i = 0; i < tSAP.RowCount; i++) { IRfcStructure stru = tSAP[i]; DataRow dr = sesstab.NewRow(); dr["物料编号"] = stru.GetValue("MATERIAL").ToString(); dr["工厂"] = stru.GetValue("PLANT").ToString(); dr["库存地点"] = stru.GetValue("STGE_LOC").ToString(); dr["批号"] = stru.GetValue("BATCH").ToString(); dr["库存"] = stru.GetValue("LABST").ToString(); dr["物料描述"] = stru.GetValue("MAKTX").ToString(); dr["规格型号"] = stru.GetValue("GROES").ToString(); dr["基本计量单位"] = stru.GetValue("MEINS").ToString(); sesstab.Rows.Add(dr); } GC.DataSource = sesstab; } catch (Exception ex) { XtraMessageBox.Show(ex.Message); }

 

最新回复(0)