.NET利用RFC连接SAP,查询、读取SAP数据

为黄朴整理!!!!!!!!!!!!!!!!!

在NuGet 添加 sapnco

.NET利用RFC连接SAP,查询、读取SAP数据_第1张图片

一个简单的SAPCommand,方法 GetDataTableFromRFCTable 复制于 https://www.cnblogs.com/jamin/p/3374139.html

using System;
using System.Collections.Generic;
using System.Text;
using SAP.Middleware.Connector;
using System.Data;
using System.Linq;
namespace Blog.Core.Business.RFC
{
    /// 
    /// SAPCommand
    /// 
    public static class SAPCommand
    {
        /// 
        /// 获取连接SAP参数
        /// 
        /// 
        private static RfcConfigParameters GetRfcConfigParameters()
        {
            RfcConfigParameters pairs = new RfcConfigParameters();
            pairs.Add(RfcConfigParameters.Name, "SAP连接名");
            pairs.Add(RfcConfigParameters.AppServerHost, "SAP服务器地址");
            pairs.Add(RfcConfigParameters.SystemNumber, "00");
            pairs.Add(RfcConfigParameters.SystemID, "D01");
            pairs.Add(RfcConfigParameters.User, "SAP账号");
            pairs.Add(RfcConfigParameters.Password, "SAP密码");
            pairs.Add(RfcConfigParameters.Client, "客户端");
            pairs.Add(RfcConfigParameters.Language, "en");
            pairs.Add(RfcConfigParameters.PoolSize, "5");
            pairs.Add(RfcConfigParameters.MaxPoolSize, "10");
            pairs.Add(RfcConfigParameters.IdleTimeout, "30");
            return pairs;
        }

        /// 
        /// 获取SAP连接
        /// 
        /// 
        private static RfcDestination GetRfcDestination()
        {
            RfcDestination destination = RfcDestinationManager.GetDestination(GetRfcConfigParameters());
            return destination;
        }

        /// 
        /// Get SAP Datatable Info
        /// 
        /// SAP Function Module
        /// SAP Function Group
        /// SAP所需参数
        /// DataTable
        public static DataTable GetDatatableFromSAP(string rfcFuctionName, string rfcTableName, Dictionary<string, object> keyValues = null) =>
            GetDataSetFromSAP(rfcFuctionName, new List<string> { rfcTableName }, keyValues)[rfcTableName];

        /// 
        /// Get SAP DataSet Info(SAP返回多个表)
        /// 
        /// SAP Function Module
        /// SAP Function Groups
        /// SAP所需参数
        /// Dictionary
        public static Dictionary<string ,DataTable> GetDataSetFromSAP(string rfcFuctionName, List<string> rfcTableNameList, Dictionary<string, object> keyValues = null)
        {
            if (string.IsNullOrEmpty(rfcFuctionName) || rfcTableNameList == null || rfcTableNameList.Count <= 0)
                return null;
            List<string> rfcTableNames = rfcTableNameList.Distinct().ToList();
            try
            {
                Dictionary<string, DataTable> result = new Dictionary<string, DataTable>();
                Dictionary<string, IRfcTable> rfcTableDic = new Dictionary<string, IRfcTable>();
                RfcDestination destination = GetRfcDestination();
                IRfcFunction func = destination.Repository.CreateFunction(rfcFuctionName);
                if (keyValues != null && keyValues.Count > 0)
                {
                    foreach (var item in keyValues)
                    {
                        func.SetValue(item.Key, item.Value);
                    }
                }
                rfcTableNames.ForEach(item =>
                {
                    IRfcTable rfcTable = func.GetTable(item);
                    rfcTableDic.Add(item, rfcTable);
                });
                func.Invoke(destination);
                rfcTableNames.ForEach(item =>
                {
                    result.Add(item, GetDataTableFromRFCTable(rfcTableDic[item]));
                });
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// 
        /// 转换IRfcTable为Datatable
        /// 
        /// 
        /// 
        private static DataTable GetDataTableFromRFCTable(IRfcTable myrfcTable)
        {

            DataTable loTable = new DataTable();
            int liElement = 0;
            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;
        }
    }
}

 

你可能感兴趣的:(.NET利用RFC连接SAP,查询、读取SAP数据)