How to fetch data from SAP system using sap .net c

2020-02-26 12:03发布

问题:

I am developing an windows application, here I wanna extract data from sap system and show it in a datagridview... I have extracted the column names alone like name, city and so on..

I don't know how to extract the data from the columns, can anyone help me with the code?

I am using RFC_READ_TABLE function module and rfc destiantion manager

Thanks in advance!!!

回答1:

untested, but this is essentially how it works:

first create the connection

RfcDestination destination = mDestinationManager.GetDestination("MYDESTINATION");

create the function

IRfcFunction readTable = destination.Repository.CreateFunction("RFC_READ_TABLE");

set parameters before invoking the function

// we want to query table KNA1
readTable.SetValue("QUERY_TABLE", "KNA1");
// fields will be separated by semicolon
readTable.SetValue("DELIMITER", ";");

table parameters are created by retrieving the table from the function, using the Append() function to add a row and using SetValue() to set values for individual columns in that row

// Parameter table FIELDS contains the columns you want to receive
// here we query 2 fields, KUNNR and NAME1
IRfcTable fieldsTable = readTable.GetTable("FIELDS");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "KUNNR");
fieldsTable.Append();
fieldsTable.SetValue("FIELDNAME", "NAME1");

// the table OPTIONS contains the WHERE condition(s) of your query
// here a single condition, KUNNR is to be 0012345600
// several conditions have to be concatenated in ABAP syntax, for instance with AND or OR
IRfcTable optsTable = readTable.GetTable("OPTIONS");
optsTable.Append();
optsTable.SetValue("TEXT", "KUNNR = '0012345600'");

call the function

readTable.Invoke(destination);

process the data

IRfcTable dataTable = readTable.GetTable("DATA");

foreach(var dataRow in dataTable)
{
    string data = dataRow.GetValue("WA");
    string[] columns = data.Split(';');
}