准备工作:安装sapnco3 sapcon3.0_X64.rar 或 Setup_ntintel_301_32位安装包.rar,根据服务器系统选择32位或64位安装。将安装目录下的dll引用到工程项目中。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using SAP.Middleware.Connector; 6 7 8 9 namespace Sapnoc30Demo_Yan 10 { 11 12 public class SapncoClient 13 { 14 private RfcDestination _rfcDestination = null; 15 16 /// <summary> 17 /// Register Client 18 /// </summary> 19 public void RegisterRfcDestination() 20 { 21 try 22 { 23 if (_rfcDestination == null) 24 _rfcDestination = RfcDestinationManager.GetDestination("NCO_Destination"); //获取 config配置中NCO_Destination节点参数,并注册RfcClient 25 } 26 catch (Exception ex) 27 { 28 throw ex; 29 } 30 } 31 /// <summary> 32 /// 调用Sap服务器函数 33 /// (非IDOC) 34 /// </summary> 35 /// <param name="orderCode"></param> 36 /// <returns></returns> 37 public string InvokeRFCFunctionGetCompanyID(string orderCode) 38 { 39 IRfcFunction function = null; 40 string companyID = String.Empty; 41 try 42 { 43 function = _rfcDestination.Repository.CreateFunction("ZRFC_JGM_INFO2");//调用sap函数 44 function.SetParameterActive(0, true); 45 function.SetValue("VBELN", orderCode);//给函数 ZRFC_JGM_INFO2 传值 46 function.Invoke(_rfcDestination); 47 IRfcTable tb = function.GetTable("E_TAB"); //获取 ZRFC_JGM_INFO2函数返回的 "E_TAB"表 48 49 foreach (IRfcStructure dr in tb) 50 { 51 string orderNumber = dr.GetValue("VBELN").ToString(); //获取表 "E_TAB"结构中 "VBELN" 字段的值 52 if (orderNumber.Equals(orderCode)) 53 { 54 companyID = dr.GetValue("KUNNR").ToString(); 55 break; 56 } 57 } 58 } 59 catch (RfcBaseException e) 60 { 61 throw e; 62 } 63 return companyID ; 64 } 65 66 #region 非IDOC RfcTable 结构 67 // [SAP.Middleware.Connector.RfcTable] 68 //{TABLE [STRUCTURE E_TAB { FIELD KUNNR=0000001000 FIELD NAME1=安徽邦泰医 FIELD ZZDKHID= FIELD ZZDKH= FIELD VBELN=8000000001 FIELD MATNR=000000002015000211 FIELD MAKTX=胞磷胆碱钠氯化钠注射液&100ml:0.5gx80瓶 FIELD CHARG=2013052201 FIELD LFIMG=1.000 FIELD VRKME=KAR }]} 69 #endregion 70 71 } 72 }
1 <?xml version="1.0"?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="SAP.Middleware.Connector"> 5 <sectionGroup name="ClientSettings"> 6 <section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration,sapnco"/> 7 </sectionGroup> 8 <sectionGroup name="ServerSettings"> 9 <section name="ServerConfiguration" type="SAP.Middleware.Connector.RfcServerConfiguration, sapnco"/> 10 </sectionGroup> 11 </sectionGroup> 12 </configSections> 13 <SAP.Middleware.Connector> 14 <ClientSettings> 15 <DestinationConfiguration> 16 <destinations> 17 <add NAME="NCO_Destination" USER="CHA" PASSWD="123" CLIENT="300" SYSNR="00" ASHOST="172.16.8.100" LANG="EN" GROUP="PUBLIC" MAX_POOL_SIZE="5"></add> 18 </destinations> 19 </DestinationConfiguration> 20 </ClientSettings> 21 <ServerSettings> 22 <ServerConfiguration> 23 <servers> 24 <add NAME="NCO_Server" GWHOST="hostsys1" GWSERV="sapgw00" PROGRAM_ID="NCoServer" REPOSITORY_DESTINATION="NCO_TESTS" REG_COUNT="1"/> 25 </servers> 26 </ServerConfiguration> 27 </ServerSettings> 28 </SAP.Middleware.Connector> 29 </configuration>
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace Sapnoc30Demo_Yan 11 { 12 public partial class ClientTest : Form 13 { 14 public ClientTest() 15 { 16 InitializeComponent(); 17 } 18 19 private void ClientTest_Load(object sender, EventArgs e) 20 { 21 SapncoClient client = new SapncoClient(); 22 client.RegisterRfcDestination(); 23 client.InvokeRFCFunctionGetCompanyID(""); 24 } 25 } 26 }
另一种客户端(服务端)注册方式
1 public class MyBackendConfig : IDestinationConfiguration 2 { 3 4 #region IDestinationConfiguration Members 5 6 public bool ChangeEventsSupported() 7 { 8 return false; 9 } 10 11 public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged; 12 13 public RfcConfigParameters GetParameters(string destinationName) 14 { 15 string example = ConfigurationSettings.AppSettings["destinationName"]; 16 17 if (example.Equals(destinationName)) 18 { 19 RfcConfigParameters parms = new RfcConfigParameters(); 20 parms.Add(RfcConfigParameters.AppServerHost, ConfigurationSettings.AppSettings["AppServerHost"]); //SAP IP 21 parms.Add(RfcConfigParameters.SystemNumber,ConfigurationSettings.AppSettings["SystemNumber"]); //SAP 实例 22 parms.Add(RfcConfigParameters.Client, ConfigurationSettings.AppSettings["Client"]);//要操作的Client 23 parms.Add(RfcConfigParameters.User, ConfigurationSettings.AppSettings["User"]); 24 parms.Add(RfcConfigParameters.Password,ConfigurationSettings.AppSettings["Password"]); 25 //parms.Add(RfcConfigParameters.Language,"ZH"); 26 //parms.Add(RfcConfigParameters.PoolSize, "5"); 27 //parms.Add(RfcConfigParameters.MaxPoolSize, "10"); 28 //parms.Add(RfcConfigParameters.IdleTimeout, "60"); 29 return parms; 30 } 31 return null; 32 } 33 #endregion 34 }
源码下载地址Sapnoc30Demo_Yan.rar