设为首页 收藏本站
查看: 1125|回复: 0

[经验分享] C#通过RFC调用SAP

[复制链接]

尚未签到

发表于 2015-9-18 14:24:12 | 显示全部楼层 |阅读模式
  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
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-115516-1-1.html 上篇帖子: SAP收货时自动创建采购订单 下篇帖子: SAP的Selection屏幕的事件处理顺序
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表