开发学院WEB开发ASP 分享一个完整的SAP RFC调用接口封装 阅读

分享一个完整的SAP RFC调用接口封装

 2010-01-28 10:46:44 来源:WEB开发网   
核心提示:因为经常需要访问sap操作数据,就封装了一个类方便调用,分享一个完整的SAP RFC调用接口封装,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,字段都为string型</returns>''' <remarks></remarks>
因为经常需要访问sap操作数据,就封装了一个类方便调用,运行条件需要安装sap客户端,在sap客户端安装之后会带有一个com接口,本接口就通过这个com访问sap,因为com的后期绑定问题故使用了vb.net开发,分享给大家。

特色之处:把SAP的传入、传出内表直接映射成dotNet的DataTable方便操作,并提供了从字段列表到DataTable的转换函数。


'----------------------------------------------------------------
' Copyright (C) 2009
' 版权所有。
'
' 文件名 :SAP.vb
' 功能描述:封装对SAP的基本访问,本类只提供基础信息,具体访问SAP RFC的类从此类继承
' 
' 创建标识:www.cnblogs.com/81, 2009年12月1日
'
' 修改标识:www.cnblogs.com/81, 2009年12月9日
' 修改描述:增加传入内表、传出参数等的封闭操作
' ------------------------------------------------------------------------
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




Tags:分享 一个 完整

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读