. Net connection to SAP system topic: C # Call RFC code (3)

Source: Internet
Author: User
Tags rfc

This article describes how to write code in C # To Call The RFC function in SAP to obtain data. (Winform32)

First, you must reference two nco3.0 DLL files.

Then reference it on the program code page:

Using SAP. Middleware. connector;

Then all the code is as follows:

Namespace sap_rfc

{

Public partial class form1: Form

{

String matnr = string. empty;

Public form1 ()

{

Initializecomponent ();

}

Public void NCO ()

{

Idestinationconfiguration id = new mybackendconfig ();

Rfcdestinationmanager. registerdestinationconfiguration (ID );

Rfcdestination PRD = rfcdestinationmanager. getdestination ("prd_000 ");

Rfcdestinationmanager. unregisterdestinationconfiguration (ID );

NCO (PRD );

}

Public void NCO (rfcdestination PRD)

{

Rfcrepository repo = PRD. repository;

Irfcfunction companybapi = Repo. createfunction ("zrfc_mara_info"); // call the function name

Companybapi. setvalue ("matnr", matnr); // you can specify the import parameter.

Companybapi. Invoke (PRD); // executes the Function

Irfctable table = companybapi. gettable ("it_mara"); // obtain the table in the corresponding product number.

String maktx = companybapi. getvalue ("maktx"). tostring (); // get the product name

Datatable dt = new datatable (); // create a table

DT. Columns. Add ("product number"); // Add a column to the table

For (INT I = 0; I <Table. rowcount; I ++)

{

Table. currentindex = I; // index row of the current inner table

Datarow DR = DT. newrow ();

Dr [0] = table. getstring ("matnr"); // obtain the value of a column in a table row.

DT. Rows. Add (DR); // fill in the Table value

}

If (matnr = "")

{

For (INT I = 0; I <DT. Rows. Count; I ++)

{

This. combobox1.items. Add (Dt. Rows [I] [0]. tostring (); // fill in the drop-down box

}

}

This. label1.text = maktx; // display item name

PRD = NULL;

Repo = NULL;

}

// Preparations before logging on to sap

Public class mybackendconfig: idestinationconfiguration

{

Public rfcconfigparameters getparameters (string destinationname)

{

If ("prd_000". Equals (destinationname ))

{

Rfcconfigparameters parms = new rfcconfigparameters ();

Parms. Add (rfcconfigparameters. appserverhost, "192.168.1.3"); // ip address of the SAP host

Parms. Add (rfcconfigparameters. systemnumber, "00"); // sap instance

Parms. Add (rfcconfigparameters. User, "mengxin"); // User Name

Parms. Add (rfcconfigparameters. password, "5239898"); // Password

Parms. Add (rfcconfigparameters. Client, "888"); // Client

Parms. Add (rfcconfigparameters. Language, "ZH"); // login Language

Parms. Add (rfcconfigparameters. poolsize, "5 ");

Parms. Add (rfcconfigparameters. maxpoolsize, "10 ");

Parms. Add (rfcconfigparameters. idletimeout, "60 ");

Return parms;

}

Else return NULL;

}

Public bool changeeventssupported ()

{

Return false;

}

Public event rfcdestinationmanager. configurationchangehandler configurationchanged;

}

Private void form1_load (Object sender, eventargs E)

{

Combobox1.items. Clear ();

NCO ();

Combobox1.selectedindex = 1;

}

// When the index of the drop-down box changes, pass the product number to query the product name.

Private void combobox#selectedindexchanged (Object sender, eventargs E)

{

Matnr = combobox1.text. tostring ();

NCO ();

}

}

}

I think this C # code is very simple and I will not detail it in detail. The result is as follows:

The product numbers in SAP are as follows:

It can be seen that the data is completely OK and the call is successful.

The program is a little slow when it is loaded for the first time. It is linked to sap and logged on. When the drop-down box changes, the product name is immediately displayed, without any pause. The second link to sap probably does not need to log on, SAP system has login information, run T-CODE: sm04

In the red box, these two are the logon sessions left by our RFC call. Once our C # program exits, the two RFC also exits.

If our C # program is ASP. NET, the RFC logon information is still available after the page is closed. Unless IIS is disabled, you must wait until the SAP system times out to exit these two login sessions.

DLL:

Http://files.cnblogs.com/mengxin523/SAP_DotNetConnector3.zip

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.