Click here to Skip to main content
15,172,351 members
Articles / Programming Languages / C#
Posted 19 Oct 2003


82 bookmarked

Read Tables from SAP R/3 using SAP.NET Connector

Rate me:
Please Sign up or sign in to vote.
4.70/5 (22 votes)
19 Feb 2004LGPL32 min read
Reading SAP R/3 made easy.


Using SAP's .NET Connector is very easy, all it takes to manage it is a little work and effort. This article provides you an introductory example on how to use the Connector within C#.

The example will teach you:

  1. How to use SAP.Connector.SAPLogonDestination to let you select any destination provided by your saplogon.ini file.
  2. How to make an RFC call to the SAP R/3 System. Especially the example shows how to call the RFC_READ_TABLE function module to read from any table within your SAP system.
  3. How to extract the field values of the data rows returned by the RFC call.

Any GUI stuff is omitted to keep the example as simple as possible and to focus on the Connector's use.


You should have some experiences with ABAP/4 and function modules and the SAP.NET Connector installed. Any project that uses the Connector must have a reference to SAP.Connector.dll. The Connector is available at and an overview is given at

Using the code

If you want to use DBReader in your application, download and unzip the source file. Add a reference to the library assembly DBReader.dll that comes with the sources. DBReader.dll provides the following classes:

  1. DBReader.TableReader, wrapper for the RFC_READ_TABLE call to SAP.
  2. DBReader.ResultSet, to manage the returned dataset.
  3. DBReader.ResultColumn, to hold a column of the returned dataset.
  4. DBReader.Proxy, wrapper for the proxy generated by the Connector wizard.
  5. DBReader.Logon, wrapper for SAP.Connector.SAPLogonDestination.
  6. DBReader.ConnectionInfo, to hold logon information.

If you want to see how it works, download and unzip the demo file to disk. Either run DBReaderDemo.exe in the bin subfolder of the demo project from console directly, or open DBReaderDemo.sln into Visual Studio and run the demo.

The TableReaderDemo class shows how to use the DBReader for a query to SAP. Consider the following SQL statement in ABAP:

             TABCLASS EQ 'POOL' OR
             TABCLASS EQ 'VIEW' )

The equivalent code to achieve this with DBReader from outside SAP shows a demo function of class TableReaderDemo:

