RFC访问SAP(C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SAPFunctionsOCX;
using SAPLogonCtrl;
using SAPTableFactoryCtrl;
namespace SAPFunction
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void BtnConn_Click(object sender, EventArgs e)
{
//设置登录连接的类
DataSet dset = new DataSet();
SAPLogonCtrl.SAPLogonControlClass logon = new SAPLogonCtrl.SAPLogonControlClass();
//连接参数设置
logon.ApplicationServer = "172.18.95.173";
logon.Client = "164";
logon.Language = "ZH";
logon.User = "CRMDEV69";
logon.Password = "654321";
logon.SystemNumber = 7;
//实例化连接对象
SAPLogonCtrl.Connection Conn;
Conn = (SAPLogonCtrl.Connection)logon.NewConnection();
//Conn.CodePage = "8400";
//调用登录方法
if (Conn.Logon(0, true))
{
label1.Text = "登录成功!";
}
//实例SAPFunction对象
SAPFunctionsOCX.SAPFunctionsClass func = new SAPFunctionsClass();
//设置连接
func.Connection = Conn;
SAPFunctionsOCX.IFunction ifunc = (SAPFunctionsOCX.IFunction)func.Add("ZCSMS_GET_HRINFO");
//设置输入参数
SAPFunctionsOCX.IParameter BEGDAFROM = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("BEGDAFROM");
BEGDAFROM.Value = "";
SAPFunctionsOCX.IParameter BEGDATO = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("BEGDATO");
BEGDATO.Value = "";
SAPFunctionsOCX.IParameter MILL = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("MILL");
MILL.Value = "7960";
SAPFunctionsOCX.IParameter NUMBERFROM = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("NUMBERFROM");
NUMBERFROM.Value = "0061500001";
SAPFunctionsOCX.IParameter NUMBERTO = (SAPFunctionsOCX.IParameter)ifunc.get_Exports("NUMBERTO");
NUMBERTO.Value = "0061500020";
//调用RFC方法
ifunc.Call();
SAPTableFactoryCtrl.Tables ENQs = (SAPTableFactoryCtrl.Tables)ifunc.Tables;
SAPTableFactoryCtrl.Table ENQ = (SAPTableFactoryCtrl.Table)ENQs.get_Item("THR");
int j = ENQ.RowCount;
DataTable dt = new DataTable();
for (int i = 1 ;i<=ENQ.RowCount; i ++)
{
DataRow dr = dt.NewRow();
if (i == 1)
{
dt.Columns.Add("MILL");
dt.Columns.Add("PERNR");
dt.Columns.Add("NAME1");
dt.Columns.Add("STEXT");
}
dr["MILL"] = (String)ENQ.get_Value(i, "MILL");
dr["PERNR"] = (String)ENQ.get_Value(i, "PERNR");
dr["NAME1"] = (String)ENQ.get_Value(i, "NAME1");
dr["STEXT"] = (String)ENQ.get_Value(i, "STEXT");
dt.Rows.Add(dr);
}
dataGridView1.DataSource = dt.DefaultView;
}
}
}
当然, RFC连接SAP只是其中的方法之一, BAPI等多种方式.