1. 程式人生 > >VB.NET中SAP RFC呼叫介面封裝

VB.NET中SAP RFC呼叫介面封裝

因為經常需要訪問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
   
PR
ivate 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="
application
Server">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="Pass
Word
">口令</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