程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 分享一個完整的SAP RFC調用接口封裝

分享一個完整的SAP RFC調用接口封裝

編輯:ASP技巧

因為經常需要訪問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

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved