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)
{
// Set the login connection class
Dataset dset = new dataset ();
Saplogonctrl. saplogoncontrolclass logon = new saplogonctrl. saplogoncontrolclass ();
// Connection parameter settings
Logon. applicationserver = "172.18.95.173 ";
Logon. Client = "164 ";
Logon. Language = "ZH ";
Logon. User = "crmdev69 ";
Logon. Password = "654321 ";
Logon. systemnumber = 7;
// Instantiate the connection object
Saplogonctrl. Connection conn;
Conn = (saplogonctrl. Connection) logon. newconnection ();
/Conn. codePage = "8400 ";
// Call the logon Method
If (conn. Logon (0, true ))
{
Label1.text = "Logon successful! ";
}
// Instance sapfunction object
Sapfunctionsocx. sapfunctionsclass func = new sapfunctionsclass ();
// Set the connection
Func. Connection = conn;
Sapfunctionsocx. ifunction ifunc = (sapfunctionsocx. ifunction) func. Add ("zcsms_get_hrinfo ");
// Set input parameters
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 ";
// Call the RFC Method
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;
}
}
}
Of course, connecting to sap using RFC is only one of the methods, and there are multiple methods such as bapi.