C#通过RFC调用SAP
using System;
using System.Collections.Generic;
using
SAP.Middleware.Connector;
using System.Data;
using
System.Xml;
namespace RFC
{
///
/// C#与SAP的RFC接口类
///
public class RFCInterface
{
#region
初始化
///
///
初始化SRM-RFC类
///
///
PRD_000为500系统,PRD_001为300系统
public
RFCInterface(string
destinationName)
{
DestName
=
destinationName;
//RfcDest =
GetDestination(DestName);
ID = new MyBackendConfig();
//实例化IDestinationConfiguration
}
#endregion
#region
数据定义
private RfcDestination
rfcDest;
protected
RfcDestination RfcDest
{
get
{
if (rfcDest == null && DestName !=
null)
{
this.Connect();
return
rfcDest;
}
else
{
return
rfcDest;
}
}
set {
rfcDest = value; }
}
///
///
DestinationName,用来选择不同的连接参数
///
private string
destName;
protected string
DestName
{
get {
return destName;
}
set {
destName = value; }
}
IDestinationConfiguration ID;
#endregion
#region
管理RfcDestination连接
///
///
建立RFC连接
///
public void
Connect()
{
RfcDestinationManager.RegisterDestinationConfiguration(ID);
RfcDest =
RfcDestinationManager.GetDestination(DestName);
}
///
///
断开RFC连接
///
public void
DisConnect()
{
RfcDestinationManager.UnregisterDestinationConfiguration(ID);
}
#endregion
#region
从SAP读取数据
///
///
从SAP按Table读取数据
///
///
函数名称
///
表格名称
///
表格列名
///
数据表
public DataTable
ReadTableFromSAP(string functionName, string tableName, List
columnNames)
{
RfcRepository repo =
RfcDest.Repository;
IRfcFunction functionBapi = repo.CreateFunction(functionName);
//调用函数名
functionBapi.Invoke(RfcDest);
//执行函数
IRfcTable table = functionBapi.GetTable(tableName);
//获取相应的品号内表
DataTable dt = new DataTable();
//新建表格
//增加表的列
foreach (string colName in
columnNames)
{
dt.Columns.Add(colName);
//表格添加一列
}
for
(int i = 0; i < table.RowCount;
i++)
{
table.CurrentIndex = i;
//当前内表的索引行
DataRow dr =
dt.NewRow();
foreach (string colName in
columnNames)
{
dr[colName] = table.GetString(colName);
//给表格赋值
}
dt.Rows.Add(dr);
//填充该表格的值
}
//prd =
null;
repo
=
null;
return dt;
}
///
///
从SAP按Table读取数据
///
///
函数名称
///
表格名称
///
表格列名
///
传入参数
///
数据表
public DataTable
ReadTableFromSAP(string functionName, string tableName, List columnNames, List
inParams)
{
RfcRepository repo =
RfcDest.Repository;
IRfcFunction functionBapi = repo.CreateFunction(functionName);
//调用函数名
//设置Import的参数
foreach (RFCInParameter inParam in
inParams)
{
functionBapi.SetValue(inParam.ParameterName,
inParam.ParameterValue);
}
functionBapi.Invoke(RfcDest);
//执行函数
IRfcTable table = functionBapi.GetTable(tableName);
//获取相应的品号内表
DataTable dt = new DataTable();
//新建表格
//增加表的列
foreach (string colName in
columnNames)
{
dt.Columns.Add(colName);
//表格添加一列
}
for
(int i = 0; i < table.RowCount;
i++)
{
table.CurrentIndex = i;
//当前内表的索引行
DataRow dr =
dt.NewRow();
foreach (string colName in
columnNames)
{
dr[colName] = table.GetString(colName);
//给表格赋值
}
dt.Rows.Add(dr);
//填充该表格的值
}
//prd =
null;
repo
=
null;
return dt;
}
#endregion
#region
写回数据到SAP
///
///
将结果表返回给SAP
///
///
Function名称
///
返回表名
///
需返回的数据表,表名需和SAP列名相同
///
成功返回True,失败False
public bool
WriteTableToSAP(string functionName, string tableName, DataTable
dt)
{
if (dt
!= null)
{
//对空的DataTable直接返回真
if (dt.Rows.Count ==
0)
{
return
true;
}
try
{
int intSL = 10000;
//一次同步数量
int intRowNumber =
dt.Rows.Count;
int intCS, intYS, intCount; //循环次数,剩余数量,
循环时处理数量
intCS = System.Convert.ToInt32(intRowNumber / intSL) +
1;
//分批处理次数
intYS = intRowNumber %
intSL;
//最后执行数量
RfcRepository repo =
rfcDest.Repository;
IRfcFunction funBapi = repo.CreateFunction(functionName);
//调用函数名
IRfcTable tblROF = funBapi.GetTable(tableName);
//获取相应的表
int k =
0;
for (int j = 0; j < intCS;
j++)
{
intCount = (j == intCS - 1 ? intYS :
intSL);
tblROF.Clear();
for (int i = 0; i < intCount;
i++)
{
tblROF.Insert();
for (int m = 0; m < dt.Columns.Count;
m++)
{
tblROF.CurrentRow.SetValue(dt.Columns[m].ColumnName,
dt.Rows[k][m].ToString());
}
k++;
}
funBapi.SetValue(tableName,
tblROF);
funBapi.Invoke(rfcDest);
//提交
}
//引用回传结果
//IRfcTable RETURNStructure =
funBapi.GetTable("RETURN");
//显示调用结果
//MessageBox.Show(RETURNStructure.GetString("MESSAGE").ToString());
// prd =
null;
repo =
null;
return
true;
}
catch
{
return
false;
}
}
else
{
return
false;
}
}
///
///
回写数据表到SAP
///
///
RFC函数名
///
SAP数据表名
///
回传数据表
///
SAP表列名,需与回传表保持一致
///
public bool
WriteTableToSAP(string functionName, string tableName, DataTable dt, List
SAPColumnNames)
{
if
(SAPColumnNames.Count !=
dt.Columns.Count)
{
return
false;
}
else if
(dt !=
null)
{
try
{
int intRowNumber =
dt.Rows.Count;
int intCS, intYS, intCount; //循环次数,剩余数量,
循环时处理数量
intCS = System.Convert.ToInt32(intRowNumber / 10000) +
1;
//分批处理次数
intYS = intRowNumber %
10000;
//最后执行数量
RfcRepository repo =
rfcDest.Repository;
IRfcFunction funBapi = repo.CreateFunction(functionName);
//调用函数名
IRfcTable tblROF = funBapi.GetTable(tableName);
//获取相应的表
int k =
0;
for (int j = 0; j < intCS;
j++)
{
intCount = (j == intCS - 1 ? intYS :
10000);
tblROF.Clear();
for (int i = 0; i < intCount;
i++)
{
tblROF.Insert();
for (int m = 0; m < dt.Columns.Count;
m++)
{
tblROF.CurrentRow.SetValue(SAPColumnNames[m],
dt.Rows[k][m].ToString());
}
k++;
}
funBapi.SetValue(tableName,
tblROF);
funBapi.Invoke(rfcDest);
//提交
}
//引用回传结果
//IRfcTable RETURNStructure =
funBapi.GetTable("RETURN");
//显示调用结果
//MessageBox.Show(RETURNStructure.GetString("MESSAGE").ToString());
// prd =
null;
repo =
null;
return
true;
}
catch
{
return
false;
}
}
else
{
return
false;
}
}
#endregion
}
#region
RFC传入参数
///
///
RFC传入参数
///
public struct
RFCInParameter
{
///
///
参数名称
///
string
parameterName;
///
///
参数值
///
string
parameterValue;
public string
ParameterName
{
get {
return parameterName;
}
set {
parameterName = value; }
}
public string
ParameterValue
{
get {
return parameterValue;
}
set {
parameterValue = value; }
}
public RFCInParameter(string
paramName, string paramValue)
{
parameterName =
paramName;
parameterValue = paramValue;
}
}
#endregion
#region
Destination参数类
///
///
destination参数类
///
public class
RFCDestinationParameter
{
private string
paramName;
//参数名称
private string appServerHost;
private string systemNumber;
private string user;
private
string password;
private string
client;
private string
language;
private string
poolsize;
private string
maxpoolsize;
private string
idleTimeout;
public string
ParamName
{
get {
return paramName;
}
set {
paramName = value; }
}
public string
AppServerHost
{
get {
return appServerHost;
}
set {
appServerHost = value; }
}
public string
SystemNumber
{
get {
return systemNumber;
}
set {
systemNumber = value; }
}
public string
User
{
get {
return user;
}
set {
user = value; }
}
public string
Password
{
get {
return password;
}
set {
password = value; }
}
public string
Client
{
get {
return client;
}
set {
client = value; }
}
public string
Language
{
get {
return language;
}
set {
language = value; }
}
public string
Poolsize
{
get {
return poolsize;
}
set {
poolsize = value; }
}
public string
Maxpoolsize
{
get {
return maxpoolsize;
}
set {
maxpoolsize = value; }
}
public string
IdleTimeout
{
get {
return idleTimeout;
}
set {
idleTimeout = value; }
}
public
RFCDestinationParameter()
{
}
public
RFCDestinationParameter(string serverHost, string systemNumber, string user,
string password, string client, string language, string poolsize, string
maxpoolsize, string idleTimeout)
{
AppServerHost =
serverHost;
SystemNumber =
systemNumber;
User =
user;
Password =
password;
Client =
client;
Language =
language;
Poolsize =
poolsize;
Maxpoolsize =
maxpoolsize;
IdleTimeout = idleTimeout;
}
}
#endregion
#region 读取配置类
///
/// 参数配置类
///
public static class RFCConfig
{
private static string
configFileName = string.Empty;
public static string
ConfigFileName
{
get {
return configFileName;
}
set {
configFileName = value; }
}
#region
读取XML参数
//读取RFC配置文件
public static List
ReadRFCConfig()
{
//string
xmlName = System.Environment.CurrentDirectory +
@"\RFCConfig.xml";
if (ConfigFileName ==
string.Empty)
{
ConfigFileName = System.Environment.CurrentDirectory +
@"\RFCConfig.xml";
}
return
ReadRFCConfig(ConfigFileName);
}
///
///
读取RFC配置文件
///
///
配置文件名称
///
private static List
ReadRFCConfig(string configName)
{
if
(System.IO.File.Exists(configName))
{
XmlDocument doc = new
XmlDocument();
doc.Load(configName);
//读取XML文件
List destParams = new
List();
XmlNodeList dests =
doc.SelectNodes("/configuration/SAP.Middleware.Connector/ClientSettings/DestinationConfiguration/destinations/add");
if (dests !=
null)
{
foreach (XmlNode destParam in
dests)
{
//paramNames.Add(destParam.Attributes["name"].Value);
RFCDestinationParameter rfcDestParam = new
RFCDestinationParameter();
rfcDestParam.ParamName =
destParam.Attributes["NAME"].Value;
rfcDestParam.AppServerHost =
destParam.Attributes["ASHOST"].Value;
rfcDestParam.User =
destParam.Attributes["USER"].Value;
rfcDestParam.Password =
destParam.Attributes["PASSWD"].Value;
rfcDestParam.Client =
destParam.Attributes["CLIENT"].Value;
rfcDestParam.Language =
destParam.Attributes["LANG"].Value;
rfcDestParam.SystemNumber =
destParam.Attributes["SYSNR"].Value;
rfcDestParam.Maxpoolsize =
destParam.Attributes["MAX_POOL_SIZE"].Value;
rfcDestParam.Poolsize =
destParam.Attributes["POOL_SIZE"].Value;
rfcDestParam.IdleTimeout =
destParam.Attributes["IDLE_TIMEOUT"].Value;
destParams.Add(rfcDestParam);
}
}
return
destParams;
}
else
{
return
null;
}
}
#endregion
}
#endregion
#region
登录参数设置类
///
///
登录参数设置
///
public class
MyBackendConfig : IDestinationConfiguration
{
public
RfcConfigParameters GetParameters(String
destinationName)
{
List
rfcDestParams =
RFCConfig.ReadRFCConfig();
foreach (RFCDestinationParameter destParam in
rfcDestParams)
{
if (destParam.ParamName.Equals(destinationName,
StringComparison.OrdinalIgnoreCase))
{
RfcConfigParameters parms = new
RfcConfigParameters();
parms.Add(RfcConfigParameters.AppServerHost,
destParam.AppServerHost);
//SAP主机IP
parms.Add(RfcConfigParameters.SystemNumber, destParam.SystemNumber);
//SAP实例
parms.Add(RfcConfigParameters.User, destParam.User);
//用户名
parms.Add(RfcConfigParameters.Password, destParam.Password);
//密码
parms.Add(RfcConfigParameters.Client, destParam.Client); //
Client
parms.Add(RfcConfigParameters.Language, destParam.Language);
//登陆语言
parms.Add(RfcConfigParameters.PoolSize,
destParam.Poolsize);
parms.Add(RfcConfigParameters.MaxPoolSize,
destParam.Maxpoolsize);
parms.Add(RfcConfigParameters.IdleTimeout,
destParam.IdleTimeout);
return
parms;
}
}
return null;
}
public bool
ChangeEventsSupported()
{
return
false;
}
public event
RfcDestinationManager.ConfigurationChangeHandler
ConfigurationChanged;
}
#endregion
}
C#通过RFC调用SAP的更多相关文章
- C#通过RFC连接sap系统
先理解一下 RFC(Romote Function Call)远程函数调用 调用前提: 1.要想通过C# 通过RFC调用SAP端,SAP端要存在RFC远程调用的函数才行(例如SAP端通过SE37创建) ...
- C# 使用 SAP NCO3.0 调用SAP RFC函数接口
最近使用C#调用SAP RFC函数,SAP提供了NCO3.0组件. 下载组件安装,之后引用“sapnco.dll”和“sapnco_utils.dll”两个文件. 在程序中 using SAP.Mid ...
- C#如何通过NCO3.0来连接SAP并调用SAP中的RFC
,这是SAP针对.Net开发的专用组件,安装完成之后在C:\Program Files\SAP\SAP_DotNetConnector3_x86目录下面会有sapnco_utils.dll sapnc ...
- K2 BPM项目 基于COM组件调用SAP RFC 问题
K2 BPM项目 基于COM组件调用SAP RFC 问题 问题前景: 环境:Win 2008 R2 64bit 最近项目中有支流程需求中需要在会计入账环节回写SAP的会计凭证. SAP组给我们提供.N ...
- 一个完整的SAP RFC调用接口封装
因为经常需要访问sap操作数据,就封装了一个类方便调用,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,本接口就通过这个com访问sap,因为com的后期绑定问题故使用了vb ...
- java 调用SAP RFC函数错误信息
RFC接口调用SAP如果有异常会通过com.sap.mw.jco.JCO$Exception: 抛出异常 在开发中遇到的异常有如下 用户名密码可能是错误或者用户无权限,确认用户,必要时联系SAP负责人 ...
- 还在写SQL做SAP二开?通过RFC调用NetWeaver,让HANA数据库操作更可靠
相比于从零开始构建全套信息化系统,基于成熟的ERP等行业软件做二次开发是更多中大型企业应对个性化软件需求的首选方案.如何在二开模块中,可靠地对成品软件的数据库进行读写操作,以满足单据自动创建.元数据自 ...
- .NET通过RFC读取SAP数据
本篇文章中我主要讲的是.NET如何通过RFC从SAP中读取数据.为了功能的可复用性,我将调用RFC的代码从业务层中分离出来单独建立在一个namespace中. 当然除了需要我们自己编写代码以外,还需要 ...
- XP安装IIS来加载aspx页面(Web调用SAP数据)
1,安装IIS 在XP中安装IIS方法很简单,安装时需要提供安装光盘来加载I386文件,可以使用虚拟光驱或光盘.在此做个简单说明(控制面板-添加/删除 Windows组件-勾选Internet信息服务 ...
随机推荐
- 再谈:jquery编写插件的方法
版权声明:作者原创,转载请注明出处! 编写插件的两种方式: 1.类级别开发插件(1%) 2.对象级别开发(99%) 类级别的静态开发就是给jquery添加静态方法,三种方式 1.添加新的全局函数 2. ...
- com组件远程桌面rdp模块的调用
rdp(remote desktop protocol)是一个多通道的协议,包括客户端视音传输.文件传输和通讯端口转向等等功能,通过压缩处理的数据网络传输也是相当快.我们在windows操作系统下面, ...
- C语言实现二叉树-02版
---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...
- 将外卖O2O广告一棍子打成竞价排名,秤把平了吗?
近日,诸多媒体报道称美团外卖.饿了么等外卖O2O将竞价排名引入外卖平台当中进行广告运营一事闹得沸沸扬扬.那么,美团外卖.饿了么真的都是竞价排名吗? 其实,美团外卖的付费推广仅仅只是针对列表的固定位置, ...
- android: 实现跨程序数据共享
简单起见,我们还是在上一章中 DatabaseTest 项目的基础上继续开发,通过内容提供器 来给它加入外部访问接口. 打开 DatabaseTest 项目,首先将 MyDatabaseHelper ...
- 你的项目真的需要Session吗?
在web开发中,Session这个东西一直都很重要,至少伴随我10年之久, 前一段时间发生一个性能问题,因为Redis session 问题,后来想想 其实我的项目session 是不需要的. 先看看 ...
- js弹出放大图
<script type="text/javascript"> function openpic(url){ OpenWindow = window.open(&quo ...
- 流媒体选择Nginx是福还是祸?
CDN,视频云,已经“僧多粥少” 视频直播的持续升温,无意间也让带宽生意的争夺变得异常残酷.一时间,各种云计算.CDN.视频云提供商都在视频尤其是直播上投入重兵,揭竿而起的新生起义军们也正马不停蹄的赶 ...
- 连接UI到代码
本章,你将连接FoodTracker应用程序的UI到代码并定义一些可执行的动作.当你完成时,你的应用程序将是这个样子: 学习目标在课程结束时,你将能够:1.解释一个storyboard中的场景和vie ...
- iphone/ipad/ipod设置VPN(pptp连接方式)
一.点击桌面上的-设置-图标进入设置(如图) 二.点击-通用-进入通用设置 三.点击-VPN-进入VPN设置(如图) 四.点击添加VPN设置进行设置 五.选择并连接