[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using SAP. Middleware. Connector;
Using System. Data;
Namespace SAPMVC. Controllers
{
Public class HomeController: Controller
{
String MATNR = string. Empty;
Public ActionResult Index ()
{
ViewBag. Message = "Welcome to ASP. net mvc! ";
Nco ();
// Nco2 ();
Return View ();
}
Public void nco2 ()
{
RfcConfigParameters rfcPar = new RfcConfigParameters ();
// RfcPar. Add (RfcConfigParameters. Name, "CON ");
// RfcPar. Add (RfcConfigParameters. AppServerHost, "192.168.1.3 ");
// RfcPar. Add (RfcConfigParameters. Client, "800 ");
// RfcPar. Add (RfcConfigParameters. User, "UserID ");
// RfcPar. Add (RfcConfigParameters. Password, "Password ");
// RfcPar. Add (RfcConfigParameters. SystemNumber, "02 ");
// RfcPar. Add (RfcConfigParameters. Language, "EN ");
RfcPar. Add (RfcConfigParameters. Name, "CON ");
RfcPar. Add (RfcConfigParameters. AppServerHost, "192.168.1.3"); // IP address of the SAP host
RfcPar. Add (RfcConfigParameters. SystemNumber, "00"); // SAP instance
RfcPar. Add (RfcConfigParameters. User, "MENGXIN"); // User Name
RfcPar. Add (RfcConfigParameters. Password, "5239898"); // Password
RfcPar. Add (RfcConfigParameters. Client, "888"); // Client
// RfcPar. Add (RfcConfigParameters. Language, "ZH"); // login Language
// RfcPar. Add (RfcConfigParameters. PoolSize, "5 ");
// RfcPar. Add (RfcConfigParameters. MaxPoolSize, "10 ");
// RfcPar. Add (RfcConfigParameters. IdleTimeout, "60 ");
RfcDestination dest = RfcDestinationManager. GetDestination (rfcPar );
RfcRepository rfcrep = dest. Repository;
IRfcFunction myfun = null;
Myfun = rfcrep. CreateFunction ("function name in SAP ");
Myfun. SetValue ("VTYPE", "0"); // input parameters in SAP
Myfun. Invoke (dest );
IRfcTable IrfTable = myfun. GetTable ("IT_ZMYTB2 ");
// Instantiate an empty table structure in advance
DataTable dt = new DataTable ();
Dt. Columns. Add ("USERID ");
Dt. Columns. Add ("USERPWD ");
Dt. Columns. Add ("USERADDRESS ");
// Cyclically place the data in IRfcTable into the Table. It cannot be directly used because of different types.
For (int I = 0; I <IrfTable. Count; I ++)
{
IrfTable. CurrentIndex = I;
DataRow dr = dt. NewRow ();
Dr ["USERID"] = IrfTable. GetString ("USERID ");
Dr ["USERPWD"] = IrfTable. GetString ("USERPWD ");
Dr ["USERADDRESS"] = IrfTable. GetString ("USERADDRESS ");
Dt. Rows. Add (dr );
}
}
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;
}
Public ActionResult About ()
{
Return View ();
}
// 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. Name, "PRD_000 ");
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;
}
}
}