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

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.mfbz.cn/a/35229.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

win10安装pytorch GPU

我记得以前安装过深度学习库GPU版本&#xff0c; 需要安装cuda什么的&#xff0c;翻了下还真写过一篇win10安装tensorflow的文章&#xff0c;但是流程不止不详细&#xff0c;还不清晰。这次就再记录一遍 这次安装的是pytorch&#xff0c;这么多年似乎pytorch要逐渐统一深度学习…

【算法与数据结构】232、LeetCode用栈实现队列

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;这道题要求我们用栈模拟队列&#xff08;工作上一定没人这么搞&#xff09;。程序当中&#xff0c;pus…

spring之BeanFactory

spring之BeanFactory DefaultListableBeanFactory示例代码类继承实现结构 BeanFactory是Bean工厂&#xff0c;所以很明显&#xff0c;BeanFactory会负责创建Bean&#xff0c;并且提供获取Bean的API。 DefaultListableBeanFactory 在Spring源码中&#xff0c;BeanFactory接口存…

自定义的车牌号键盘组件

<template><view class"keyboard-wrap" v-if"kbShow"><view class"head"><view class"done" tap"done"><text class"iconfont iconxiala-"></text>关闭</view></vi…

spring boot + Apache tika 实现文档内容解析

Apache tika是Apache开源的一个文档解析工具。Apache Tika可以解析和提取一千多种不同的文件类型(如PPT、XLS和PDF)的内容和格式&#xff0c;并且Apache Tika提供了多种使用方式&#xff0c;既可以使用图形化操作页面&#xff08;tika-app&#xff09;&#xff0c;又可以独立部…

Python实现微信发送文件实例

新建Python文件&#xff1a;wx_file.py&#xff0c;代码如下 # -*- coding: utf-8 -*- # Author : CxiuM # Time : 2023-07-06 10:12 # Name : wx_operation.py"""微信群发消息"""import os import time import subprocessimport requests …

【ElasticSearch】DSL查询语法

文章目录 1、DSL查询分类2、DSL基本语法3、全文检索查询4、精确查询5、地理查询6、复合查询--相关性打分算法7、复合查询之Function Score Query8、复合查询之BooleanQuery 1、DSL查询分类 Elasticsearch提供了基于JSON的DSL&#xff08;Domain Specific Language&#xff09;…

Java版本工程项目管理系统源码

Java版工程项目管理系统 Spring CloudSpring BootMybatisVueElementUI前后端分离 功能清单如下&#xff1a; 首页 工作台&#xff1a;待办工作、消息通知、预警信息&#xff0c;点击可进入相应的列表 项目进度图表&#xff1a;选择&#xff08;总体或单个&#xff09;项目显示…

Librosa库——语音识别,语音音色识别训练及应用

很多同学以为语音识别是非常难的&#xff0c;其实并不然&#xff0c;起初我也是这么认为&#xff0c;但后来发现语音识别是最简单的&#xff0c;因为同学们可能不知道Python有一个音频处理库Librosa&#xff0c;这个库非常的强大&#xff0c;可以进行音频处理、频谱表示、幅度转…

CSS(持续更新!~)

二&#xff1a; 进阶&#xff1a; 只打算起到装饰作用的图片就建议就背景图片 块级标签就是&#xff1a;独占一行的标签&#xff08;比如div&#xff09;并且可以加宽加高 行内元素&#xff1a;就是不会独占一行的标签&#xff08;比如a&#xff0c;span等等&#xff0c;不可以…

TensorFlow项目练手(二)——猫狗熊猫的分类任务

项目介绍 通过猫狗熊猫图片来对图片进行识别&#xff0c;分类出猫狗熊猫的概率&#xff0c;文章会分成两部分&#xff0c;从基础网络模型->利用卷积网络经典模型Vgg。 基础网络模型 基础的网络模型主要是用全连接层来分类&#xff0c;比较经典的方法&#xff0c;也是祖先…

MinGW编译OpenCV 过程记录

1.下载源码opencv-3.4.10.zip &#xff0c;可以在OpenCV官网下载Releases - OpenCV 解压缩如下: 2.下载Mingw64工具&#xff0c;需要支持posix 并设置系统环境目录&#xff0c;下载的文件名x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0.7z (可以在网上找) 3.使用Cmake工具构建…

微信小程序个人中心展示样式(2)

这是之前的详细的看这里 因为这是好多年前写的了&#xff0c;好多人私信我代码有问题。正好今天有时间简单的还原下代码 话不多说先看图(图片样式自己搞奥~~~~我也好久没弄了这就是个参考demo) 以下是一个使用微信小程序开发的个人中心展示详情的示例&#xff1a; 在微信开发…

基于PyQt5的桌面图像调试仿真平台开发(10)色彩矩阵

系列文章目录 基于PyQt5的桌面图像调试仿真平台开发(1)环境搭建 基于PyQt5的桌面图像调试仿真平台开发(2)UI设计和控件绑定 基于PyQt5的桌面图像调试仿真平台开发(3)黑电平处理 基于PyQt5的桌面图像调试仿真平台开发(4)白平衡处理 基于PyQt5的桌面图像调试仿真平台开发(5)…

解决问题:通配符的匹配很全面, 但无法找到元素 ‘context:component-scan‘ 的声明~

异常描述如下&#xff1a; 产生异常原因&#xff1a; 因为在配置文件中没有找到<context:component-scan />元素的声明&#xff0c;解决办法&#xff1a;将XML配置文件中的声明改为下述代码&#xff1a; <beans xmlns"http://www.springframework.org/schema/b…

01 | 一条 SQL 查询语句是如何执行的?

以下内容出自 《MySQL 实战 45 讲》 一条 SQL 查询语句是如何执行的&#xff1f; 下面是 MySQL 的基本架构示意图&#xff0c;从中可以清楚地看到 SQL 语句在 MySQL 的各个功能模块中的执行过程。 大体来说&#xff0c;MySQL 可以分为 Server 层和存储引擎层两部分。 Server …

Python如何批量将图片以超链接的形式插入Excel

【研发背景】 在日常办公中&#xff0c;我们经常需要将图片插入进Excel中&#xff0c;但是如果插入的图片太多的话&#xff0c;就会导致Excel的文件内存越来越大&#xff0c;但是如果我直插入图片的路径&#xff0c;或者只是更改某一列的数据设置为超链接&#xff0c;这样的话&…

Spring底层核心架构

Spring底层核心架构 相关的配置类 1. user类 package com.zhouyu.service;import org.springframework.stereotype.Component;public class User { }2. AppConfig类 package com.zhouyu;import org.springframework.context.annotation.*; import org.springframework.sched…

open*w*r*t +dnspod ddns动态解析ipv6 远程控制移动内网路由器

1.修改openw*r*t web https管理端口为8443 修改ipv6 https 监听端口list listen_https [::]:8443 cd /etc/config/vi uhttpdvi /etc/config/uhttpdconfig uhttpd mainlist listen_http 0.0.0.0:80list listen_http [::]:80list listen_https 0.0.0.0:443list listen_https [:…

前端Vue一款基于canvas的精美商品海报生成组件 根据个性化数据生成商品海报图 长按保存图片

前端Vue一款基于canvas的精美商品海报生成组件 根据个性化数据生成商品海报图 长按保存图片&#xff0c;下载完整代码请访问uni-app插件市场地址&#xff1a;https://ext.dcloud.net.cn/plugin?id13326 效果图如下: # cc-beautyPoster #### 使用方法 使用方法 <!-- pos…
最新文章