利用Sapnco 在.net/c# 中跨系统调用 SAP RFC 功能,执行SAP中的函数
Sapnco -C#中使用的核心组件,sap官网有下,本文使用的版本在 framework 平台使用,
sap 官网地址,但是下载要权限,全名称为:
SAP Connector for Microsoft .NET ,链接:https://support.sap.com/en/product/connectors/msnet.html?anchorId=section_512604546
核心功能其实就是用到两个dll:
sapnco.dll
sapnco_utils.dll
本文是在 .net 4.8 上测试使用
实现效果如下,输入料号,点button1, 即可通过该winform,远程调用sap函数,然后返回 料号的 描述到 winform 画面上
步骤如下:
1. SAP中se37建立一个函数:ZKAVEN_F001 ,注意勾上启用远程模式。
该函数作用就是输入一个料号,查询出该料号的描述,和得到一个类似mara 表的关于物料代码信息的内表
输入参数:
输出参数:
输出表:
代码:很简单,就是获取一个料号描述给到上面的输出参数,给一个内表到上面的参数表
先sap自行测试下:
可以得到一个描述 和一个内表, 共两个输出
以上是SAP 中函数建立过程,没啥问题,sap自行测试已通过。
2.打开visual studio 2022, 创建一个.NET Framework 项目
本文用的 .net 4.8
右击项目,添加引用,引用下载的 两个dll
程序中先 using SAP.Middleware.Connector;
winform 添加3个组件 textbox label button
完成代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using SAP.Middleware.Connector;namespace WindowsFormSAP_RFC1 {public partial class Form1 : Form{public Form1(){InitializeComponent();}public void nco(){RfcDestination prd = RfcDestinationManager.GetDestination("PRD_000");// RfcDestinationManager.UnregisterDestinationConfiguration(ID); //返注册 nco(prd);}public void nco(RfcDestination prd){RfcRepository repo = prd.Repository;IRfcFunction companyBapi = repo.CreateFunction("ZKAVEN_F001"); //调用函数名companyBapi.SetValue("L_MATNR", this.textBox1.Text.ToString()); //设置Import的参数companyBapi.Invoke(prd); //执行函数IRfcTable table = companyBapi.GetTable("IT_MARA"); //获取相应的品号内表string MAKTX = companyBapi.GetValue("L_MAKTX").ToString(); //获取品名this.label1.Text = MAKTX; //显示品名prd = null;repo = null;}//登陆SAP前的准备工作public class MyBackendConfig : IDestinationConfiguration{public RfcConfigParameters GetParameters(String destinationName){if ("PRD_000".Equals(destinationName)){RfcConfigParameters parms = new RfcConfigParameters();parms.Add(RfcConfigParameters.Name, "PRD_000");parms.Add(RfcConfigParameters.AppServerHost, "10.100.112.11"); //SAP主机IPparms.Add(RfcConfigParameters.SystemNumber, "10"); //SAP实例 parms.Add(RfcConfigParameters.User, "xxx"); //用户名parms.Add(RfcConfigParameters.Password, "xxxxx"); //密码parms.Add(RfcConfigParameters.Client, "710"); // Clientparms.Add(RfcConfigParameters.Language, "ZH"); //登陆语言parms.Add(RfcConfigParameters.PoolSize, "5");return parms;}else return null;}public bool ChangeEventsSupported(){return false;}public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;}private void button1_Click(object sender, EventArgs e){IDestinationConfiguration ID = new MyBackendConfig();RfcDestinationManager.RegisterDestinationConfiguration(ID);nco();}} }
另一边利用 SapNwRfc 实现同样功能,可参考 链接:https://www.cnblogs.com/cnishop/p/18729102