VB.NET連接SAP實際應用方法介紹
作者:佚名
VB.NET連接SAP的相關方法將會在這篇文章中進行詳細的介紹。希望初學者們能夠通過我們介紹的內容詳細掌握其中的應用技巧。
大家可能對于VB.NET的開發(fā)能力非常感興趣。因為它的應用能夠幫助我們帶來一個良好的功能強大的開發(fā)平臺。VB.NET連接SAP中只是把讀取出來的內容存放在數據庫中,然后利用GridView顯示出來。 當然可以直接存入DataTable或DataSet中直接顯示出來。#t#
以下見代碼示例:
- Imports System
- Imports System.Collections.Generic
- Imports System.ComponentModel
- Imports System.Data
- Imports System.Drawing
- Imports System.Text
- Imports System.Windows.Forms
- Imports System.Data.OleDb
- Imports System.Xml
- Public Class SAPConn
- Public oFunction As Object ' SAP Functions
- Public oConnection As Object ' SAP oConnection
- Dim cmd As OleDbCommand
- Dim SqlAd As OleDbDataAdapter
- Dim sql As String
- '測試連接的代碼
- Private Sub BtnConnn_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles BtnConnn.Click - Try
- oFunction = CreateObject("SAP.Functions.unicode")
- oConnection = oFunction.Connection
- oConnection.User = "CRMDEV69"
- oConnection.Password = "654321"
- oConnection.System = "CD2"
- oConnection.ApplicationServer = "172.18.95.173"
- oConnection.SystemNumber = 7
- oConnection.Client = "164"
- oConnection.Language = "ZH"
- If oConnection.Logon(0, True) = True Then
- MsgBox("連接成功!")
- Else
- MsgBox("連接失??!")
- End If
- Catch ex As Exception
- MsgBox(ex.ToString(), MsgBoxStyle.Information, "提示")
- Return
- End Try
- End Sub
- Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles Button1.Click - Dim GetCustomers As Object
- Dim Customers As Object
- Dim i As Integer
- Dim sqlstr As String = ""
- ' 通過RFC接口遠程運行SAP內部函數ZCSMS_GET_HRINFO
- ' 賦要調用的SAP內建函數名
- Try
- GetCustomers = oFunction.Add("ZCSMS_GET_HRINFO")
- '設置輸入參數并賦值
- GetCustomers.Exports("BEGDAFROM") = ""
- GetCustomers.Exports("BEGDATO") = ""
- GetCustomers.Exports("MILL") = "7960"
- GetCustomers.Exports("NUMBERFROM") = "0061500001"
- GetCustomers.Exports("NUMBERTO") = "0061500200"
- Customers = GetCustomers.Tables("THR")
- If GetCustomers.Call Then
- '循環(huán)插入到數據庫表中
- For i = 1 To Customers.RowCount
- sqlstr = "Insert into ghy_employee(MILL,
PERNR, NAME1, STEXT) values ('" & Customers
(i, "MILL") & "','" & Customers(i, "PERNR")
& "','" & Customers(i, "NAME1") & "','"
& Customers(i, "STEXT") & "' )" - Config.ExecAccess(sqlstr)
- Next i
- MsgBox("獲取數據成功")
- Else
- MsgBox(" 搜索出錯! 出錯信息: " +
GetCustomers.exception) - End If
- Catch ex As Exception
- MsgBox(ex.ToString)
- Return
- End Try
- End Sub
- '通過GridView顯示數據
- Private Sub Button2_Click(ByVal sender As System.
Object, ByVal e As System.EventArgs) Handles
Button2.Click - sql = "select * from ghy_employee "
- SqlAd = New OleDbDataAdapter(sql, oConn)
- DS.Clear()
- If DS.Tables.Contains("ghy_employee") Then
- DS.Tables.Remove("ghy_employee")
- End If
- SqlAd.Fill(DS, "ghy_employee")
- DvInvoice.DataSource = DS.Tables("ghy_employee").
DefaultView - DvInvoice.Refresh()
- DvInvoice.ClearSelection()
- DvInvoice.Columns("MILL").HeaderText = "工廠"
- DvInvoice.Columns("PERNR").HeaderText = "員工編號"
- DvInvoice.Columns("NAME1").HeaderText = "員工姓名"
- DvInvoice.Columns("STEXT").HeaderText = "員工部門"
- End Sub
- End Class
以上兩種寫法都是利用創(chuàng)建組件OCX的方式進行, 通過調用類的方法進行也可以實現。 缺點是中文無法正常顯示。
責任編輯:曹凱
來源:
博客園