.net 连接 SAP for NCO3.0(三)

来源:互联网 发布:开盘数据恢复成功率 编辑:程序博客网 时间:2024/05/29 18:05
Imports Microsoft.VisualBasic
Imports SAP.Middleware.Connector
Imports System.Data
Imports System.IO
Imports LogInfo
Imports System.Diagnostics
Imports System.Collections
Imports System.Collections.Generic


Public Class SAPRFC
    Implements IDestinationConfiguration   '继承接口
    Public Destination As RfcDestination     'SAP Connector3.0 的RFC源
    Public SapFunction As IRfcFunction       'SAP RFC函数调用
    Public mystruct As IRfcStructure
    Public datatbl As IRfcTable              '调用到的RFC SAP table
    Public optionstbl As IRfcTable              '调用到的OPTIONS table
    Public fieldtbl As IRfcTable             '调用到的FIELD table
    Public ID As IDestinationConfiguration        '继承


    Public Function ChangeEventsSupported() As Boolean Implements IDestinationConfiguration.ChangeEventsSupported
        Return False
    End Function


    Public Event ConfigurationChanged(ByVal destinationName As String, ByVal args As RfcConfigurationEventArgs) Implements IDestinationConfiguration.ConfigurationChanged  '事件


    Public Function GetParameters(ByVal destinationName As String) As RfcConfigParameters Implements IDestinationConfiguration.GetParameters
        Dim parms As RfcConfigParameters = New RfcConfigParameters()
   
        If ("qas_000".Equals(destinationName)) Then
            parms.Add(RfcConfigParameters.AppServerHost, "")  'SAP主机IP
            parms.Add(RfcConfigParameters.SystemNumber, "")  'SAP实例
            parms.Add(RfcConfigParameters.User, "")      ' 用户名
            parms.Add(RfcConfigParameters.Password, "")  '密码
            parms.Add(RfcConfigParameters.Client, "777")       ' Client
            parms.Add(RfcConfigParameters.Language, "EN")      '登陆语言
            parms.Add(RfcConfigParameters.PoolSize, "5")
            parms.Add(RfcConfigParameters.PeakConnectionsLimit, "10")
            parms.Add(RfcConfigParameters.IdleTimeout, "600")
            Return parms
        ElseIf ("prd_000".Equals(destinationName)) Then
            'parms.Add(RfcConfigParameters.AppServerHost, "")  'SAP主机IP
            parms.Add(RfcConfigParameters.AppServerHost, "")  'SAP主机IP
            parms.Add(RfcConfigParameters.SystemNumber, "")  'SAP实例
            parms.Add(RfcConfigParameters.User, "")      ' 用户名
            parms.Add(RfcConfigParameters.Password, "")  '密码
            parms.Add(RfcConfigParameters.Client, "777")       ' Client
            parms.Add(RfcConfigParameters.Language, "ZH")      '登陆语言
            parms.Add(RfcConfigParameters.PoolSize, "5")
            'parms.Add(RfcConfigParameters.MaxPoolSize, "10")
            parms.Add(RfcConfigParameters.PeakConnectionsLimit, "10")
            parms.Add(RfcConfigParameters.IdleTimeout, "600")
            Return parms
        ElseIf ("qa1_000".Equals(destinationName)) Then
            parms.Add(RfcConfigParameters.AppServerHost, "")  'SAP主机IP
            parms.Add(RfcConfigParameters.SystemNumber, "")  'SAP实例
            parms.Add(RfcConfigParameters.User, "")      ' 用户名
            parms.Add(RfcConfigParameters.Password, "")  '密码
            parms.Add(RfcConfigParameters.Client, "777")       ' Client
            parms.Add(RfcConfigParameters.Language, "EN")      '登陆语言
            parms.Add(RfcConfigParameters.PoolSize, "5")
            'parms.Add(RfcConfigParameters.MaxPoolSize, "10")
            parms.Add(RfcConfigParameters.PeakConnectionsLimit, "10")
            parms.Add(RfcConfigParameters.IdleTimeout, "600")
            Return parms
        Else
            Return Nothing
        End If
    End Function


    'SAP3.0 连接SAP 
    Function ConnectSAP() As Boolean
        Dim GetSapValue = System.Configuration.ConfigurationManager.AppSettings("Description")
        'Dim ID As IDestinationConfiguration = New SAPRFC()         '继承
        ID = New SAPRFC()
        Try
            If Destination Is Nothing Then
                RfcDestinationManager.RegisterDestinationConfiguration(ID)
                If GetSapValue = "QAS" Then
                    Destination = RfcDestinationManager.GetDestination("qas_000")          '连接SAP, SAPConnector 3.0方式 QAS SYSTEM
                ElseIf GetSapValue = "PRDALL" Then
                    Destination = RfcDestinationManager.GetDestination("prd_000")          '连接SAP, SAPConnector 3.0方式 PRD SYSTEM
                ElseIf GetSapValue = "QA1" Then
                    Destination = RfcDestinationManager.GetDestination("qa1_000")          '连接SAP, SAPConnector 3.0方式 PRD SYSTEM
                End If
                'RfcDestinationManager.UnregisterDestinationConfiguration(ID)
            End If
            Return True
        Catch ex As Exception
            Loger.Error(ex.Message.ToString)
            Return False
        End Try


    End Function


    Public Function GetMaterial(ByVal material As String) As DataTable
        Try
            Destination = Nothing
            SapFunction = Nothing
            mystruct = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("BAPI_MATERIAL_GET_DETAIL")     '建立一个函数对象
                SapFunction.SetValue("MATERIAL", material)                             '传递参数


                SapFunction.Invoke(Destination)                                        '激活提交
                mystruct = SapFunction.GetStructure("MATERIAL_GENERAL_DATA")     '返回表 
            End If
        Catch ex As Exception
            Loger.Error(ex.Message.ToString)
        Finally
            RfcDestinationManager.UnregisterDestinationConfiguration(ID)
            ID = Nothing
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcStruct(mystruct)
    End Function


    Public Function GetPO(ByVal PO As String) As DataTable
        Try
            Destination = Nothing
            SapFunction = Nothing
            mystruct = Nothing
            If ConnectSAP() = True Then
                SapFunction = Destination.Repository.CreateFunction("BAPI_PO_GETDETAIL")     '建立一个函数对象
                SapFunction.SetValue("PURCHASEORDER", PO)                             '传递参数


                SapFunction.Invoke(Destination)                                        '激活提交
                mystruct = SapFunction.GetStructure("PO_HEADER")     '返回表 
            End If
        Catch ex As Exception
            Loger.Error(ex.Message.ToString)
        Finally
            RfcDestinationManager.UnregisterDestinationConfiguration(ID)
            ID = Nothing
            SapFunction = Nothing
            Destination = Nothing
        End Try
        Return GetDataTableFromRfcStruct(mystruct)
    End Function




    Public Function GetDataTableFromRfcTable(ByVal rfcTable As IRfcTable) As DataTable
        Dim dt As New DataTable()
        If rfcTable Is Nothing Then
            dt = Nothing
        Else
            Dim liElement As Integer = 0
            Dim rfcEMD As RfcElementMetadata
            Dim dr As DataRow
            Dim row As IRfcStructure
            For liElement = 0 To rfcTable.ElementCount - 1
                rfcEMD = rfcTable.GetElementMetadata(liElement)
                dt.Columns.Add(rfcEMD.Name)
            Next
            For Each row In rfcTable
                dr = dt.NewRow()
                For liElement = 0 To rfcTable.ElementCount - 1
                    rfcEMD = rfcTable.GetElementMetadata(liElement)
                    dr(rfcEMD.Name) = row.GetString(rfcEMD.Name)
                Next
                dt.Rows.Add(dr)
            Next
        End If
        Return dt
    End Function


    Public Function GetDataTableFromRfcStruct(ByVal rfcStruct As IRfcStructure) As DataTable
        Dim rowTable As New DataTable
        For i As Integer = 0 To rfcStruct.ElementCount - 1
            rowTable.Columns.Add(rfcStruct.GetElementMetadata(i).Name)
        Next


        Dim dr As DataRow = rowTable.NewRow()
        For j As Integer = 0 To rfcStruct.ElementCount - 1
            dr(j) = rfcStruct.GetValue(j)
        Next
        rowTable.Rows.Add(dr)


        Return rowTable
    End Function
