• VB.NET 与 SAP RFC连接问题点


    与SAP RFC连接,电脑上必须要安装SAP软件,否则会报错ActiveX

    输入工单号,无法带出SAP内接口RFC信息。

    确认原因为:RFC接口需求的工单参数需要在前面加两位00,例如:1000541078(10个字符)改为 001000541078(12个字符),解决办法:使用string.PadLeft(12, "0")函数,自动补全左边两位00

    RFC调用方法案例:

      Public SAPojb As Object
        Public SAPConn As Object
        Public StrSAPIP As String = ""
        Public StrSAPClient As String = ""
        Public StrSAPNo As String = ""
        Public StrSAPAcc As String = ""
        Public StrSAPPwd As String = ""
        Public ObjSAPFunc As Object
        Public theFunc As Object
    
        Dim R_AUFNR As String = ""
        Dim R_NUM As String = ""
    
     Private Sub MMF02_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadSAPpara()    '加载SAP
        End Sub
    
      Public Sub SAPRFCClose()    '关闭SAP
            SAPConn = Nothing
        End Sub
    
     Public Function LoadSAPpara() As Boolean    '加载SAP
            Try
                Dim file As String = "c:TMISsaprfc.ini"
                Dim Input As String = ""
                Using sr As IO.StreamReader = IO.File.OpenText(file)
                    Input = sr.ReadLine()
                    Input = UCase(Input)
                    While sr.Peek > -1 Or Len(Input) > 0
                        If InStr(Input, "SAPIP") > 0 Then
                            StrSAPIP = Trim(Mid(Input, InStr(Input, "=") + 1))
                        End If
    
                        If InStr(Input, "SAPCLIENT") > 0 Then
                            StrSAPClient = Trim(Mid(Input, InStr(Input, "=") + 1))
                        End If
    
                        If InStr(Input, "SAPNO") > 0 Then
                            StrSAPNo = Trim(Mid(Input, InStr(Input, "=") + 1))
                        End If
    
                        If InStr(Input, "SAPACC") > 0 Then
                            StrSAPAcc = Trim(Mid(Input, InStr(Input, "=") + 1))
                        End If
    
                        If InStr(Input, "SAPPWD") > 0 Then
                            StrSAPPwd = Trim(Mid(Input, InStr(Input, "=") + 1))
                        End If
    
                        Input = sr.ReadLine()
                        Input = UCase(Input)
                    End While
                End Using
                Return True
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
            End Try
        End Function
    
     Public Function SAPRFCLink() As Boolean    
            Dim NonErr As Boolean = False
            SAPojb = CreateObject("SAP.LogonControl.1")
            SAPConn = SAPojb.NewConnection
            Try
                SAPConn.applicationserver = StrSAPIP
                SAPConn.client = StrSAPClient
                SAPConn.user = StrSAPAcc
                SAPConn.password = StrSAPPwd
                SAPConn.language = "EN"
                SAPConn.CodePage = "8300"
    
                If SAPConn.logon(0, True) <> True Then
                    NonErr = False
                Else
                    NonErr = True
                End If
    
            Catch er As Exception
                MsgBox(er.ToString)
            End Try
    
            Return NonErr
        End Function
    
    Private Sub txtWkNo_KeyDown(sender As Object, e As KeyEventArgs) Handles txtWkNo.KeyDown
            Dim str As String = ""
            Dim str2 As String = ""
            Dim RNUM As Double = 0
            R_AUFNR = txtWkNo.Text.Trim
            If e.KeyCode = Keys.Enter Then
                If SAPRFCLink() Then
                    MsgBox("SAP连接成功")
                    ObjSAPFunc = CreateObject("SAP.FUNCTIONS")
                    ObjSAPFunc.Connection = SAPConn
                    theFunc = ObjSAPFunc.add("Z_SFC_WOSMT")
                    theFunc.EXPORTS("P_AUFNR") = R_AUFNR.PadLeft(12, "0")
    
                    If theFunc.call Then
                        MsgBox("OK,RFC有回传资料")
                        str = theFunc.imports("R_AUFNR").value.ToString()
                        str2 = theFunc.imports("R_NUM").value.ToString()
                        RNUM = Convert.ToDouble(str2) - 2
                    End If
                Else
                    MsgBox("SAP连接错误,请检查SAP")
                End If
                lbSAP.Text = "工单:" & R_AUFNR  & ",数量:" & RNUM
                SAPRFCClose()
            End If
        End Sub
    

      SAPRFC.ini配置如下:

    [SAPRFC]
    SAPIP=10.10.10.96
    SAPClient=888
    SAPNo=00
    SAPAcc=RFC
    SAPPwd=123456

    本文来自博客园,作者:云辰,转载请注明原文链接:https://www.cnblogs.com/yunchen/p/11820072.html

  • 相关阅读:
    ActiveMQ 5.x 消息队列
    Spring Boot 整合 ElasticSearch 框架
    Spring Boot 整合 Logback 日志框架
    Spring Boot 整合定时任务和异步任务处理
    Spring 中使用 Java 5.0 Executor
    二级指针三种内存模型综合训练
    08-图8 How Long Does It Take (25 分)
    08-图9 关键活动 (30 分)
    08-图7 公路村村通 (30 分)
    C函数之index、strtoul
  • 原文地址:https://www.cnblogs.com/yunchen/p/11820072.html
Copyright © 2020-2023  润新知