当前位置: 移动技术网 > IT编程>开发语言>.net > .NET利用RFC查询SAP数据

.NET利用RFC查询SAP数据

2018年10月22日  | 移动技术网IT编程  | 我要评论

黑百合,校园春色 校花,mm公寓图片

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

在nuget 添加 sapnco

一个简单的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;
        }
    }
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网