public void demoTableReader(){
  // Show the available destinations and let the user select one.
  string destName = this.askForDestination();
  if(destName == null)

  // Get the selected destination object.
  SAP.Connector.Destination dest = this.saplogon.getDestinationByName(
  Console.WriteLine("Connecting to '" + destName + "'");

  // Information like system number, SAPServer etc. are already
  // obtained by the .Net Connector from the c:\windows\saplogon.ini file.
  // It remains to add these information:
  dest.Client   = 1;
  Console.Write("User:     ");
  dest.Username = Console.ReadLine();
  Console.Write("Password: ");
  dest.Password = Console.ReadLine();
  dest.Language = "EN";

  // Setup the TableReader
  SAPReader.TableReader tabReader = new TableReader(dest);
  // Specify the table to read.
  // The table DD02L contains metadata of all the available
  // tables on a SAP system.
  tabReader.QueryTable = "DD02L";
  // How many rows of the table should be skipped.
  tabReader.RowSkip = 10;
  // How many rows should be selected (at most, if available).
  tabReader.RowCount = 6;
  // Yes, we want to see data.
  // Default is NoData=true, i.e., only table information is retrieved.
  tabReader.NoData = false;
  // Select FOI (fields of interest)
  SAPReader.SAPKernel.RFC_DB_FLD tabNameField  =
  SAPReader.SAPKernel.RFC_DB_FLD tabClassField =
  // Add the WHERE clause
  string pattern = "NP%";
  // Select the names of all the transparent, cluster, pool
  // and view tables containing the pattern:
  tabReader.addWhereClause("TABNAME LIKE '" + pattern + "'");
  tabReader.addWhereClause("AND ( TABCLASS EQ 'TRANSP' OR");
  tabReader.addWhereClause("      TABCLASS EQ 'CLUSTER' OR");
  tabReader.addWhereClause("      TABCLASS EQ 'POOL' OR");
  tabReader.addWhereClause("      TABCLASS EQ 'VIEW' )");

  // Force to read the table from SAP
  string abapException =;
  if(abapException == null){
    // Output the result:
    SAPReader.ResultSet RS = tabReader.getResultSet();
    Console.WriteLine("\n\nTableData read:");
    Console.WriteLine(tabNameField.Fieldname + "\t\t" +
    for(int i = 0; i < RS.LineCount; i++){
      Console.Write(RS.getEntryAt(tabNameField.Fieldname, i).Trim());
      Console.Write("\t\t" +
           RS.getEntryAt(tabClassField.Fieldname, i).Trim());
    Console.WriteLine("ABAP-Exception: " + abapException );
  // That's it!

After the table was read successfully, the ResultSet is traversed row-wise (although the data is stored column-wise there) and printed to the screen.

Points of Interest

The fragmented screenshot below shows the console output for this example.

Image 1

The SAPReader.Logon saplogon is an attribute of class TableReaderDemo and is derived from SAP.Connector.SAPLogonDestination. If the user selects a destination name, the saplogon selects the corresponding destination parameters:

public SAP.Connector.Destination getDestinationByName(string name){
  // Map the name used for displaying the available destination,
  // e.g. at SAPLogon, to the internal name used to address this item.
  // BTW, the internal name (key) is derived from saplogon.ini.
  string destName = this.GetDestinationNameFromPrintName(name);
  // null returned if the destination does not exist.
  if(destName == null || destName == "" ){
      + ".getDestinationByName: Destination " + name +
              " does not exist."
    Console.WriteLine("Available Destinations are: ");
  // This is the key statement for selecting the
  // desired destination item:
  this.DestinationName = destName;
  // Now all information is retrieved from the SAPLogon's ini file
  // to the respective variables of 'this' destination object.
  // (The ini file is stored in the private variable:
  // SAP.Connector.SAPLogonDestination.saplogon.fileName)

  return (SAP.Connector.Destination)this;

The RFC call is performed by TableReader this way:

public string read(){
  if(this.proxy.connected() == false){

  try {
    // Force the proxy to make the rfc call synchronously
    // See documentation for RFC_READ_TABLE Function Module at
    // your SAP system for further details (Transaction SE37).
    this.proxy.SAPProxy.Rfc_Read_Table(this.delim, this.noData,
      this.qTable, this.rowCount, this.rowSkip,
      ref, ref this.fields, ref this.options);

    // Check if data found
    if( this.NoData == false && == 0 ){
      return "No data found";

    // NoData set
    if(this.NoData == true){
      for( int i = 0; i < this.fields.Count; i++){
        this.results.addEntry(this.fields[i], "NoData");
    // NoData not set
    // Save data to the column-wise ResultSet.
    else if( > 0 ){
      //Loop over data rows
      for( int i = 0; i <; i++){
        //Loop over fields
        for(int j = 0; j < this.fields.Count; j++){
          string val = this.parseTableRow(this.fields[j],[i]);
          this.results.addEntry(this.fields[j], val);
  catch (SAP.Connector.RfcSystemException ex) {
    System.Text.StringBuilder msg = new System.Text.StringBuilder();
    msg.Append("Table: " + this.qTable);
    foreach(SAPKernel.RFC_DB_FLD field in this.fields){
      msg.Append("\nFields: " + field.Fieldname);
    foreach(SAPKernel.RFC_DB_OPT opt in this.options){
      msg.Append("\nOptions: " + opt.Text);
    Console.WriteLine(msg + "\n"
      + "Error calling SAP RFC \n" + ex.ToString() + "\n" + ex.ErrorCode,
      "Problem with SAP"

    return ex.ErrorCode;
  catch(SAP.Connector.RfcAbapException ex){
    return ex.ErrorCode;
  return null;

Data returned by the RFC call is stored row-wise. A value of a field in a certain row can be extracted by using the offset and length stored in each field structure returned:

public string parseTableRow(SAPReader.SAPKernel.RFC_DB_FLD field,
     SAPReader.SAPKernel.TAB512 dataRow ){
  // Length of the field
  int len = int.Parse(field.Length);
  // Position where the field's value starts in the data row
  int offset = int.Parse(field.Offset);

  // The data row, containing all the field values concatenated
  // by SAP's rfc_read_table
  string row = dataRow.Wa;
  string retValue = "";

    if(offset < row.Length){
      // Read the field's value starting at the position specified
      // by offset.
      if(offset + len > row.Length)
        // Read until the end of the row string
        retValue = dataRow.Wa.Substring(offset);
        // Read only len characters, otherwise.
        retValue = dataRow.Wa.Substring(offset,len);
  }catch(System.ArgumentOutOfRangeException e){
  return retValue;

If you have any comments or suggestions on this, please let me know!


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Germany Germany
No Biography provided

Comments and Discussions

QuestionThe provided links are dead. Pin
Martin Maat13-Nov-19 3:48
MemberMartin Maat13-Nov-19 3:48 
QuestionThe WHERE condition has an unexpected format. Pin
kpablo9-Jul-13 0:29
Memberkpablo9-Jul-13 0:29 
AnswerRe: The WHERE condition has an unexpected format. Pin
hardteck14-Jul-13 10:07
Memberhardteck14-Jul-13 10:07 
Questionsapmsg.ini Pin
Anuradha Wijesinghe6-Jun-13 1:10
MemberAnuradha Wijesinghe6-Jun-13 1:10 
QuestionHow to set connection to my SAP Server using DBReader Pin
snehalpatel846-Feb-12 23:03
Membersnehalpatel846-Feb-12 23:03 
GeneralConnect SAP R/3 using Asp.Net Pin
Tej Aj8-Nov-10 23:24
MemberTej Aj8-Nov-10 23:24 
GeneralRe: Connect SAP R/3 using Asp.Net Pin
hardteck30-Jun-11 22:47
Memberhardteck30-Jun-11 22:47 
GeneralSAP.NET Runtime Error Pin
Vishal Dave16-Nov-09 2:42
MemberVishal Dave16-Nov-09 2:42 
GeneralRe: SAP.NET Runtime Error Pin
Cleofus23-Dec-09 9:10
MemberCleofus23-Dec-09 9:10 
GeneralSAP.NET Connector Issue - Pin
Vishal Dave15-Nov-09 20:12
MemberVishal Dave15-Nov-09 20:12 
GeneralI want to get number of fields in a table Pin
Mogamboo_Khush_Hua28-Jul-09 0:26
MemberMogamboo_Khush_Hua28-Jul-09 0:26 
QuestionSAP Functions help Pin
abhijeetpatila27-Apr-09 9:15
Memberabhijeetpatila27-Apr-09 9:15 
GeneralSAP .Net Connector Issue Pin
abhijeetpatila17-Apr-09 4:33
Memberabhijeetpatila17-Apr-09 4:33 
GeneralRe: SAP .Net Connector Issue Pin
abhijeetpatila17-Apr-09 4:52
Memberabhijeetpatila17-Apr-09 4:52 
Generalfunctions in sap Pin
link18218-Apr-08 9:48
Memberlink18218-Apr-08 9:48 
GeneralRe: functions in sap Pin
hardteck21-Apr-08 2:43
Memberhardteck21-Apr-08 2:43 
QuestionVB.Net? Pin
David_379-Aug-07 10:21
MemberDavid_379-Aug-07 10:21 
Questionsapmsg.ini Pin
wizardkronuz3-Jul-07 10:17
Memberwizardkronuz3-Jul-07 10:17 
AnswerRe: sapmsg.ini Pin
hardteck10-Jul-07 22:08
Memberhardteck10-Jul-07 22:08 
GeneralRe: sapmsg.ini Pin
wizardkronuz12-Jul-07 1:38
Memberwizardkronuz12-Jul-07 1:38 
QuestionRFC_READ_TABLE is not Released to Customers??? Pin
Solo.Sun12-Apr-07 6:22
MemberSolo.Sun12-Apr-07 6:22 
QuestionCall Transactions in SAP with SAP.Net Connector Pin
Prishendra9-Mar-07 9:22
MemberPrishendra9-Mar-07 9:22 
GeneralRe: Call Transactions in SAP with SAP.Net Connector Pin
hardteck16-Mar-07 11:42
Memberhardteck16-Mar-07 11:42 
QuestionRfcCommunicationException Pin
albert.cao@163.com13-Feb-07 23:45
Memberalbert.cao@163.com13-Feb-07 23:45 
QuestionCalling a another RFC Pin
Andi_Le20-Sep-06 7:17
MemberAndi_Le20-Sep-06 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.