.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號