C#调用SAP的RFC接口那点事


在说怎么用C#调用RFC接口之前,先讲讲自己对SAP接口的理解。

1,SAP的常用接口

PI接口:即我们常用的webservice

RFC接口:即是这里要讲的一种接口方式,至于定义嘛,有请百度百科

 

2,RFC接口的参数

input类型:即键值对,可以理解成webapi的key-value那样的参数。

table类型:即对象数组,可以理解成List<object>对象,并且可以传多个这样的对象。

使用过程中,上面两种类型可以同时传,也只用其中一种类型,看接口需求而定。

3,RFC接口的返回值

跟传参类似,返回值里面可以是key-value类型,也可以是多个List<object>。

 

4,C#怎么调用RFC函数

很简单,使用现成的类库。

网上可以找到很多版本的类库,每个都有一定的差异,我使用的是nuget上下载的。nuget上搜NSAPConnector_x64就可以了。

(我使用的这个版本有一些小问题,但比起别的版本来说算好的了,当然可能有别的完善版本我没发现)

下好后,会有下面3个dll引用

 

因为没有文档,建议大家直接用对象浏览器,直接看dll暴露出来的接口。根据方法名就可以大概猜出的知道每个方法的用途。

(这里可以用NSAPConnector封装的方法,如果觉得封装的方法不好用,也可以直接调用sapnco里面的方法,我是直接使用sapnco的方法的,因为那个好像有BUG)

 

DLL里面的接口方法就不截图出来了,有需要的自己去看看,也可以反编译去看源码。

说了这么多,粘一点调用的示例出来吧:

 1 public DataSet ExecuteDataSet(string cmdText, Dictionary<string, string> dic, Dictionary<string, List<string>> dic2 = null) 
 2 { 
 3             DataSet ds = new DataSet();//存放结果
 4             RfcDestination des = GetRfcDestination();//创建连接
 5             IRfcFunction fun = des.Repository.CreateFunction(cmdText);//传入调用函数名
 6        //input类型参数
 7             if (dic != null)
 8                 foreach (var item in dic)
 9                     fun.SetValue(item.Key, item.Value);
10 
11             //table类型,表结构参数
12             if (dic2 != null)
13                 foreach (var item in dic2)
14                 {
15                     IRfcTable table = fun.GetTable(item.Key);
16                     foreach (var itemval in item.Value)
17                     {
18                         table.Insert();
19                         Dictionary<string, string> dicobj = itemval.ToObject<Dictionary<string, string>>();
20                         foreach (var obj in dicobj)
21                         {
22                             table.CurrentRow.SetValue(obj.Key, obj.Value);
23                         }
24                     }
25                 }
26 
27             fun.Invoke(des);//调用
28 
29         //处理返回结果
30             for (int i = 0; i < fun.Count; i++)
31             {
32                 //IRfcTable转datatable
33                 try
34                 {
35                     object RfcResult = fun[i].GetObject();
36                     if (RfcResult == null || RfcResult.GetType().Name == "String")
37                     {
38                         RfcTableToDataTable(fun[i].GetValue().ToString(), fun[i].Metadata.Name, ds);
39                     }
40                     else if (RfcResult.GetType().Name == "RfcTable")
41                     {
42                         IRfcTable rfctable = fun[i].GetTable();
43                         ds.Tables.Add(RfcTableToDataTable(rfctable, fun[i].Metadata.Name));
44                     }
45                     else if (RfcResult.GetType().Name == "RfcStructure")
46                     {
47                         IRfcStructure rfctable = fun[i].GetStructure();
48                         foreach (var item in rfctable)
49                         {
50                             RfcTableToDataTable(item.GetString(), item.Metadata.Name, ds);
51                         }
52                     }
53                 }
54                 catch (Exception ex)
55                 {
56                     continue;
57                 }
58 
59             }
60             return ds;
61         }

最后,还有一点要提醒的是,sapnco.dll要运行在Microsoft Visual C++ 2010 Redistributable Package (x64)以上版本的环境下,就是说要去下载安装才能跑起来!!

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM