C# 如何调用SAP RFC
作者:哈哈哈嗝 时间:2023-07-02 18:11:42
结构:
安装NuGet包:
using SAP.Middleware.Connector;
using System.Data;
namespace DFDN.SDK.ServiceInterface
{
public class RfcDemo
{
public void Loading()
{
RfcDestination SapRfcDestination = RfcDestinationManager.GetDestination(GetParameters()); //连接字符串
RfcDestination SapRfcDestinationForConfig = RfcDestinationManager.GetDestination("dad"); //连接字符串 从App.config配置
RfcRepository SapRfcRepository = SapRfcDestination.Repository;
DataTable dt = new DataTable(); //数据源
dt.Columns.Add("DATA1", typeof(string));
dt.Columns.Add("DATA2", typeof(string));
dt.Columns.Add("DATA3", typeof(string));
IRfcFunction func = SapRfcRepository.CreateFunction("Z_RFC_XXXX");
IRfcTable tSAP = func.GetTable("INPUT_TABLE");
for (int i = 0; i < dt.Rows.Count; i++)
{
string str1 = dt.Rows[i][0].ToString();
string str2 = dt.Rows[i][1].ToString();
string str3 = dt.Rows[i][2].ToString();
IRfcStructure struSAP = tSAP.Metadata.LineType.CreateStructure();
struSAP.SetValue("str1", str1);
struSAP.SetValue("str2", str2);
struSAP.SetValue("str3", str3);
tSAP.Append(struSAP);
}
func.SetValue("INPUT_TABLE", tSAP); //table 参数
func.SetValue("WERKS", "A"); //单个参数
func.SetValue("STATUS", "B"); //单个参数
func.Invoke(SapRfcDestination); //调用
IRfcTable SAPDataTable = func.GetTable("RETURN_TABLE"); //获取表格结果
DataTable table = ToDataTable(SAPDataTable); //转换DataTable
string result = func.GetValue("X").ToString(); //获取指定返回结果
}
/// <summary>
/// RfcTable 转换为 DataTable
/// </summary>
/// <param name="myrfcTable"></param>
/// <returns></returns>
public DataTable ToDataTable(IRfcTable myrfcTable)
{
DataTable loTable = new DataTable();
int liElement;
for (liElement = 0; liElement <= myrfcTable.ElementCount - 1; liElement++)
{
RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement);
loTable.Columns.Add(metadata.Name);
}
foreach (IRfcStructure Row in myrfcTable)
{
DataRow ldr = loTable.NewRow();
for (liElement = 0; liElement <= myrfcTable.ElementCount - 1; liElement++)
{
RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement);
ldr[metadata.Name] = Row.GetString(metadata.Name);
}
loTable.Rows.Add(ldr);
}
return loTable;
}
/// <summary>
/// SAP RFC 连接信息设置
/// </summary>
/// <returns></returns>
public RfcConfigParameters GetParameters()
{
RfcConfigParameters parms = new RfcConfigParameters();
parms.Add(RfcConfigParameters.AppServerHost, "192.168.0.114"); //SAP主机IP
parms.Add(RfcConfigParameters.SystemNumber, "01"); //SAP实例
parms.Add(RfcConfigParameters.User, "RFC_EDI"); //用户名
parms.Add(RfcConfigParameters.Password, "init1"); //密码
parms.Add(RfcConfigParameters.Client, "300"); // Client
parms.Add(RfcConfigParameters.Language, "ZH"); //登陆语言
parms.Add(RfcConfigParameters.PoolSize, "10");
parms.Add(RfcConfigParameters.IdleTimeout, "600");
parms.Add(RfcConfigParameters.Name, "DAP");
return parms;
}
}
}
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="SAP.Middleware.Connector">
<sectionGroup name="ClientSettings">
<section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration,sapnco"/>
</sectionGroup>
</sectionGroup>
</configSections>
<SAP.Middleware.Connector>
<ClientSettings>
<DestinationConfiguration>
<destinations>
<add NAME="dad" USER="ddac" PASSWD="qwe" CLIENT="110" SYSNR="00" ASHOST="192.168.0.111" LANG="ZH" GROUP="PUBLIC" MAX_POOL_SIZE="10" IDLE_TIMEOUT="600"></add>
</destinations>
</DestinationConfiguration>
</ClientSettings>
</SAP.Middleware.Connector>
</configuration>
来源:https://www.cnblogs.com/Liyuting/p/14132187.html
标签:c#,sap,rfc
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java开发环境配置方法
2022-05-25 02:02:29
![](https://img.aspxhome.com/file/2023/0/69340_0s.jpg)
C#使用linq对数组进行筛选排序的方法
2023-12-06 06:21:21
Java Calendar类使用案例详解
2023-07-09 14:03:22
Android控件PopupWindow模仿ios底部弹窗
2023-03-09 20:42:10
![](https://img.aspxhome.com/file/2023/0/137990_0s.gif)
学习Java之如何正确地跳出循环结构
2021-10-21 11:21:53
![](https://img.aspxhome.com/file/2023/4/78244_0s.webp)
Java常用函数式接口总结
2022-02-02 00:02:50
![](https://img.aspxhome.com/file/2023/8/68178_0s.png)
mybatis注解与xml常用语句汇总
2022-05-17 18:39:47
c++ 函数指针相关总结
2022-09-26 04:42:33
详解SpringCloud mysql实现配置中心
2023-11-02 05:12:57
![](https://img.aspxhome.com/file/2023/8/58718_0s.png)
mybatis-plus中lambdaQuery()与lambdaUpdate()比较常见的使用方法总结
2023-11-24 22:43:04
![](https://img.aspxhome.com/file/2023/6/60156_0s.jpg)
springcloud中RabbitMQ死信队列与延迟交换机实现方法
2023-05-04 04:25:54
简单了解JAVA public class与class区别
2023-11-15 23:59:26
![](https://img.aspxhome.com/file/2023/5/59675_0s.png)
Java实现五子棋游戏
2022-07-08 12:50:27
Android为View添加拖放效果的方法实例
2023-06-25 18:53:45
Android进阶从字节码插桩技术了解美团热修复实例详解
2022-05-27 18:06:07
![](https://img.aspxhome.com/file/2023/4/137174_0s.png)
java设计模式学习之装饰模式
2021-10-02 04:22:54
![](https://img.aspxhome.com/file/2023/7/121937_0s.jpg)
基于@RequestBody和@ResponseBody及Stringify()的作用说明
2021-12-14 01:47:38
![](https://img.aspxhome.com/file/2023/6/69106_0s.png)
Android安卓中循环录像并检测内存卡容量
2021-06-21 13:12:55
Java实现对两个List快速去重并排序操作示例
2023-05-02 03:40:14
![](https://img.aspxhome.com/file/2023/7/71937_0s.png)
Java中synchronized关键字修饰方法同步的用法详解
2022-03-05 07:11:22