.NET利用RFC查询SAP数据
程序员文章站
2022-04-15 11:13:43
为黄朴整理!!!!!!!!!!!!!!!!! 在NuGet 添加 sapnco 一个简单的SAPCommand,方法 GetDataTableFromRFCTable 复制于 https://www.cnblogs.com/jamin/p/3374139.html ......
为黄朴整理!!!!!!!!!!!!!!!!!
在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; } } }
下一篇: ELK环境配置+log4j日志记录
推荐阅读
-
asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)
-
Database.NET强大的数据库查询管理工具使用图文教程
-
C#利用ODP.net连接Oracle数据库的操作方法
-
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
-
ASP.NET中利用存储过程实现模糊查询
-
利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTable数据列表
-
asp.net实现的MVC跨数据库多表联合动态条件查询功能示例
-
利用Django框架中select_related和prefetch_related函数对数据库查询优化
-
C#利用ODP.net连接Oracle数据库的操作方法
-
ASP.NET MVC5+EF6+EasyUI 后台管理系统(81)-数据筛选(万能查询)实例