因為經常需要存取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