.NET链接SAP
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using SAP.Middleware.Connector ; using System.Windows.Forms;
……
public string SetSapChgScr(string vbeln, string posnr,string mat,string net, string car,string con,string sel,string flag) { string tye = ""; try { RfcDestination rfcDestination = RfcDestinationManager.GetDestination("EP1"); RfcRepository rfcRepository = rfcDestination.Repository; IRfcFunction myfun = rfcRepository.CreateFunction("ZMM_BAPI_OFFCUT_CHANGE"); IRfcStructure Hdata = myfun.GetStructure("HEADER_DATA"); IRfcStructure Hctrl = myfun.GetStructure("HEADER_CONTROL"); IRfcTable Ttext = myfun.GetTable("T_TEXT"); IRfcStructure Stext = rfcRepository.GetStructureMetadata("ZZS_TEXT").CreateStructure(); IRfcTable Tdata = myfun.GetTable("ITEM_DATA"); IRfcStructure Idata = rfcRepository.GetStructureMetadata("BAPIOBDLVITEMCHG").CreateStructure(); IRfcTable Tctrl = myfun.GetTable("ITEM_CONTROL"); IRfcStructure Ictrl = rfcRepository.GetStructureMetadata("BAPIOBDLVITEMCTRLCHG").CreateStructure(); Hdata.SetValue("DELIV_NUMB", vbeln); Hctrl.SetValue("DELIV_NUMB", vbeln); myfun.SetValue("ZBJL", flag); myfun.SetValue("DELIVERY", vbeln); Idata.SetValue("DELIV_NUMB",vbeln); Idata.SetValue("DELIV_ITEM",posnr); Idata.SetValue("MATERIAL",mat); Idata.SetValue("DLV_QTY",net); Idata.SetValue("FACT_UNIT_NOM","1"); Idata.SetValue("FACT_UNIT_DENOM","1"); Tdata.Insert(Idata); Ictrl.SetValue("DELIV_NUMB",vbeln); Ictrl.SetValue("DELIV_ITEM",posnr); Ictrl.SetValue("CHG_DELQTY","X"); Tctrl.Insert(Ictrl); Stext.SetValue("DELIV_NUMB", vbeln); Stext.SetValue("DELIV_ITEM", posnr); Stext.SetValue("ZGH", con); Stext.SetValue("ZCH", car); Stext.SetValue("ZFTH", sel); Ttext.Insert(Stext); myfun.Invoke(rfcDestination); IRfcTable Rtables = myfun.GetTable("RETURN"); tye = Rtables.GetString("TYPE").ToString(); MSG = ""; for (int Z = 0; Z <= Rtables.RowCount - 1; Z++) { MSG = MSG + Rtables.GetString("MESSAGE").ToString(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } return tye; }
/**********************************************************************
// .net调用多个BAPI时,可使用RfcSessionManager,因为Invoke(dest)之后会重设会话状态,所以第二个函数的执行不会成功。
// 设置目前与SAP系统的连接为独占模式,后续函数调用在同一个Session中
RfcSessionManager.BeginContext(sapDestination);
// codes goes here
// 重设会话状态,释放连接
RfcSessionManager.EngdContext(sapDestination);
/**************************************************************************



浙公网安备 33010602011771号