C# 登录SAP命令---已安装SAP GUI 640 WIN32
添加引用:SAPLogonCtrl
SAPLogonCtrl.SAPLogonControlClass sapLogon
=
new
SAPLogonCtrl.SAPLogonControlClass();
sapLogon.User = strUser; // Specify user
sapLogon.Password = strPassword; // Then password
sapLogon.Client = strClient; // Client
sapLogon.ApplicationServer = strApplicationServer; // Target server address
sapLogon.Language = strLanguage; // Language code
sapLogon.SystemNumber = iSystemNumber; // SystemNumber
SAPLogonCtrl.Connection sapConn = (SAPLogonCtrl.Connection)sapLogon.NewConnection();
if ( ! sapConn.Logon( 0 , true ))
{
Msg = "Cannot Log on to SAP"; //Issue message if cannot logon
return false;
}
sapLogon.User = strUser; // Specify user
sapLogon.Password = strPassword; // Then password
sapLogon.Client = strClient; // Client
sapLogon.ApplicationServer = strApplicationServer; // Target server address
sapLogon.Language = strLanguage; // Language code
sapLogon.SystemNumber = iSystemNumber; // SystemNumber
SAPLogonCtrl.Connection sapConn = (SAPLogonCtrl.Connection)sapLogon.NewConnection();
if ( ! sapConn.Logon( 0 , true ))
{
Msg = "Cannot Log on to SAP"; //Issue message if cannot logon
return false;
}
登录进去了,但是不知如何调用(不用SAP.Connector 2.0),网上也找不到相关信息,只能到此为止。。。
C# 不行,那只能用VB.Net写类似Function了,打包成dll,在C#中调用。
以下是VB.Net调用C#例子:
Public
Class
QMSToSAP
Public strPostNO As String
Public strErrorMsg As String
Private strUser As String
Private strPassword As String
Private strClient As String
Private strApplicationServer As String
Private strLanguage As String
Private iSystemNumber As Integer
Public Sub New ( ByVal user As String , ByVal password As String , ByVal client As String , _
ByVal applicationServer As String , ByVal language As String , ByVal systemNumber As Integer )
strUser = user
strPassword = password
strClient = client
strApplicationServer = applicationServer
strLanguage = language
iSystemNumber = systemNumber
End Sub
Public Function QMStoSAP( ByVal dt As DataTable) As Boolean
QMStoSAP = False
Dim sapConn As Object ' 'Declare variant
sapConn = CreateObject ( " SAP.Functions " ) ' 'Create ActiveX object
' 'Specify user
sapConn.Connection.user = strUser
' 'Then password
sapConn.Connection.Password = strPassword
' 'Client
sapConn.Connection.Client = strClient
' 'Target server address
sapConn.Connection.ApplicationServer = strApplicationServer
' 'Language code
sapConn.Connection.Language = strLanguage
' 'SystemNumber
sapConn.Connection.SystemNumber = iSystemNumber
If sapConn.Connection.Logon( 0 , True ) <> True Then
QMStoSAP = False
MsgBox ( " Cannot Log on to SAP " ) ' 'Issue message if cannot logon
Exit Function
End If
' Define function
Dim objRfcFunc As Object
objRfcFunc = sapConn.Add( " ZRFC_WO_AUTO_GR " )
Dim objVBPA As Object
objVBPA = objRfcFunc.Tables( " ITABIN " )
strErrorMsg = ""
strPostNO = ""
' set value for shipemnt
For Each dr As DataRow In dt.Rows
objVBPA.Rows.Add()
objVBPA(objVBPA.RowCount, " ZPALLETID " ) = Trim (dr( " ZPALLETID " ))
objVBPA(objVBPA.RowCount, " PLANT " ) = Trim (dr( " PLANT " ))
objVBPA(objVBPA.RowCount, " COSTCENTER " ) = Trim (dr( " COSTCENTER " ))
objVBPA(objVBPA.RowCount, " WO " ) = Trim (dr( " WO " ))
objVBPA(objVBPA.RowCount, " QTY " ) = Trim (dr( " QTY " ))
objVBPA(objVBPA.RowCount, " MATERIAL " ) = Trim (dr( " MATERIAL " ))
objVBPA(objVBPA.RowCount, " REGION " ) = Trim (dr( " REGION " ))
objVBPA(objVBPA.RowCount, " LOCA " ) = Trim (dr( " LOCA " ))
objVBPA(objVBPA.RowCount, " REF1 " ) = Trim (dr( " REF1 " ))
objVBPA(objVBPA.RowCount, " REF2 " ) = Trim (dr( " REF2 " ))
Next
If objRfcFunc.Call = False Then
sapConn.Connection.Logoff()
QMStoSAP = False
MsgBox ( " Error occured - " & objRfcFunc.Exception)
Exit Function
End If
' 'VB6.0 中的写法:strPostNO = objRfcFunc.Imports("POSTNO")
' '在.Net中是不行的, 因.Net是强类型语言
strPostNO = objRfcFunc.Imports( " POSTNO " ).Value
strErrorMsg = objRfcFunc.Imports( " ERRORMSG " ).Value
sapConn.Connection.Logoff()
If Trim (strPostNO) <> "" Then
QMStoSAP = True
Else
QMStoSAP = False
End If
End Function
End Class
Public strPostNO As String
Public strErrorMsg As String
Private strUser As String
Private strPassword As String
Private strClient As String
Private strApplicationServer As String
Private strLanguage As String
Private iSystemNumber As Integer
Public Sub New ( ByVal user As String , ByVal password As String , ByVal client As String , _
ByVal applicationServer As String , ByVal language As String , ByVal systemNumber As Integer )
strUser = user
strPassword = password
strClient = client
strApplicationServer = applicationServer
strLanguage = language
iSystemNumber = systemNumber
End Sub
Public Function QMStoSAP( ByVal dt As DataTable) As Boolean
QMStoSAP = False
Dim sapConn As Object ' 'Declare variant
sapConn = CreateObject ( " SAP.Functions " ) ' 'Create ActiveX object
' 'Specify user
sapConn.Connection.user = strUser
' 'Then password
sapConn.Connection.Password = strPassword
' 'Client
sapConn.Connection.Client = strClient
' 'Target server address
sapConn.Connection.ApplicationServer = strApplicationServer
' 'Language code
sapConn.Connection.Language = strLanguage
' 'SystemNumber
sapConn.Connection.SystemNumber = iSystemNumber
If sapConn.Connection.Logon( 0 , True ) <> True Then
QMStoSAP = False
MsgBox ( " Cannot Log on to SAP " ) ' 'Issue message if cannot logon
Exit Function
End If
' Define function
Dim objRfcFunc As Object
objRfcFunc = sapConn.Add( " ZRFC_WO_AUTO_GR " )
Dim objVBPA As Object
objVBPA = objRfcFunc.Tables( " ITABIN " )
strErrorMsg = ""
strPostNO = ""
' set value for shipemnt
For Each dr As DataRow In dt.Rows
objVBPA.Rows.Add()
objVBPA(objVBPA.RowCount, " ZPALLETID " ) = Trim (dr( " ZPALLETID " ))
objVBPA(objVBPA.RowCount, " PLANT " ) = Trim (dr( " PLANT " ))
objVBPA(objVBPA.RowCount, " COSTCENTER " ) = Trim (dr( " COSTCENTER " ))
objVBPA(objVBPA.RowCount, " WO " ) = Trim (dr( " WO " ))
objVBPA(objVBPA.RowCount, " QTY " ) = Trim (dr( " QTY " ))
objVBPA(objVBPA.RowCount, " MATERIAL " ) = Trim (dr( " MATERIAL " ))
objVBPA(objVBPA.RowCount, " REGION " ) = Trim (dr( " REGION " ))
objVBPA(objVBPA.RowCount, " LOCA " ) = Trim (dr( " LOCA " ))
objVBPA(objVBPA.RowCount, " REF1 " ) = Trim (dr( " REF1 " ))
objVBPA(objVBPA.RowCount, " REF2 " ) = Trim (dr( " REF2 " ))
Next
If objRfcFunc.Call = False Then
sapConn.Connection.Logoff()
QMStoSAP = False
MsgBox ( " Error occured - " & objRfcFunc.Exception)
Exit Function
End If
' 'VB6.0 中的写法:strPostNO = objRfcFunc.Imports("POSTNO")
' '在.Net中是不行的, 因.Net是强类型语言
strPostNO = objRfcFunc.Imports( " POSTNO " ).Value
strErrorMsg = objRfcFunc.Imports( " ERRORMSG " ).Value
sapConn.Connection.Logoff()
If Trim (strPostNO) <> "" Then
QMStoSAP = True
Else
QMStoSAP = False
End If
End Function
End Class
成功调用!!!