C# .NET 如何调用 SAP RFC 接口

news2024/9/20 12:41:32

1.分析传参结构
SAP 传参格式对应 .NET 参数格式

SAP 参数.NET 参数参数类型
import(导入)——关联类型为数据元素Param单个变量参数
import(导出)——关联类型为结构体Struct结构体
tableTable

下面是 SAP 对应参数类型:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.web.config 配置
配置文件需要客户端名和连接地址和账户密码

  <appSettings>
    <add key="SAPINSName" value="OND"/>    //客户端名字
  </appSettings>
 <SAP.Middleware.Connector>
    <ClientSettings>
      <DestinationConfiguration>
        <destinations>
         <add NAME="OND" USER="" PASSWD="******" CLIENT="300" LANG="ZH" ASHOST="10.10.xx.xx" SYSNR="00" MAX_POOL_SIZE="10" IDLE_TIMEOUT="10"/>
        </destinations>
      </DestinationConfiguration>
    </ClientSettings>
  </SAP.Middleware.Connector>

3.调用示例:
调用 SAP 接口类型为输入输出表的接口:

var SAP = ConfigHelper.AppSettings("SAPINSName");
Test.SapDBBase.SAPBaseDAL sd = new Test.SapDBBase.SAPBaseDAL(SAP); //基本设置
DataTable tb = new DataTable();
tb.Columns.Add("WERKS");
tb.Columns.Add("MATNR");
DataRow rowsap = tb.NewRow();
rowsap["WERKS"] = WERK;
rowsap["MATNR"] = PN;
tb.Rows.Add(rowsap);
try
{
	//调用 SAP RFC 接口,传参是表结构,返回表数据和结构体
    DataTable returntd = sd.WriteDataTable("ZFM_EXPORT", "IT_IN", tb, "IT_OUT");
}
catch (Exception e)
{
    Type = "E";
    Msg = e.Message;
}      

4.调用方法部分代码

  • 调用准备
using System;
using System.Collections;
using System.Data;
using System.Linq;
using ONET.ServerCommLib;
using SAP.Middleware.Connector;

		public SAPBaseDAL(string SapConfigNodeName = "")
		{
			if (!string.IsNullOrEmpty(SapConfigNodeName))
			{
				SapRFCBase.SapConfigNodeName = SapConfigNodeName;
			}
		}


try
{
	RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
	RfcRepository repository = rfcDestination.Repository;
	IRfcFunction rfcFunction = repository.CreateFunction(functionName);
	...//不同入参类型
	rfcFunction.Invoke(rfcDestination);
	...//不同出参类型
}
catch (Exception ex)
{
	this.writeExceptionLog(functionName, ex);
}
  • 出入参构造
//入参结构体
rfcFunction = this.GetIRfcStructeFromDataTable(rfcFunction, writeSapStructName, writeStructTable);
//入参表
rfcFunction = this.GetIRfcTableFromDataTable(rfcFunction, writeSapTableName, writeTable);
rfcFunction = writeDataSet.Tables.Cast<DataTable>().Aggregate(rfcFunction, (IRfcFunction current, DataTable table) => this.GetIRfcTableFromDataTable(current, table.TableName, table));//多表
//入参单个变量
                if (htParam != null && htParam.Count > 0)
                {
                    foreach (object obj in htParam.Keys)
                    {
                        string text = (string)obj;
                        rfcFunction.SetValue(text, htParam[text].ToString());
                    }
                }
//出参结构
方式1:
				if (!string.IsNullOrEmpty(returnStructName))
				{
					IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
					return new SAPRetStruct
					{
						Type = structure.GetString("TYPE"),
						Msg = structure.GetString("MESSAGE")
					};
				}
				return default(SAPRetStruct);
方式2:
                //ref Hashtable hstb 返回hashtable
				IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
				if (hstb != null && hstb.Count > 0)
				{
					ArrayList arrayList = new ArrayList(hstb.Keys);
					for (int i = 0; i < arrayList.Count; i++)
					{
						hstb[arrayList[i]] = structure.GetString(arrayList[i].ToString());
					}
				}
