cancel
Showing results for 
Search instead for 
Did you mean: 

Convert SAP table data into a data table

dileeppingeli
Member
0 Kudos

Hello All, We are using SAP .NET Connector (NCo) libraries (SAP.Connector.dll and SAP.Middleware.Connector.dll) to retrieve data from SAP using C#. Establishing the connection to SAP and retrieving the data is working fine. Below is the sample line of code at the end where we have the SAP table data.

`IRfcTable irfcTableColumn = function.GetTable("FIELDS");`
`IRfcTable irfcTableRow = function.GetTable("DATA");`
for "irfcTableColumn" data is coming in below format.
"FIELDS":[
      {
         "FIELDNAME":"MANDT",
         "OFFSET":"000000",
         "LENGTH":"000003",
         "TYPE":"C",
         "FIELDTEXT":"Client"
      },
      {
         "FIELDNAME":"AGRTY",
         "OFFSET":"000004",
         "LENGTH":"000004",
         "TYPE":"C",
         "FIELDTEXT":"AgreementTypeCode"
      },      
      {
         "FIELDNAME":"AEZET",
         "OFFSET":"000147",
         "LENGTH":"000006",
         "TYPE":"T",
         "FIELDTEXT":"SubTypeCode"
      }
   ]
For the Rows, data is coming in the below format.
 "DATA":[
      {
         "WA":"100|DISP|01 "
      },     
      {
         "WA":"100|DISP|05 "
      }
   ]
Now I need a data table in below format

Used below class to convert the data into columns and rows but it is not coming in above expected format. Any help in tweaking the below code to get the expected format is highly appreciated.

dt = SapToDataExtensionClass.GetDataTable(irfcTableColumn, irfcTableRow);

public static class SapToDataExtensionClass

{

public static DataTable GetDataTable(this IRfcTable i_TableC, IRfcTable i_TableR )

{

DataTable dt = new DataTable();

dt.GetColumnsFromSapTable(i_TableC);

dt.FillRowsFromSapTable(i_TableR);

return dt;

}

public static void FillRowsFromSapTable(this DataTAble i_DataTable, IRfcTable i_Table)

{

foreach (IRfcStructure tableRow in i_Table)

{

DataRow dr = i_DataTable.NewRow();

dr.ItemArray = tableRow.Select(structField => structField.GetValue()).ToArray();

i_DataTable.Rows.Add(dr);

}

}

public static void GetColumnsFromSapTable(this DataTable i_DataTable , IRfcTable i_SapTable)

{

var DataColumnsArr = i_SapTable.Metadata.LineType.CreateStructure().ToList().Select

(structField => new DataColumn(structField.Metadata.Name)).ToArray();

i_DataTable.Columns.AddRange(DataColumnsArr);

}

}

Accepted Solutions (0)

Answers (1)

Answers (1)

Ulrich_Schmidt
Product and Topic Expert
Product and Topic Expert
0 Kudos

All I can answer to this is:

Do not use RFC_READ_TABLE!

It's a glaring security vulnerability, which should be disabled in any productive SAP System. (Search for some of my earlier postings about RFC_READ_TABLE on this forum. I don't want to repeat all the details here.)

Get the data on application level, not on database level. If necessary (e.g. no standard BAPI returns the data you need), write your own function module, which retrieves exactly the data you need in the form you need it.