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

10 篇文章 0 订阅
文章详细介绍了如何在.NET环境中配置SAP连接参数,包括在web.config中的客户端设置,以及调用SAP接口时传入和传出参数的处理,如单个变量、结构体和表类型。提供了输入输出表类型的接口调用示例,并展示了不同出入参类型的构造方法。此外,还包含了处理异常的代码片段。
摘要由CSDN通过智能技术生成

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;
		}
.NET 4.0 是微软公司开发的一种应用程序框架。它提供了开发和运行多种类型应用程序的环境,包括 Web 应用程序、服务、Windows 应用程序等。它是微软所提供的一种全面的开发平台,并提供了丰富的类库和工具,支持多种编程语言,如C#、VB.NET等。.NET 4.0 可以帮助开发人员快速、高效地开发应用程序,并提供了良好的可扩展性和性能。 SAP RFCSAP(系统、应用与产品)公司提供的一种远程功能调用接口(Remote Function Call)。它允许外部系统通过远程方式与SAP应用程序进行通信和交互。通过SAP RFC,外部系统可以调用SAP应用程序中的功能模块,并传输相关的数据。这样,外部系统可以与SAP系统集成,实现数据的共享和交换。同时,SAP RFC也可以用于在SAP系统内部进行模块之间的通信。 .NET 4.0 和 SAP RFC 可以结合使用,以实现.NET应用程序与SAP系统之间的集成。通过.NET 4.0所提供的丰富功能和灵活性,可以使用.NET编程语言(如C#)开发用于与SAP系统进行通信和交互的应用程序。同时,通过使用SAP RFC,可以调用SAP系统中的功能,并在.NET应用程序中处理相关的数据。这样,可以实现.NET应用程序与SAP系统之间的数据共享和交换,实现系统间的无缝集成。 总之,.NET 4.0 是一种开发框架,用于开发各种类型的应用程序,而SAP RFC 则是用于实现与SAP系统的远程通信的接口。两者可以结合使用,实现.NET应用程序和SAP系统之间的数据共享和交互。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值