RFC access to SAP (C #)

Source: Internet
Author: User
Tags rfc

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.