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 to the Visual Studio project.
Create a new class connection function to C# SAP integration, for example, ECCDestinationConfig.cs
using SAP.Middleware.Connector;
public class ECCDestinationConfig : IDestinationConfiguration
{
public bool ChangeEventsSupported()
{
return true;
}
public event RfcDestinationManager.ConfigurationChangeHandler ConfigurationChanged;
public RfcConfigParameters GetParameters(string destinationName)
{
RfcConfigParameters parms = new RfcConfigParameters();
// SAP Parameters
if (destinationName.Equals("mySAPdestination"))
{
parms.Add(RfcConfigParameters.AppServerHost, "IPAddress");
parms.Add(RfcConfigParameters.SystemNumber, "00");
parms.Add(RfcConfigParameters.SystemID, "ID");
parms.Add(RfcConfigParameters.User, "Username");
parms.Add(RfcConfigParameters.Password, "Password");
parms.Add(RfcConfigParameters.RepositoryPassword, "Password");
parms.Add(RfcConfigParameters.Client, "100");
parms.Add(RfcConfigParameters.Language, "EN");
parms.Add(RfcConfigParameters.PoolSize, "5");
}
return parms;
}
}
The function below is used for data types, the class name is IRfcTableExtension.
using SAP.Middleware.Connector;
public static class IRfcTableExtension
{
/// <summary>
/// Converts SAP table to .NET DataTable table
/// </summary>
/// <param name="sapTable">The SAP table to convert.</param>
/// <returns></returns>
public static DataTable ToDataTable(this IRfcTable sapTable, string name)
{
DataTable adoTable = new DataTable(name);
//... Create ADO.Net table.
for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)
{
RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);
adoTable.Columns.Add(metadata.Name, GetDataType(metadata.DataType));
}
//Transfer rows from SAP Table to ADO.Net table.
foreach (IRfcStructure row in sapTable)
{
DataRow ldr = adoTable.NewRow();
for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)
{
RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);
switch (metadata.DataType)
{
case RfcDataType.DATE:
ldr[metadata.Name] = row.GetString(metadata.Name).Substring(0, 4) + row.GetString(metadata.Name).Substring(5, 2) + row.GetString(metadata.Name).Substring(8, 2);
break;
case RfcDataType.BCD:
ldr[metadata.Name] = row.GetDecimal(metadata.Name);
break;
case RfcDataType.CHAR:
case RfcDataType.STRING:
ldr[metadata.Name] = row.GetString(metadata.Name);
break;
case RfcDataType.INT2:
case RfcDataType.INT4:
ldr[metadata.Name] = row.GetInt(metadata.Name);
break;
case RfcDataType.FLOAT:
ldr[metadata.Name] = row.GetDouble(metadata.Name);
break;
default:
ldr[metadata.Name] = row.GetString(metadata.Name);
break;
}
}
adoTable.Rows.Add(ldr);
}
return adoTable;
}
private static Type GetDataType(RfcDataType rfcDataType)
{
switch (rfcDataType)
{
case RfcDataType.DATE:
case RfcDataType.CHAR:
case RfcDataType.STRING:
return typeof(string);
case RfcDataType.BCD:
return typeof(decimal);
case RfcDataType.INT2:
case RfcDataType.INT4:
return typeof(int);
case RfcDataType.FLOAT:
return typeof(double);
default:
return typeof(string);
}
}
}
The functions below will help to connect to the SAP System and access the RFC function by communicating the required input and output parameters.
using SAP.Middleware.Connector;
internal static void CallingSAP()
{
ECCDestinationConfig cfg = null;
RfcDestination dest = null;
try
{
cfg = new ECCDestinationConfig();
RfcDestinationManager.RegisterDestinationConfiguration(cfg);
dest = RfcDestinationManager.GetDestination("mySAPdestination");
RfcRepository repo = dest.Repository;
IRfcFunction fnpush = repo.CreateFunction("ZFUNCTION");
// Send Data With RFC Structure
IRfcStructure data = fnpush.GetStructure("IM_STRUCTURE");
data.SetValue("ITEM1", "VALUE1");
data.SetValue("ITEM2", "VALUE2");
data.SetValue("ITEM3", "VALUE3");
data.SetValue("ITEM4", "VALUE4");
fnpush.SetValue("IM_STRUCTURE", data);
// Send Data With RFC Table
IRfcTable dataTbl = fnpush.GetTable("IM_TABLE");
foreach (var item in ListItem)
{
dataTbl.Append();
dataTbl.SetValue("ITEM1", item.VALUE1);
dataTbl.SetValue("ITEM2", item.VALUE2);
dataTbl.SetValue("ITEM3", item.VALUE3);
dataTbl.SetValue("ITEM4", item.VALUE4);
}
fnpush.Invoke(dest);
var exObject = fnpush.GetObject("EX_OBJECT");
IRfcStructure exStructure = fnpush.GetStructure("EX_STRUCTURE");
RfcSessionManager.EndContext(dest);
RfcDestinationManager.UnregisterDestinationConfiguration(cfg);
}
catch (Exception ex)
{
RfcSessionManager.EndContext(dest);
RfcDestinationManager.UnregisterDestinationConfiguration(cfg);
Thread.Sleep(1000);
}
}
Although creating, invoking, and extracting your data from either a structure 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 Here