Need Help at Getting data from SAP to C#

2019-08-08 03:05发布

问题:

I want to get some data from an SAP Server to my C# Programm in Visual Studio. The connection between the SAP and the program is already working but I don't really know how to get the data from the Server into the program.

namespace ConsoleApp1
{
    class Program //: IDestinationConfiguration
    {
        static void Main(string[] args)
        {
            fn f = new fn();

            RfcDestination destination = f.GetRfcDestination("vhcalnplci", "brachi", "Abap2017", "001", "EN", "vhcalnplci", "00", "20", "10");
            RfcSessionManager.BeginContext(destination);
            destination.Ping();

            IRfcFunction function = destination.Repository.CreateFunction("/BODS/RFC_READ_TABLE");

            IRfcTable addresses = function["ADDRESSES"].GetTable();
            Console.WriteLine("STREET");
            for (int index = 0;
            index < addresses.RowCount;
            ++index)
            {
                Console.WriteLine(addresses[index]["STREET"].GetString());
            }
        }
    }
}

As you can see I have already written some code but unfortunately when I am trying to execute this program i get the following error:

SAP.Middleware.Connector.RfcInvalidParameterException: "Element ADDRESSES of container metadata /BODS/RFC_READ_TABLE unknown"

At the following line of code:

IRfcTable addresses = function["ADDRESSES"].GetTable();

I know the error is because the element is not existing in the RFC_READ_TABLE module, but as I said I don't really know how to get data and what code I really need for that part of the program. I already looked through the programming guide from SAP but maybe it will help someone help me: .NET Connector Programming Guide

回答1:

From my perspective your question is too broad to be answered in short here. You seem to lack basic knowledge about SAP systems and their function and data model. The recommended way to work with SAP systems is to use specific function modules (BAPIs) which fit to your needs, and if such a function module would not be available out-of-the-box, then develop your own in ABAP.

If choosing any of the RFC_READ_TABLE function modules, you are showing that you would like to access the SAP System like a database, which it is not. It is an ERP system (which uses a database). With all of the RFC_READ_TABLE variants, you even need to know SAP's internal database table design and their table dependencies and you have to specify appropriate SQL statements in the function call. This approach is error-prone and not the recommended way to access data from a SAP system.

Just to give an example, if you would like to read stored customer data (including their addresses), use the remote function module BAPI_CUSTOMER_GETLIST instead.

And finally to your source code:

  1. You created a connection resource leak with calling RfcSessionManager.BeginContext(destination) but not ending the context again. Call this only if you really need it for doing a stateful call sequence. You don't need it here.
  2. You did not set or fill any import parameter for calling the function module.
  3. You did not execute/call the function module in the SAP System. This requires calling function.Invoke(destination).
  4. You tried to access a parameter "ADDRESSES" which is not available in the choosen function module /BODS/RFC_READ_TABLE. Have a look into the SAP System and check with transaction SE37 which parameters and tables your function module interface exposes.