C # calls the SAP RFC interface

Source: Internet
Author: User
Tags rfc

1, install the SAP. NET Connector 3.0 program, provided in the appendix

Pay attention to your choice of environment:

Compiled with. NET Framework 2.0:
Sapnco30p_8-20007347.zip:sap Connector for Microsoft. NET 3.0.8.0 for Windows 32bit (x86)
Sapnco30p_8-20007348.zip:sap Connector for Microsoft. NET 3.0.8.0 for Windows 64bit (x64)


Compiled with. NET Framework 4.0
Sapnco30dotnet40p_8-20007347.zip:sap Connector for Microsoft. NET 3.0.8.0 for Windows 32bit (x86)
Sapnco30dotnet40p_8-20007348.zip:sap Connector for Microsoft. NET 3.0.8.0 for Windows 64bit (x64)

2, in the process of referencing Sapnco.dll, Sapnco_utils.dll

For example: C:\Program Files (x86) \sap\sap_dotnetconnector3_x64/(based on their own installation)


3, part of the code


Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
Using SAP. Middleware.connector;


Namespace WindowsApplication1
{
public partial class Form1:form
{
String Matnr = String. Empty;


Public Form1 ()
{


InitializeComponent ();


}


public void Execute ()
{


Idestinationconfiguration ID = new Mybackendconfig ();


Rfcdestinationmanager.registerdestinationconfiguration (ID);
Login
Rfcdestination PRD = rfcdestinationmanager.getdestination ("my_asp"); The name is defined for yourself, and the attention is the same as the mybackendconfig.
Execute (PRD);
Exit Login
Rfcdestinationmanager.unregisterdestinationconfiguration (ID);


}


public void Execute (Rfcdestination PRD)
{


Rfcrepository repo = PRD. Repository;


Irfcfunction Companybapi = Repo.   Createfunction ("Sap_function_name"); Call Function name


Companybapi.setvalue ("param1", "value1"); Set parameters for import
Companybapi.setvalue ("param2", "value2"); Set parameters for import



Companybapi.invoke (PRD); Execution function


irfctable table = companybapi.gettable ("out_table");//This is set according to its own parameters

Declares a field that is placed in a database to enable dynamic setting of fields
string[] columns = new string[]{
"Clumn1",
"Clumn2",
"Clumn3",
"Clumn4",
"Clumn5"
};
DataTable dt = new DataTable (); Create a new table
foreach (string clmn in columns)
{
Dt. Columns.Add (CLMN);
}
for (int i = 0; i < table. ROWCOUNT; i++)
{
DataRow dr = dt. NewRow ();
foreach (string clmn in columns)
{
DR[CLMN] = table. GetString (CLMN);
}
Dt.  Rows.Add (DR); Fill the value of the table
}
This.dataGridView1.DataSource = DT;
PRD = null;
Repo = null;


}


Pre-landing SAP preparation work


public class Mybackendconfig:idestinationconfiguration
{


Public rfcconfigparameters getparameters (String destinationname)
{


if ("My_sap"). Equals (Destinationname))
{


Rfcconfigparameters parms = new Rfcconfigparameters ();


Parms.   ADD (Rfcconfigparameters.appserverhost, "10.10.10.2*"); SAP Host IP


Parms.  ADD (Rfcconfigparameters.systemnumber, "2*"); SAP instance


Parms.  ADD (Rfcconfigparameters.user, "username"); User name


Parms.  ADD (Rfcconfigparameters.password, "Password"); Password


Parms.  ADD (rfcconfigparameters.client, "800"); Client


Parms.  ADD (rfcconfigparameters.language, "en"); Landing language


Parms. ADD (Rfcconfigparameters.poolsize, "5");


Parms. ADD (Rfcconfigparameters.idletimeout, "60");




return parms;


}


Else
return null;


}


public bool Changeeventssupported ()
{


return false;


}


public event Rfcdestinationmanager.configurationchangehandler Configurationchanged;


}
private void Button1_Click (object sender, EventArgs e)
{
Execute ();
}
}
}


Related Article

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.