栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C# 如何调用SAP RFC

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 如何调用SAP RFC

结构:

安装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(); //获取指定返回结果

    }

    /// 
    /// RfcTable 转换为 DataTable
    /// 
    /// 
    /// 
    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;
    }

    /// 
    /// SAP RFC 连接信息设置
    /// 
    /// 
    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;
    } 
  }
}


 
  
   
    

以上就是C# 如何调用SAP RFC的详细内容,更多关于C# 调用SAP RFC的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/121576.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号