Connect To SAP Using C#

This article will teach you about connecting to SAP using C# via SAP .NET Connector 3.0. This connector is the latest stable version  of SAP's development environment to do communication between the Microsoft .NET platform and SAP systems. This connector library can support RFCs and Web services. This allows developers to type in different applications such as Web form, Windows form, or console Windows applications in the Microsoft Visual Studio.Net. With this Connector, developers can use all common programming languages, like Visual Basic. NET, C#, or Managed C++.
 
In this guide, I will tell you how to make a C# SAP connector. 
 
To get started, download the SAP library from the official SAP and add references in the Visual Studio project.
 
Create a new class connection function to C# SAP integration, for example ECCDestinationConfig.cs :
  1. using SAP.Middleware.Connector;  
  2.   
  3. public class ECCDestinationConfig : IDestinationConfiguration  
  4.     {  
  5.         public bool ChangeEventsSupported()  
  6.         {  
  7.             return true;  
  8.         }  
  9.   
  10.         public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;  
  11.           
  12.   
  13.         public RfcConfigParameters GetParameters(string destionationName)  
  14.         {  
  15.             RfcConfigParameters parms = new RfcConfigParameters();  
  16.   
  17.             SAP Parameters  
  18.             if (destionationName.Equals("mySAPdestination"))  
  19.             {  
  20.                 parms.Add(RfcConfigParameters.AppServerHost, "IPAddress");  
  21.                 parms.Add(RfcConfigParameters.SystemNumber, "00");  
  22.                 parms.Add(RfcConfigParameters.SystemID, "ID");  
  23.                 parms.Add(RfcConfigParameters.User, "Username");  
  24.                 parms.Add(RfcConfigParameters.Password, "Password");  
  25.                 parms.Add(RfcConfigParameters.RepositoryPassword, "Password");  
  26.                 parms.Add(RfcConfigParameters.Client, "100");  
  27.                 parms.Add(RfcConfigParameters.Language, "EN");  
  28.                 parms.Add(RfcConfigParameters.PoolSize, "5");  
  29.             }  
  30.   
  31.             return parms;  
  32.         }  
  33.     } 
The function below is used for data types, the class name is IRfcTableExtension.
  1. using SAP.Middleware.Connector;  
  2.   
  3. public static class IRfcTableExtension  
  4.     {  
  5.         /// <summary>  
  6.         /// Converts SAP table to .NET DataTable table  
  7.         /// </summary>  
  8.         /// <param name="sapTable">The SAP table to convert.</param>  
  9.         /// <returns></returns>  
  10.         public static DataTable ToDataTable(this IRfcTable sapTable, string name)  
  11.         {  
  12.             DataTable adoTable = new DataTable(name);  
  13.             //... Create ADO.Net table.  
  14.             for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)  
  15.             {  
  16.                 RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);  
  17.                 adoTable.Columns.Add(metadata.Name, GetDataType(metadata.DataType));  
  18.             }  
  19.   
  20.             //Transfer rows from SAP Table ADO.Net table.  
  21.             foreach (IRfcStructure row in sapTable)  
  22.             {  
  23.                 DataRow ldr = adoTable.NewRow();  
  24.                 for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)  
  25.                 {  
  26.                     RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);  
  27.   
  28.                     switch (metadata.DataType)  
  29.                     {  
  30.                         case RfcDataType.DATE:  
  31.                             ldr[metadata.Name] = row.GetString(metadata.Name).Substring(0, 4) + row.GetString(metadata.Name).Substring(5, 2) + row.GetString(metadata.Name).Substring(8, 2);  
  32.                             break;  
  33.                         case RfcDataType.BCD:  
  34.                             ldr[metadata.Name] = row.GetDecimal(metadata.Name);  
  35.                             break;  
  36.                         case RfcDataType.CHAR:  
  37.                             ldr[metadata.Name] = row.GetString(metadata.Name);  
  38.                             break;  
  39.                         case RfcDataType.STRING:  
  40.                             ldr[metadata.Name] = row.GetString(metadata.Name);  
  41.                             break;  
  42.                         case RfcDataType.INT2:  
  43.                             ldr[metadata.Name] = row.GetInt(metadata.Name);  
  44.                             break;  
  45.                         case RfcDataType.INT4:  
  46.                             ldr[metadata.Name] = row.GetInt(metadata.Name);  
  47.                             break;  
  48.                         case RfcDataType.FLOAT:  
  49.                             ldr[metadata.Name] = row.GetDouble(metadata.Name);  
  50.                             break;  
  51.                         default:  
  52.                             ldr[metadata.Name] = row.GetString(metadata.Name);  
  53.                             break;  
  54.                     }  
  55.                 }  
  56.                 adoTable.Rows.Add(ldr);  
  57.             }  
  58.             return adoTable;  
  59.         }  
  60.   
  61.         private static Type GetDataType(RfcDataType rfcDataType)  
  62.         {  
  63.             switch (rfcDataType)  
  64.             {  
  65.                 case RfcDataType.DATE:  
  66.                     return typeof(string);  
  67.                 case RfcDataType.CHAR:  
  68.                     return typeof(string);  
  69.                 case RfcDataType.STRING:  
  70.                     return typeof(string);  
  71.                 case RfcDataType.BCD:  
  72.                     return typeof(decimal);  
  73.                 case RfcDataType.INT2:  
  74.                     return typeof(int);  
  75.                 case RfcDataType.INT4:  
  76.                     return typeof(int);  
  77.                 case RfcDataType.FLOAT:  
  78.                     return typeof(double);  
  79.                 default:  
  80.                     return typeof(string);  
  81.             }  
  82.         }  
  83.     } 
