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

在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, "");
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, "");
pairs.Add(RfcConfigParameters.MaxPoolSize, "");
pairs.Add(RfcConfigParameters.IdleTimeout, "");
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 <= )
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 > )
{
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 = ;
for (liElement = ; liElement <= myrfcTable.ElementCount - ; liElement++)
{
RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement);
loTable.Columns.Add(metadata.Name);
}
foreach (IRfcStructure Row in myrfcTable)
{
DataRow ldr = loTable.NewRow();
for (liElement = ; liElement <= myrfcTable.ElementCount - ; liElement++)
{
RfcElementMetadata metadata = myrfcTable.GetElementMetadata(liElement);
ldr[metadata.Name] = Row.GetString(metadata.Name);
}
loTable.Rows.Add(ldr);
}
return loTable;
}
}
}

.NET利用RFC连接SAP,查询、读取SAP数据的更多相关文章

  1. 利用Angular.js从PHP读取后台数据

    之前已经有非常多方法能够通过angular进行本地数据的读取.曾经的样例中,大多数情况都是将数据存放到模块的$scope变量中,或者直接利用ng-init定义初始化的数据. 可是这些方法都仅仅为了演示 ...

  2. Jquery Mobile实例--利用优酷JSON接口读取视频数据

    本文将介绍,如何利用JqueryMobile调用优酷API JSON接口显示视频数据. (1)注册用户接口. 首页,到 http://open.youku.com 注册一个账户,并通过验证.然后找到A ...

  3. 30. PL/SQL Developer连接服务器查询时,数据乱码处理

    在windows中创 建一个名为“NLS_LANG”的系统环境变量,设置其值为"AMERICAN_AMERICA.ZHS16GBK",   NLS_LANG的值为:select u ...

  4. 在ASP.NET MVC中利用Aspose.cells 将查询出的数据导出为excel,并在浏览器中下载。

    正题前的唠叨 本人是才出来工作不久的小白菜一颗,技术很一般,总是会有遇到一些很简单的问题却不知道怎么做,这些问题可能是之前解决过的.发现这个问题,想着提升一下自己的技术水平,将一些学的新的'好'东西记 ...

  5. 利用Aspose.cells 将查询出的数据导出为excel,并在浏览器中下载。

    正题前的唠叨 本人是才出来工作不久的小白菜一颗,技术很一般,总是会有遇到一些很简单的问题却不知道怎么做,这些问题可能是之前解决过的.发现这个问题,想着提升一下自己的技术水平,将一些学的新的‘好’东西记 ...

  6. [PHP]socket的连接超时 与 读取/写入超时

    socket处理时有两种超时 , 分为连接超时 和 读取/写入数据超时 1. stream_socket_client 函数中的超时时间是连接超时 , 默认是php.ini中的default_sock ...

  7. STM32读取Guidance数据——Guidance SDK

    更新记录:2019/11/14    更新STM32(F407VET6)读取Guidance数据 Github地址. 背景:想要将祖传的Guidance用于DJI A3/新固件的N3飞控.DJI已经停 ...

  8. .NET通过RFC读取SAP数据

    本篇文章中我主要讲的是.NET如何通过RFC从SAP中读取数据.为了功能的可复用性,我将调用RFC的代码从业务层中分离出来单独建立在一个namespace中. 当然除了需要我们自己编写代码以外,还需要 ...

  9. C#如何通过NCO3.0来连接SAP并调用SAP中的RFC

    ,这是SAP针对.Net开发的专用组件,安装完成之后在C:\Program Files\SAP\SAP_DotNetConnector3_x86目录下面会有sapnco_utils.dll sapnc ...

随机推荐

  1. POJ1016 Numbers That Count

    题目来源:http://poj.org/problem?id=1016 题目大意: 对一个非负整数定义一种运算(inventory):数这个数中各个数字出现的次数,然后按顺序记录下来.比如“55531 ...

  2. Github网站css加载不出来的处理方法(转,亲测有效)

    转载链接:https://blog.csdn.net/qq_36589706/article/details/80573008因为工作需求,自己会经常使用到github,但是突然有一天打开github ...

  3. inode与block

    1.   inode 是索引节点,在每个Linux存储设备或者存储设备的分区被格式化为ext4文件系统,一般生成两个部分:第一部分为inode,第二部分为block        inode:存放的是 ...

  4. DozerBeanMapper 配置

    applicationContext.xml <bean id="mapper" class="org.dozer.spring.DozerBeanMapperFa ...

  5. HashMap遍历时的性能对比

    使用KeySet和EntrySet遍历的差别 public static void main(String[] args) { HashMap<Integer, Integer> hasM ...

  6. js——移动端js事件、zepto.js

    1. touchstart : 手指放到屏幕上时触发 2. touchmove : 手指在屏幕上滑动时触发 3. touched : 手指离开屏幕时触发 4. touchcancel : 系统取消to ...

  7. 配置HEXO

    软件介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. 使用版本和操作系统 官网最新版 Windo ...

  8. postgresql导出某张表的数据

    \copy 表名 to 路径 with csv 比如: \copy dataset to /home/backup/dataset.csv with csv \copy dataset to /hom ...

  9. IE浏览器与非IE浏览器JS日期兼容性问题处理

    执行语句 console.log(new Date("2017-07-04 18:40").getTime()); 在IE浏览器中打印出:NAN 在非IE浏览器中打印出:14991 ...

  10. windows 安装python

    前言: Windows 中直接使用Python真的是心累 安装vs 2017(最好是最新版的, 因为python依赖于一些vs提供的包) 下载最新的python的安装程序 安装完毕之后, 不像Linu ...