Call bapi in Asp.net

Source: Internet
Author: User
Tags rfc
Dataprovidersap. installation:

Run the installation file to install dataprovidersap. MSI. The default installation path is c: \ Program Files \ common files \ microsoft shared \ adapters \ sap \. 2 . Microsoft Visual Studio 2005 Create a project : Add reference Microsoft. Adapter. sap. sapprovider. dll. Microsoft. Adapter. sap. sapprovider. dll can be found in the following installation path: C: \ Program Files \ common files \ microsoft shared \ adapters \ sap \. 3 . C # Call FRC Statement: Using Microsoft. adapter. SAP; // Add reference sapconnection con = new sapconnection ("ashost = 10.1.10.102; client = 200; sysnr = 00; user = ***; passwd = ***; lang = ZH "); // SAP server connection parameter settings, Con. open (); sapcommand cmd = new sapcommand (CON); cmd. commandtext = "Exec bapi_customer_getlist @ idrange = @ Param output"; // execute the remote RFC bapi_customer_getlist, for information about how to pass RFC parameters, see the exec statement syntax below. // assign values to the RFC call parameters and specify Input and Output sapparameter Param = new sapparameter ("@ Param", parameterdirection. inputOutput); datatable dt = new datatable (); DT. columns. add ("sign"); DT. columns. add ("option"); DT. columns. add ("low"); DT. columns. add ("high"); datarow ROW = DT. newrow (); row ["low"] = 1; row ["high"] = 1000; DT. rows. add (ROW); Param. value = DT; cmd. parameters. add (PARAM); // place the execution result in sapdatareade. sapdatareader DR = cmd. executereader (); // retrieving returned datareaders do {console. writeline ("value of returned datareader:" + dr. getschematable (). tablename); While (dr. read () {string line = ""; for (INT I = 0; I <dr. fieldcount; I ++) line = line + "|" + dr. getvalue (I ). tostring (); console. writeline (line) ;}} while (dr. nextresult (); console. writeline ("Checking returned value of parameter @ idrange... "); datatable dt1 = (datatable) Param. value; foreach (datarow row1 in dt1.rows) {string line = ""; for (INT I = 0; I <dt1.columns. count; I ++) line = line + "|" + row1 [I]. tostring (); console. writeline (line );} 4 . Exec Statement syntax The following sections describe how to provide Program Implements the exec statement syntax specification. Note that, in some cases, this syntax is slightly different from the transact-SQL syntax. 4.1 Exec Syntax Exec rfc_name [{value | @ variable [Output]}] [,... n] [@ parameter = {value | @ variable [Output]}] [,... n] [;] where: Rfc_name The name of the function to be executed. Parameter Specify the parameter name defined in the function interface. Value Specify the parameter value. @ Variable Specify the replacement parameter. Use Dbparameter Interface can bind parameter values. Output Specify the sap RFC parameter Output Or InputOutput . 4.2 Process named parameters and unnamed Parameters The following describes how to specify the named parameters and unnamed parameters in the exec query: 1. when specifying parameters, you can specify these parameters by naming them (for example, @ parameter_name = value), or you can only provide values. 2. When you use the default value to define a parameter, you can perform this operation without specifying the parameter. 3. When @ parameter_name = value format is used, parameter names and constants are not provided in the order defined in the function call. 4. The order of unnamed parameters is as follows: O import (input) O Export (output) O table (InputOutput). However, sorting is performed within the parameter type in the order listed above. For example, if you have 6 unnamed parameters, the three parameter types are two, as shown in the following table, the parameter sorting should be Param1 , Param2 , Param3 , Param4 , Param5 , Param6 .
ImportParameters ExportParameters TableParameters
Param1 Param3 Param5
Param2 Param4 Param6
5. When using unnamed parameters, you must specify the values of all parameters (including optional and required parameters. Optional parameters can be ignored only when they appear at the end of the parameter list. You cannot use "default" or space to skip the optional parameter, as shown in the following example:

Exec proc_test_defaults, 'a ';

Use the following syntax to obtain the required results:

Exec proc_test_defaults @ P2 = 'a ';

6. Exec query does not support parameters with the following attributes: O nested structure (including other structures as the field structure ). O nested table. O tables that contain the structure. O contains the table structure. The O field contains the composite string type (for example Sstring Or Rawstring . 7. The following table lists the logical mappings between the RFC parameter types and parameter directions when RFC is executed:
PFCParameter type Query keywords Parameter direction
Import Parameters None Paramdirection. Input
Export Parameters Output Paramdirection. Output
Table Parameters Output/None InputOutput
. The following is a general rule for processing parameters: A. You can specify the parameter value as a constant or use a placeholder in a query to specify the parameter value. B. When using placeholders in a query, you must create Sapparameter Object and add it to the corresponding command object. Then, the placeholder name is passed to the constructor. The direction and value depend on the context. § Input Parameter. do not specify a keyword in the parameter direction in the query. However, you must set Value Field, otherwise the provider will cause an exception. You must not explicitly set Direction Field, because the default setting of the provider is Input . § For other parameters, use the format @ paramname = @ placeholder and explicitly specify Output Keyword. Then, you must add Sapparameter Explicitly set the parameter direction Paramdirection. Output Or Paramdirection. InputOutput . C. The parameter name and placeholder name are case-insensitive. D. Unless the parameters have different directions, their names cannot be repeated in the query. E. The placeholder name cannot be repeated in the query. [Back to Top] 4.3 Exec Statement example · To execute a bapi without input parameters, use the following syntax. Data is returned through the datareader object: · exec bapi_companycode_getlist · to execute an RFC with input parameters, use the following syntax: · exec rfc_customer_get @ name1 = 'toso' · to execute an RFC with the specified input parameter but no parameter name, use the following syntax: · exec rfc_customer_get '*', 'contoso'. to execute an RFC with an input parameter specified as a variable, use the following syntax: · exec rfc_customer_get @ var. In this example, the name must be created @ VaR And explicitly set the value of this parameter (for example, set to "1001"), because the first parameter of rfc_customer_get corresponds to kunnr (customer number ). · To execute an RFC that uses a variable as the input parameter name, use the following syntax: · exec rfc_customer_get @ kunnr = @ var1, @ name1 = 'contoso' you must create @ Var1 Specify the value of sapparameter, and then bind it to the corresponding command object. The default direction of the newly created parameter object is Input . · To execute bapi and return the table as a parameter, use the following syntax: · exec bapi_companycode_getlist @ companycode_list = @ tablevar output. You must create a table named @ Var1 Specify the value of sapparameter, and then bind it to the corresponding command object. The direction of the newly created parameter object should be InputOutput .
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.