• 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(); //获取指定返回结果
    
            }
    
            /// <summary>
            /// RfcTable 转换为 DataTable
            /// </summary>
            /// <param name="myrfcTable"></param>
            /// <returns></returns>
            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;
            }
    
            /// <summary>
            /// SAP RFC 连接信息设置
            /// </summary>
            /// <returns></returns>
            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;
            } 
        }
    }
    <?xml version="1.0"?>
    <configuration>
      <configSections>
        <sectionGroup name="SAP.Middleware.Connector">
          <sectionGroup name="ClientSettings">
            <section name="DestinationConfiguration" type="SAP.Middleware.Connector.RfcDestinationConfiguration,sapnco"/>
          </sectionGroup>
        </sectionGroup>
      </configSections>
      <SAP.Middleware.Connector>
        <ClientSettings>
          <DestinationConfiguration>
            <destinations>
              <add NAME="dad" USER="ddac" PASSWD="qwe" CLIENT="110" SYSNR="00" ASHOST="192.168.0.111" LANG="ZH" GROUP="PUBLIC" MAX_POOL_SIZE="10" IDLE_TIMEOUT="600"></add>
            </destinations>
          </DestinationConfiguration>
        </ClientSettings>
      </SAP.Middleware.Connector>
    </configuration>
  • 相关阅读:
    极致平台开发技巧介绍1如何利用升级包,快速给客户升级
    如何用极致业务基础平台做一个通用企业ERP系列之三启用期间管理设计
    caca需要用到x11作为图形输出
    spring boot 使用 mybatis 开启事务回滚 的总结
    RabbitMQ --- 直连交换机 【 同步操作,等到消费者处理完后返回处理结果 】
    RabbitMQ --- 直连交换机 【 有回调方法,获取消费结果 】
    RabbitMQ --- 直连交换机 【 无回调方法,不能获取消费结果 】
    spring boot 启动警告 WARN 15684 --- [ restartedMain] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources. 解决
    Java基础复习到此结束,统一把源码放到GitHub仓库了,响应开源精神
    用一道题 来 复习 MySQL 的 复杂 sql 语句
  • 原文地址:https://www.cnblogs.com/Liyuting/p/14132187.html
Copyright © 2020-2023  润新知