Reading SAP Tables Directly

A recurrent task in daily work with SAP and .NET applications is to read directly from tables of the SAP system. You can use the ReadTable class to manage this demand. The sample below shows how to select data from the table. The result is passed back via an easy-to-use ADO.net table object.

In this sample we want to read material description texts which are located in the table MAKT. So we need the two columns MATNR (material number) and MAKTX (material description). Furthermore we want only the English texts so we have to add the WHERE statement SPRAS=’EN’. SPRAS is the column which contains the language keys. The method Run executes the query and passes back the ADO.net table.


using System;
using ERPConnect;
using System.Data;
  
class Class1
{
    static void Main(string[] args)
    {
        ERPConnect.R3Connection con = new R3Connection("SAPServer",00,"SAPUser","Password","EN","800");
        ERPConnect.LIC.SetLic("xxxxxxxxxxxxx"); //Set your ERPConnect License.

        con.Open();  //Open the connection to SAP.
  
        ERPConnect.Utils.ReadTable table = new ERPConnect.Utils.ReadTable(con);
        table.AddField("MATNR");
        table.AddField("MAKTX");
        table.WhereClause = "SPRAS = 'EN'";    
        table.TableName = "MAKT";
        table.RowCount = 10;
  
        table.Run();
  
        DataTable resulttable = table.Result;
  
        for(int i=0; i < resulttable.Rows.Count;i++)
        {
            Console.WriteLine(
                resulttable.Rows[i]["MATNR"].ToString() + " " +
                resulttable.Rows[i]["MAKTX"].ToString());
        }
  
        Console.ReadLine();
  
    }
}


Creation date: 4/1/2022 9:38 AM      Updated: 1/22/2024 10:01 AM