End Class
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 我43岁记忆力差怎么办 艾灸灸出的湿疹怎么办 饭店合同到期房东不租怎么办 极端暴力控制不住自己怎么办 苹果已停止访问该网页怎么办 qq登陆后隐藏了怎么办 易班密码忘记了怎么办 老师上课讲错了怎么办 专升本差了一分怎么办 登录不上学信网怎么办 steam被好友删了怎么办 护士继续教育学分证丢了怎么办 护士证到期未延续注册怎么办 学籍和户口不在一起小升初怎么办 定了酒店不能退怎么办 去哪儿网酒店不允许取消怎么办 快递寄送身份证扣海关怎么办 7岁龋齿烂到牙根怎么办 法院判完对方说没钱怎么办 初中填完志愿后怎么办 上海小学借读一年级没有学籍怎么办 学历不高的我该怎么办 没学历的我该怎么办 物业达不到服务标准该怎么办 没有能力的人该怎么办 工作累了腰疼怎么办 机场来早了6小时怎么办 苏宁金融综合评分不足怎么办 苏宁金融秒拒怎么办 微盘账号忘记了怎么办 天府e税忘记密码怎么办 未成年在外面没地方住怎么办? 半框眼镜片掉了怎么办 选修差0.5个学分怎么办 脱产考博社保卡怎么办 幼儿上课不认真听讲怎么办 手机恢复的音频文件打不开怎么办 高考志愿填报不记得密码怎么办 经济纠纷案被告没有证据怎么办 管家婆管理员密码忘记了怎么办 人离职了公司扣发工资怎么办?