//出参单个变量参数  
                Hashtable hashtable = new Hashtable();
                if (outParam != null && outParam.Count > 0)
                {
                    foreach (object obj2 in outParam.Keys)
                    {
                        string text2 = (string)obj2;
                        hashtable.Add(text2, rfcFunction.GetString(text2));
                    }
                    if (hashtable.Count > 0)
                    {
                        outParam.Clear();
                        foreach (object obj3 in hashtable.Keys)
                        {
                            string text2 = (string)obj3;
                            outParam.Add(text2, hashtable[text2]);
                        }
                    }
                }    
//出参表
				IRfcTable table = rfcFunction.GetTable(returnTableName);//出参表名
				dtOut = this.GetDataTableFromRFCTable(table); //返回的表         
  • 实例1:入参:变量+表 出参:变量+结构体
public SAPRetStruct WriteParamAndTableToSapReturnParamAndRetStruct(string functionName, Hashtable htParam, string writeSapTableName, DataTable writeTable, ref Hashtable outParam, string returnStructName)
		{
			try
			{
				RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
				RfcRepository repository = rfcDestination.Repository;
				IRfcFunction rfcFunction = repository.CreateFunction(functionName);
				rfcFunction = this.GetIRfcTableFromDataTable(rfcFunction, writeSapTableName, writeTable);
				if (htParam != null && htParam.Count > 0)
				{
					foreach (object obj in htParam.Keys)
					{
						string text = (string)obj;
						rfcFunction.SetValue(text, htParam[text].ToString());
					}
				}
				rfcFunction.Invoke(rfcDestination);
				Hashtable hashtable = new Hashtable();
				if (outParam != null && outParam.Count > 0)
				{
					foreach (object obj2 in outParam.Keys)
					{
						string text2 = (string)obj2;
						hashtable.Add(text2, rfcFunction.GetString(text2));
					}
					if (hashtable.Count > 0)
					{
						outParam.Clear();
						foreach (object obj3 in hashtable.Keys)
						{
							string text2 = (string)obj3;
							outParam.Add(text2, hashtable[text2]);
						}
					}
				}
				IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
				return new SAPRetStruct
				{
					Type = structure.GetString("TYPE"),
					Msg = structure.GetString("MESSAGE")
				};
			}
			catch (Exception ex)
			{
				this.writeExceptionLog(functionName, ex);
			}
			return default(SAPRetStruct);
		}
  • 实例2:入参:多个表 出参:变量+结构体
		public SAPRetStruct WriteTablesToSapReturnParamAndRetStruct(string functionName, DataSet writeDataSet, ref Hashtable outParam, string returnStructName)
		{
			try
			{
				RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
				RfcRepository repository = rfcDestination.Repository;
				IRfcFunction rfcFunction = repository.CreateFunction(functionName);
				rfcFunction = writeDataSet.Tables.Cast<DataTable>().Aggregate(rfcFunction, (IRfcFunction current, DataTable table) => this.GetIRfcTableFromDataTable(current, table.TableName, table));
				rfcFunction.Invoke(rfcDestination);
				Hashtable hashtable = new Hashtable();
				if (outParam != null && outParam.Count > 0)
				{
					foreach (object obj in outParam.Keys)
					{
						string text = (string)obj;
						hashtable.Add(text, rfcFunction.GetString(text));
					}
					if (hashtable.Count > 0)
					{
						outParam.Clear();
						foreach (object obj2 in hashtable.Keys)
						{
							string text = (string)obj2;
							outParam.Add(text, hashtable[text]);
						}
					}
				}
				IRfcStructure structure = rfcFunction.GetStructure(returnStructName);
				return new SAPRetStruct
				{
					Type = structure.GetString("TYPE"),
					Msg = structure.GetString("MESSAGE")
				};
			}
			catch (Exception ex)
			{
				this.writeExceptionLog(functionName, ex);
			}
			return default(SAPRetStruct);
		}
  • 实例3:入参:变量 出参:表
		public DataTable GetTable(string functionName, Hashtable htParam, string returnTableName)
		{
			try
			{
				RfcDestination rfcDestination = SapRFCBase.RegisterDestination();
				RfcRepository repository = rfcDestination.Repository;
				IRfcFunction rfcFunction = repository.CreateFunction(functionName);
				if (htParam != null && htParam.Count > 0)
				{
					foreach (object obj in htParam.Keys)
					{
						string text = (string)obj;
						rfcFunction.SetValue(text, htParam[text].ToString());
					}
				}
				rfcFunction.Invoke(rfcDestination);
				IRfcTable table = rfcFunction.GetTable(returnTableName);
				return this.GetDataTableFromRFCTable(table);
			}
			catch (Exception ex)
			{
				this.writeExceptionLog(functionName, ex);
			}
			return null;
		}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/729786.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Python学习笔记(十六)————异常相关

