Because we often need to access sap operation data, we encapsulate a class for easy calling. The operating conditions require the installation of sap client. After the sap client is installed, there will be a com interface. This interface accesses sap through this com, because the later stage of com Because of the binding problem, I used vb.net to develop it and share it with everyone.
Features: Directly map SAP's incoming and outgoing internal tables to dotNet's DataTable for easy operation, and provide a conversion function from a field list to a DataTable.
'------------------------------------------------ ---------------
' Copyright (C) 2009
' all rights reserved.
'
'File name: SAP.vb
'Function description: Encapsulates basic access to SAP. This class only provides basic information. Specific access to SAP RFC classes inherit from this class.
'
'Create logo: www.cnblogs.com/81 , December 1, 2009
'
'Modified logo: www.cnblogs.com/81 , December 9, 2009
'Modify description: Add closed operations for incoming internal tables, outgoing parameters, etc.
'------------------------------------------------ -----------------------
Public Class SAP
Private m_sapObject As Object 'sap remote function call object
Protected m_sapFun As Object 'sap function
Private m_sapConnection As Object 'Connection to SAP
''' <summary>
''' constructor, pass in the basic information of sap
''' </summary>
''' <param name="sapSystem">Sap system, you can pass in null</param>
''' <param name="ApplicationServer">SAP server IP</param>
''' <param name="Client">Group number, such as 800</param>
''' <param name="SystemNumber">System number, such as 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>
''' Log in to SAP, return True if successful, return False if failed
''' </summary>
''' <param name="User">User</param>
''' <param name="Password">Password</param>
''' <param name="Language">Language, such as ZH, EN, etc., you can pass null</param>
''' <returns>Whether the login was successful</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 'Automatic login
Return Me.m_sapObject.Connection.logon(0, True) 'Whether the login was successful
End Function
''' <summary>
''' Set the name of the sap function to be called
''' </summary>
''' <param name="sapFuncName">sap function name</param>
''' <remarks></remarks>
Public Sub Set the SAP remote function name (ByVal sapFuncName As String)
Me.m_sapFun = Me.m_sapObject.Add(sapFuncName)
If m_sapFun Is Nothing Then
Throw New Exception("Sap remote function name is invalid: " + sapFuncName)
End If
End Sub
''' <summary>
''' Set the incoming call parameters of the Sap function
''' </summary>
''' <param name="paramName">Parameter name</param>
''' <param name="paramValue">Parameter value</param>
''' <remarks></remarks>
Public Sub Set parameters (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("The parameter name of the Sap remote function is invalid: " + paramName)
End If
param.Value = paramValue
End Sub
''' <summary>
''' Set the incoming internal table of sap and use dt_value to simulate this internal table
''' </summary>
''' <param name="SapTableName">The name of the internal table passed in by the sap function</param>
''' <param name="dt_value">The simulated DataTable must be consistent with the field name passed into the internal table</param>
''' <remarks></remarks>
Public Sub sets the incoming internal table (ByVal SapTableName As String, ByVal dt_value As DataTable)
Dim sapdata As Object 'sap passes in the internal table
Dim saprow As Object 'sap passes in a row of the internal table
Dim dc As DataColumn
Dim index As Integer
sapdata = Me.m_sapFun.Tables(SapTableName)
For index = 0 To dt_value.Rows.Count - 1 'Loop the table and assign values to the internal table passed to sap
saprow = sapdata.Rows.Add() 'Add a new row of records in the internal table, and assign values to the records in the internal table below.
For Each dc In dt_value.Columns
saprow(dc.ColumnName) = dt_value.Rows(index)(dc.ColumnName).ToString()
Next
Next
End Sub
''' <summary>
''' After the parameter setting is completed, execute the function call
''' </summary>
''' <remarks></remarks>
Public Sub Execute function call()
If Me.m_sapFun.Call() = False Then
Throw New Exception("Sap remote function call failed.") 'Error in fetching data from SAP, exit function
End If
End Sub
''' <summary>
''' Create a DataTable of specified fields based on the field list (comma separated)
''' </summary>
''' <param name="fields">Field list (comma separated)</param>
''' <returns>Empty table</returns>
''' <remarks></remarks>
Public Function Create an empty table (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>
''' Get the outgoing parameter value of sap
''' </summary>
''' <param name="paramName">Outgoing parameter name</param>
''' <returns>Outgoing parameter value</returns>
''' <remarks></remarks>
Public Function Get Sap outgoing parameters (ByVal paramName As String) As String
Dim param As Object
param = Me.m_sapFun.Imports(paramName)
If param Is Nothing Then
Throw New Exception("The parameter name of the Sap remote function is invalid: " + paramName)
End If
If param.Value Is Nothing Then
Return ""
Else
Return param.Value.ToString()
End If
End Function
''' <summary>
''' Convert the outgoing internal table of the sap function call structure into a dotNet table
''' </summary>
''' <param name="fields">Sap outgoing field list of the internal table, separated by commas</param>
''' <param name="SapTableName">The table name of the sap outgoing internal table</param>
''' <returns>The dotnet table exported from the sap internal table, all fields are string type</returns>
''' <remarks></remarks>
Public Function Get Sap outgoing table data (ByVal fields As String, ByVal SapTableName As String, ByVal whether to remove leading and trailing spaces As Boolean) As DataTable
'Create a table by field list, the field list in fields is separated by commas
Dim dt As DataTable
dt = Me. Create empty table (fields)
'Read data from the sap table, loop the data obtained in sap, and write it to dt
Dim sapdata As Object
Dim saprow As Object
Dim dr As DataRow 'New row of data added
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 whether to remove leading and trailing spaces = 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>
''' Close the sap connection
''' </summary>
''' <remarks></remarks>
Public Sub DisConnectSAP()
Me.m_sapConnection.logoff()
End Sub
End Class