The functions below will help to connect to the SAP System, and access the RFC function by communicating the required input and output parameters.
  1. using SAP.Middleware.Connector;  
  2.   
  3. internal static void CallingSAP()  
  4.         {  
  5.             ECCDestinationConfig cfg = null;  
  6.             RfcDestination dest = null;  
  7.   
  8.             try  
  9.             {  
  10.                 cfg = new ECCDestinationConfig();  
  11.                 RfcDestinationManager.RegisterDestinationConfiguration(cfg);  
  12.                 dest = RfcDestinationManager.GetDestination("mySAPdestination");  
  13.   
  14.                 RfcRepository repo = dest.Repository;  
  15.   
  16.                 IRfcFunction fnpush = repo.CreateFunction("ZFUNCTION");  
  17.                   
  18.                 // Send Data With RFC Structure  
  19.                 IRfcStructure data = fnpush.GetStructure("IM_STRUCTURE");  
  20.                 data.SetValue("ITEM1""VALUE1");  
  21.                 data.SetValue("ITEM2""VALUE2");  
  22.                 data.SetValue("ITEM3""VALUE3");  
  23.                 data.SetValue("ITEM4""VALUE4");  
  24.   
  25.                 fnpush.SetValue("IM_STRUCTURE", data);  
  26.   
  27.                 // Send Data With RFC Table  
  28.                 IRfcTable dataTbl = fnpush.GetTable("IM_TABLE");  
  29.   
  30.                 foreach (var item in ListItem)  
  31.                 {  
  32.                     dataTbl.Append();  
  33.                     dataTbl.SetValue("ITEM1", item.VALUE1);  
  34.                     dataTbl.SetValue("ITEM2", item.VALUE2);  
  35.                     dataTbl.SetValue("ITEM3", item.VALUE3);  
  36.                     dataTbl.SetValue("ITEM4", item.VALUE4);  
  37.                 }  
  38.   
  39.                 fnpush.Invoke(dest);  
  40.   
  41.                 var exObject = fnpush.GetObject("EX_OBJECT");  
  42.                 IRfcStructure exStructure = fnpush.GetStructure("EX_STRUCTURE");  
  43.                   
  44.   
  45.                 RfcSessionManager.EndContext(dest);  
  46.                 RfcDestinationManager.UnregisterDestinationConfiguration(cfg);  
  47.             }  
  48.             catch (Exception ex)  
  49.             {                  
  50.                 RfcSessionManager.EndContext(dest);  
  51.                 RfcDestinationManager.UnregisterDestinationConfiguration(cfg);  
  52.                 Thread.Sleep(1000);  
  53.             }  
  54.         } 
Although creating, invoking and extracting your data from either a structure or table or object may be very easy, the hardest part of this section is finding the right function, importing parameter values ​​and which table or structure contains the right information. It's also important to remember the fact that the function uses the same field name as the SAP table, so I sometimes need to open the program to see which fields are being paid back. For this and for finding functions, tables, structures, import and export parameters, BAPI Explorer is a very valuable tool.
 
I hope this tutorial about connecting to SAP with C# contains enough information to get you going. If more information is needed leave a comment and I will try and help.
 
Thank you for reading this article about Connect to SAP using C#, I hope this article is useful for you. Visit My Github about .Net Cshap in Here


Similar Articles