目录 &#xff08;1&#xff09;异常概念 &#xff08;2&#xff09;异常的捕获 ①异常捕获的原因 ②捕获常规异常 ③捕获指定异常 ④捕获多个异常 ⑤ 捕获异常并输出描述信息 ⑥捕获所有异常 ⑦异常else ⑧异常的finally &#xff08;3&#xff09;异常的传递 &#xff08…

自动化测试 selenium 篇

✏️作者&#xff1a;银河罐头 &#x1f4cb;系列专栏&#xff1a;JavaEE &#x1f332;“种一棵树最好的时间是十年前&#xff0c;其次是现在” 目录 什么是自动化测试&#xff1f;Selenium 介绍Selenium 是什么Selenium 特点工作原理 seleniumJava环境搭建ChromeJava1.下载ch…

uni-app:删除默认title

去除前&#xff1a; 可以看到有两个title 去除后&#xff1a; 可以看出就只有手机顶部的title了 "navigationStyle": "custom",//删除默认title

堆的向上与向下调整

目录 一、堆 1、概念 2、性质 二、向上调整 三、向下调整 四、建堆的比较 1.向上调整建堆 2.向下调整建堆 3.比较 五、总结 一、堆 1、概念 如果有一个关键码的集合K {k0k1&#xff0c;k2&#xff0c;…kn-1}&#xff0c;把它的所有元素按完全二叉树的顺序存储方式存…

怎么学习PHP错误处理和调试? - 易智编译EaseEditing

学习PHP错误处理和调试技术可以通过以下步骤&#xff1a; 理解错误类型&#xff1a; 了解PHP中常见的错误类型&#xff0c;如语法错误、运行时错误和逻辑错误等。学习它们的特点和常见原因&#xff0c;以便更好地定位和解决问题。 错误报告设置&#xff1a; 在开发环境中&am…

【Java基础学习打卡14】Java注释

目录 前言一、什么是注释二、注释的重要性三、单行、多行注释1.单行注释2.多行注释 四、文档注释1.文档注释2.JDK官网文档3.javadoc生成文档 五、注释建议总结 前言 本文介绍Java注释&#xff0c;它是我们在Java编程中必不可少的。Java注释有单行注释、多行注释和文档注释。对…

BUUCTF Web CyberPunk WriteUp

想直接查看payload的点这里 前言 二次注入&#xff08;Second-Order Injection&#xff09;是指攻击者在应用程序中注入恶意数据&#xff0c;然后在稍后的操作或不同的上下文中再次使用该恶意数据&#xff0c;导致安全漏洞。它通常发生在数据库查询、数据导出、报告生成等过程…

基于springboot博客论坛系统设计与实现(源码+文档LW+数据库+报告)

HBLOG 是一个博客论坛网站&#xff0c;分为游客端和管理员端。游客端主要有注册登录&#xff0c;查看文章&#xff0c;发表撰写文章&#xff0c;管理自己的文章&#xff0c;评论文章等功能&#xff1b;而管理员端主要有登录&#xff0c;查看近期数据&#xff0c;查看日志&#…

如何做好迭代回顾 2/4

事故根因分析&#xff1a;上集 北京某软件开发公司&#xff0c;专门为电信供应商做定制软件开发&#xff0c;比如发短信做些推广活动等。公司希望做过程改进&#xff0c;我首次与公司老板访谈&#xff1a; 我&#xff1a;过程改进主要帮助管理层更好达到公司业务目标。你们自己…

365天深度学习训练营-第T5周:运动鞋品牌识别

&#x1f368; 本文为&#x1f517;365天深度学习训练营 中的学习记录博客&#x1f356; 原作者&#xff1a;K同学啊 我的环境&#xff1a; 语言环境&#xff1a;Python3.10.7编译器&#xff1a;VScode深度学习环境&#xff1a;TensorFlow2 一、前期工作&#xff1a; 1、导入…

【活动】如何在工作中管理情绪

写在前面 近期发生的新闻热点再度引发公众对稳定情绪和心理健康的关注。有时候我们遇到的最大的敌人&#xff0c;不是运气也不是能力&#xff0c;而是失控的情绪和口无遮拦的自己。如何在工作中保持稳定的情绪&#xff1f;谈谈我的看法。 愤怒的危害 说到愤怒这种情绪&#xf…

基于深度学习的高精度鸟类目标检测识别系统(PyTorch+Pyside6+YOLOv5模型)

摘要&#xff1a;基于深度学习的高精度鸟类目标&#xff08;鹦鹉&#xff08;Crested Myna&#xff09;、麻雀&#xff08;Eurasian Tree Sparrow&#xff09;、黑头文鸟&#xff08;Chestnut Munia&#xff09;、白领翡翠&#xff08;Collared Kingfisher&#xff09;、太阳鸟…

【C语言】进阶指针(二)—>函数指针与回调函数

目录 前言&#xff1a; 一、函数指针 代码1分析&#xff1a; 代码2分析&#xff1a; 二、函数指针数组 三、指向函数指针数组的指针 四、回调函数&#xff08;模拟实现库函数qsort&#xff09; &#xff08;一&#xff09;void*类型指针的作用 &#xff08;二&#xf…

Spark—Shell命令对WordCount案例的基本操作(统计、去重、排序、求平均值及join)

一、统计、去重 1、案例数据介绍 WordCount统计&#xff1a;某电商网站记录了大量的用户对商品的收藏数据&#xff0c;并将数据存储在名为buyer_favorite的文本文件中。文本数据格式如下&#xff1a; 2、启动spark-shell 配置好spark环境&#xff0c;若还没有环境可以参考…

windows下环境问题总结

nacos 启动后在spring 项目中无法加载yml配置文件 spring.datasource.platform mysql 注意一定要放开这行&#xff0c;不放的话&#xff0c;可能会导致服务可以成功注册&#xff0c;但是&#xff0c;我们无法使用局部的 nacos里yml配置文件的属性

Linux:项目自动化构建工具——make/Makefile

文章目录 一.make与Makefile的关系1.Makefile2.make 二.项目清理1.clean2. .PHONY 前言&#xff1a; 本章主要内容有认识与学习Linux环境下如何使用项目自动化构建工具——make/makefile。 一.make与Makefile的关系 当我们编写一个较大的软件项目时&#xff0c;通常需要将多个…

js实现图片压缩

创建一个type"file"的input标签&#xff0c;用于文件上传。 <input type"file" name"" id"upload" value"" />通过js实现图片压缩 window.onload function () {const upload document.getElementById("upload…

9.10UEC++生成、销毁actor

BeginPlay&#xff1a; 1.SpawnActor&#xff1a;<模板类>&#xff08;模板::staticclass&#xff08;&#xff09;&#xff0c;FVector const class&#xff0c;FRotation const class&#xff09; 生成一个actor 2.Destory&#xff08;&#xff09;从世界中销毁一个a…

SSM学习笔记-------Spring(一)

SSM学习笔记-------Spring&#xff08;一&#xff09; Spring_day011、课程介绍1.1 为什么要学?1.2 学什么?1.3 怎么学? 2、Spring相关概念2.1 初识Spring2.1.1 Spring家族2.1.2 了解Spring发展史 2.2 Spring系统架构2.2.1 系统架构图2.2.2 课程学习路线 2.3 Spring核心概念…

【zabbix 代理服务器】

目录 一、部署 zabbix 代理服务器1、设置 zabbix 的下载源&#xff0c;安装 zabbix-proxy2、初始化数据库1、创建数据库并指定字符集2、创建 zabbix 数据库用户并授权 3、导入数据库信息4、修改 zabbix-proxy 配置文件5、启动 zabbix-proxy6、在所有主机上配置 hosts 解析7、在…