准备工作:安装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>
配置文件

相关文章: