现在的位置: 首页 > 综合 > 正文

VB.net连接SAP实例

2012年07月23日 ⁄ 综合 ⁄ 共 1355字 ⁄ 字号 评论关闭

最近在研究vb连接SAP的例子, 终于可以正常登录SAP通过RFC读取SAP中的数据。

下面是具体的代码:

vb6.0写法:

 

'定义公共变量

Public Connect As Object
Public Functions As Object

'登录SAP
Private Sub Command1_Click()

   '创建ocx对象
    Set Functions = CreateObject("Sap.Functions.unicode")

   'Sap.Functions.unicode 不加unicode的话无法正常显示中文。这和登录时Language的设置没有关系

   '是因为字符集的原因。

   '设置连接信息
    Set Connect = Functions.Connection
    Connect.ApplicationServer = "172.18.95.173"
    Connect.Client = "169"
    Connect.Language = "ZH"
    Connect.User = "CRMDEV69"
    Connect.Password = "654321"
    Connect.SystemNumber = 7
    Connect.System = "CD2"
   
    If Not Connect.Logon(0, True) Then
      MsgBox "登录失败"

      Command1.SetFocus
    Else 

      Command1.Enabled = False
      MsgBox "登录成功"

    End If
End Sub

'调用RFC的写法

Private Sub Command2_Click()
  Dim GetCustomers As Object
  Dim Customers As Object
  Dim i As Integer

 '所要调用的RFC名称
 Set GetCustomers = Functions.Add("ZCSMS_GET_HRINFO")

 '传递的输入参数名称并赋值

  GetCustomers.Exports("BEGDAFROM") = ""
  GetCustomers.Exports("BEGDATO") = ""
  GetCustomers.Exports("MILL") = "7960"
  GetCustomers.Exports("NUMBERFROM") = "0061500001"
  GetCustomers.Exports("NUMBERTO") = "0061500080"
 
  '执行后返回的Table,相当于二维数组
  Set Customers = GetCustomers.Tables("THR")

 '简单的MsgBox弹出以查看值 

 If GetCustomers.Call Then
    For i = 1 To Customers.RowCount
      MsgBox Customers(i, "MILL")
      MsgBox Customers(i, "PERNR")
      MsgBox Customers(i, "NAME1")
      MsgBox Customers(i, "STEXT")
    Next i
  Else
    MsgBox " 函数调用失败" + GetCustomers.exception
  End If
End Sub

抱歉!评论已关闭.