欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET利用RFC查询SAP数据

程序员文章站 2022-04-15 11:13:43
为黄朴整理!!!!!!!!!!!!!!!!! 在NuGet 添加 sapnco 一个简单的SAPCommand,方法 GetDataTableFromRFCTable 复制于 https://www.cnblogs.com/jamin/p/3374139.html ......

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

在nuget 添加 sapnco

.NET利用RFC查询SAP数据

一个简单的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
{
    /// <summary>
    /// sapcommand
    /// </summary>
    public static class sapcommand
    {
        /// <summary>
        /// 获取连接sap参数
        /// </summary>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 获取sap连接
        /// </summary>
        /// <returns></returns>
        private static rfcdestination getrfcdestination()
        {
            rfcdestination destination = rfcdestinationmanager.getdestination(getrfcconfigparameters());
            return destination;
        }

        /// <summary>
        /// get sap datatable info
        /// </summary>
        /// <param name="rfcfuctionname">sap function module</param>
        /// <param name="rfctablename">sap function group</param>
        /// <param name="keyvalues">sap所需参数</param>
        /// <returns>datatable</returns>
        public static datatable getdatatablefromsap(string rfcfuctionname, string rfctablename, dictionary<string, object> keyvalues = null) =>
            getdatasetfromsap(rfcfuctionname, new list<string> { rfctablename }, keyvalues)[rfctablename];

        /// <summary>
        /// get sap dataset info(sap返回多个表)
        /// </summary>
        /// <param name="rfcfuctionname">sap function module</param>
        /// <param name="rfctablenamelist">sap function groups</param>
        /// <param name="keyvalues">sap所需参数</param>
        /// <returns>dictionary<string ,datatable></returns>
        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;
            }
        }

        /// <summary>
        /// 转换irfctable为datatable
        /// </summary>
        /// <param name="myrfctable"></param>
        /// <returns></returns>
        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;
        }
    }
}