C # uses SAP NCO3.0 to invoke the SAP RFC function interface
Recently, the SAP RFC function was called using C #, and SAP provided the NCO3.0 component.
Download the component installation, and then refer to the "Sapnco.dll" and "Sapnco_utils.dll" two files.
Using SAP in the program. Middleware.connector;
See the following code specifically
Registering a client connection using the App. Config file configuration
<?xml version= "1.0"?><configuration> <configSections> <sectiongroup name= "SAP. Middleware.connector "> <sectiongroup name=" clientsettings "> <section name=" destinationconfiguratio N "type=" SAP. Middleware.connector.rfcdestinationconfiguration,sapnco "/> </sectionGroup> </sectionGroup> </co Nfigsections> <sap. middleware.connector> <ClientSettings> <DestinationConfiguration> <destinations> <add name= "Conn" user= "ky_pg01" passwd= "[email protected]" client= "002" sysnr= "ten" ashost= "192.168.0.22" LANG = "ZH" group= "Public" max_pool_size= "5" ></add> </destinations> </DestinationConfiguration> </ClientSettings> </sap. Middleware.connector></configuration>
Private Rfcdestination _rfcdestination = null;public DataTable DTR = new DataTable ();p ublic void registerdestination ()// Register Client {try {if (_rfcdestination = = null) { _rfcdestination = Rfcdestinationmanager.getdestination ("Conn"); }} catch (Exception ex) {MessageBox.Show (ex). Message); }} public string Invokerfcfunctiongetcompanyid (string datebegin, String dateend) {Irfcfunction function = null; String str = string. Empty; try {function = _rfcdestination.repository.createfunction ("zky_fm_zm005b");//Call server function function. SetValue ("So_fkdat_b", datebegin);//incoming parameter function. SetValue ("So_fkdat_e", dateend);//incoming parameter function. Setparameteractive (0, true); function. Invoke (_rfcdestination);//The function that executes the server call IRFCTable myrfctable = function. GetTable ("it_zm005b");//RFC server function return value table struct name int lielement = 0; for (lielement = 0; lielement <= myrfctable.elementcount-1; lielement++) {Rfceleme Ntmetadata metadata = Myrfctable.getelementmetadata (lielement); Dtr. Columns.Add (metadata. Name);//Loop Create column} foreach (Irfcstructure dr in myrfctable)//Cyclic table structure Table { DataRow row = DTR. NewRow ();//Create New line for (lielement = 0; lielement <= myrfctable.elementcount-1; lielement++) {Rfcelementmetadata metadata = Myrfctable.getelementmetadata (lielement); Row[metadata. Name] = Dr. GetString (metadata. Name). Trim (); } DTR. Rows.Add (row); } This.dataGridView1.DataSource = DTR; } catch (Exception ex) {MessageBox.Show (ex. ToString ()); } return str; }//call this in an event or method. Registerdestination (); This. Invokerfcfunctiongetcompanyid ("20120401", "20120901");
C # calls the SAP RFC function interface using SAP NCO3.0