一个完整的SAP RFC调用接口封装
因为经常需要访问sap操作数据,就封装了一个类方便调用,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,本接口就通过这个com访问sap,因为com的后期绑定问题故使用了vb.net开发,分享给大家。
特色之处:把SAP的传入、传出内表直接映射成dotNet的DataTable方便操作,并提供了从字段列表到DataTable的转换函数。
1 SAP
操作
1.1
创建结构
ZMM_MATNR
(
tcode: se11)
1.2
创建
RFC
函数
ZTEST2
(
tcode: se37)
Attributes->Processing type:
选择
Remote-enabled module
,激活远程调用
设置
Import
参数
设置
export
参数
设置
tables
参数
编写
Source code
代码(源码见附
1
),并激活函数
2 VB.NET 2005
操作
2.1
创建
VB.NET
项目,并引用
SAP
的
COM
…
编写
VB.NET
代码调用
SAP
的
RFC
函数(源码见附
2
)
3
调试技巧
如果在
VB.NET
代码调用
SAP
的
RFC
函数时,想直接进入
SAP
的
RFC
函数调试,
先在
VB.NET
代码中设置连接为
DEBUG
状态
:
Connect.ABAPDebug = True
再在
ABAP
代码中设置外部
DEBUG
断点:
Break-point.
运行
VB.NET
代码,当调用
SAP
的
RFC
函数时,就会打开
ABAP
编辑器并在外部
DEBUG
断点停住让你调试……
附
1:
FUNCTION ZTEST2.
*"-------------------------------------------------------------------
---
*"*"Local interface:
*" IMPORTING
*" VALUE(MBLNR) TYPE MBLNR
*" EXPORTING
*" VALUE(MSG) TYPE CHAR200
*" TABLES
*" TB_MATNR STRUCTURE ZMM_MATNR
*"-------------------------------------------------------------------
---
Break-point.
MSG = 'hello'.
TB_MATNR-MATNR = '100000-000001-88'.
append TB_MATNR.
TB_MATNR-MATNR = '100000-000001-99'.
append TB_MATNR.
ENDFUNCTION.
附
2:
Public Sub sub1() Dim Connect As Object Dim Functions As Object Functions = CreateObject("Sap.Functions") ' 创建 RFC 的本地对象 Connect = Functions.Connection ' 设置连接 Connect.ApplicationServer = "192.168.1.220" ' 赋值服务器 IP Connect.Client = "001" ' 赋值 SAP 集团代码 Connect.Language = "ZH" ' 置 SAP 系统界面中文 Connect.User = "USER1" ' 赋值 SAP 登录用户名 Connect.Password = "123456" ' 赋值 SAP 登录用户密码 Connect.SystemNumber = 1 ' 赋值 SAP 系统号 Connect.ABAPDebug = True If Not Connect.Logon(0, True) Then ' 软件登录 SAP 并判断 \ Dim fffff fffff = 1 Else ' 登录 SAP 成功 End If Dim oFun As Object oFun = Functions.Add("ZTEST2") oFun.exports("MBLNR") = "aaa" Dim returnFunc returnFunc = oFun.Call Dim ss ss = oFun.imports("MSG").Value Dim tb1 As Object tb1 = oFun.Tables("TB_MATNR") Dim i For i = 1 To tb1.rowcount Dim s1 s1 = tb1(i, "MATNR") Next i End Sub
'---------------------------------------------------------------- ' Copyright (C) ' 版权所有。 ' ' 文件名 :SAP.vb ' 功能描述:封装对SAP的基本访问,本类只提供基础信息,具体访问SAP RFC的类从此类继承 ' ' 创建标识:www.cnblogs.com/slashout, ' ' 修改标识:www.cnblogs.com/slashout, ' 修改描述:增加传入内表、传出参数等的封闭操作 ' ------------------------------------------------------------------------ Public Class SAP PRivate m_sapObject As Object 'sap远程函数调用对象 Protected m_sapFun As Object 'sap函数 Private m_sapConnection As Object '与SAP的连接 ''' <summary> ''' 构造函数,传入sap的基本信息 ''' </summary> ''' <param name="sapSystem">Sap系统,可以传入null</param> ''' <param name="applicationServer">SAP服务器ip</param> ''' <param name="Client">集团号,如800</param> ''' <param name="SystemNumber">系统编号,如00</param> ''' <remarks></remarks> Public Sub New(ByVal sapSystem As String, ByVal ApplicationServer As String, ByVal Client As String, ByVal SystemNumber As String) Me.m_sapObject = CreateObject("SAP.Functions") Me.m_sapConnection = Me.m_sapObject.Connection() If String.IsNullOrEmpty(sapSystem) = False Then Me.m_sapConnection.System = sapSystem End If Me.m_sapConnection.ApplicationServer = ApplicationServer Me.m_sapConnection.Client = Client Me.m_sapConnection.SystemNumber = SystemNumber End Sub ''' <summary> ''' 登录SAP,成功,返回True,失败,返回False ''' </summary> ''' <param name="User">用户</param> ''' <param name="PassWord">口令</param> ''' <param name="Language">语言,如ZH、EN等,可以传入null</param> ''' <returns>是否登录成功</returns> ''' <remarks></remarks> Public Function ConnectToSAP(ByVal User As String, ByVal Password As String, ByVal Language As String) As Boolean Me.m_sapConnection.user = User Me.m_sapConnection.Password = Password If String.IsNullOrEmpty(Language) = False Then Me.m_sapConnection.Language = Language Else Me.m_sapConnection.Language = "EN" End If Me.m_sapObject.AutoLogon = True '自动登录 Return Me.m_sapObject.Connection.logon(0, True) '登录是否成功 End Function ''' <summary> ''' 设置调用的sap函数名称 ''' </summary> ''' <param name="sapFuncName">sap函数名称</param> ''' <remarks></remarks> Public Sub 设置SAP远程函数名(ByVal sapFuncName As String) Me.m_sapFun = Me.m_sapObject.Add(sapFuncName) If m_sapFun Is Nothing Then Throw New Exception("Sap远程函数名无效:" + sapFuncName) End If End Sub ''' <summary> ''' 设置Sap函数的传入调用参数 ''' </summary> ''' <param name="paramName">参数名称</param> ''' <param name="paramValue">参数值</param> ''' <remarks></remarks> Public Sub 设置参数(ByVal paramName As String, ByVal paramValue As Object) Dim param As Object param = Me.m_sapFun.Exports(paramName) If param Is Nothing Then Throw New Exception("Sap远程函数的参数名无效:" + paramName) End If param.Value = paramValue End Sub ''' <summary> ''' 设置sap的传入内表,用dt_value模拟这个内表 ''' </summary> ''' <param name="SapTableName">sap函数传入内表的名字</param> ''' <param name="dt_value">模拟的DataTable,要求与传入内表的字段名一致</param> ''' <remarks></remarks> Public Sub 设置传入内表(ByVal SapTableName As String, ByVal dt_value As DataTable) Dim sapdata As Object 'sap传入内表 Dim saprow As Object 'sap传入内表的一行 Dim dc As DataColumn Dim index As Integer sapdata = Me.m_sapFun.Tables(SapTableName) For index = 0 To dt_value.Rows.Count - 1 '循环表,并给sap传入内表赋值 saprow = sapdata.Rows.Add() '传入内表新增一行记录,下面为传入内表记录赋值 For Each dc In dt_value.Columns saprow(dc.ColumnName) = dt_value.Rows(index)(dc.ColumnName).ToString() Next Next End Sub ''' <summary> ''' 当参数设置完成后,执行函数调用 ''' </summary> ''' <remarks></remarks> Public Sub 执行函数调用() If Me.m_sapFun.Call() = False Then Throw New Exception("Sap远程函数调用失败。") '从SAP取数出错,退出函数 End If End Sub ''' <summary> ''' 根据字段列表(逗号分隔)建立指定字段的DataTable ''' </summary> ''' <param name="fields">字段列表(逗号分隔)</param> ''' <returns>空表</returns> ''' <remarks></remarks> Public Function 建立空表(ByVal fields As String) As DataTable Dim dt As New DataTable Dim strs As String() Dim s As String strs = fields.Split(",") For Each s In strs dt.Columns.Add(s.Trim()) Next Return dt End Function ''' <summary> ''' 取得sap的传出参数值 ''' </summary> ''' <param name="paramName">传出参数名</param> ''' <returns>传出参数值</returns> ''' <remarks></remarks> Public Function 取Sap传出参数(ByVal paramName As String) As String Dim param As Object param = Me.m_sapFun.Imports(paramName) If param Is Nothing Then Throw New Exception("Sap远程函数的参数名无效:" + paramName) End If If param.Value Is Nothing Then Return "" Else Return param.Value.ToString() End If End Function ''' <summary> ''' 把sap函数调用结构的传出内表转成dotNet的表 ''' </summary> ''' <param name="fields">sap传出内表的字段列表,字段间以逗号分隔</param> ''' <param name="SapTableName">sap传出内表的表名</param> ''' <returns>把sap内表导出的dotnet表,字段都为string型</returns> ''' <remarks></remarks> Public Function 取Sap传出表数据(ByVal fields As String, ByVal SapTableName As String, ByVal 是否去前后空格 As Boolean) As DataTable '按字段列表建立表,fields中的字段列表用逗号分隔 Dim dt As DataTable dt = Me.建立空表(fields) '从sap表中读数据,循环sap中取得的数据,写入dt Dim sapdata As Object Dim saprow As Object Dim dr As DataRow '数据增加的新行 Dim dc As DataColumn sapdata = Me.m_sapFun.Tables(SapTableName) For Each saprow In sapdata.Rows dr = dt.NewRow() For Each dc In dt.Columns If 是否去前后空格 = True Then dr(dc.ColumnName) = saprow(dc.ColumnName).ToString().Trim() Else dr(dc.ColumnName) = saprow(dc.ColumnName).ToString() End If Next dt.Rows.Add(dr) Next Return dt End Function ''' <summary> ''' 关闭sap的连接 ''' </summary> ''' <remarks></remarks> Public Sub DisConnectSAP() Me.m_sapConnection.logoff() End Sub End Class
本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/p/4073790.html