因为经常需要访问sap操作数据,就封装了一个类方便调用,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,本接口就通过这个com访问sap,因为com的后期绑定问题故使用了vb.net开发,分享给大家。
特色之处:把SAP的传入、传出内表直接映射成dotNet的DataTable方便操作,并提供了从字段列表到DataTable的转换函数。
文件名 :SAP.vb
' 功能描述:封装对SAP的基本访问,本类只提供基础信息,具体访问SAP RFC的类从此类继承
' 修改描述:增加传入内表、传出参数等的封闭操作
' ------------------------------------------------------------------------
Public Class SAP
Private m_sapObject As Object 'sap远程函数调用对象
Protected m_sapFun As Object 'sap函数
Private m_sapConnection As Object '与SAP的连接
'''
''' 构造函数,传入sap的基本信息
'''
''' Sap系统,可以传入null
''' SAP服务器IP
''' 集团号,如800
''' 系统编号,如00
'''
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
'''
''' 登录SAP,成功,返回True,失败,返回False
'''
''' 用户
''' 口令
''' 语言,如ZH、EN等,可以传入null
'''
'''
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
'''
''' 设置调用的sap函数名称
'''
''' sap函数名称
'''
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
'''
''' 设置Sap函数的传入调用参数
'''
''' 参数名称
''' 参数值
'''
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
'''
''' 设置sap的传入内表,用dt_value模拟这个内表
'''
''' sap函数传入内表的名字
''' 模拟的DataTable,要求与传入内表的字段名一致
'''
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
'''
''' 当参数设置完成后,执行函数调用
'''
'''
Public Sub 执行函数调用()
If Me.m_sapFun.Call() = False Then
Throw New Exception("Sap远程函数调用失败。") '从SAP取数出错,退出函数
End If
End Sub
'''
''' 根据字段列表(逗号分隔)建立指定字段的DataTable
'''
''' 字段列表(逗号分隔)
'''
'''
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
'''
''' 取得sap的传出参数值
'''
''' 传出参数名
'''
'''
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
'''
''' 把sap函数调用结构的传出内表转成dotNet的表
'''
''' sap传出内表的字段列表,字段间以逗号分隔
''' sap传出内表的表名
'''
'''
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
'''
''' 关闭sap的连接
'''
'''
Public Sub DisConnectSAP()
Me.m_sapConnection.logoff()
End Sub
End Class