Introduction
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:
- How to use
SAP.Connector.SAPLogonDestination
to let you select any destination provided by your saplogon.ini file.
- 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.
- 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.
Background
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 http://service.sap.com/connectors and an overview is given at http://www.microsoft-sap.com/net_connector.aspx.
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:
DBReader.TableReader
, wrapper for the RFC_READ_TABLE
call to SAP.
DBReader.ResultSet
, to manage the returned dataset.
DBReader.ResultColumn
, to hold a column of the returned dataset.
DBReader.Proxy
, wrapper for the proxy generated by the Connector wizard.
DBReader.Logon
, wrapper for SAP.Connector.SAPLogonDestination
.
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:
Collapse Copy Code
SELECT TABNAME, TABCLASS
FROM DD02L
WHERE TABNAME LIKE 'NP%'
AND ( TABCLASS EQ 'TRANSP' OR
TABCLASS EQ 'CLUSTER' OR
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
:
Collapse Copy Code
public void demoTableReader(){
string destName = this.askForDestination();
if(destName == null)
return;
SAP.Connector.Destination dest = this.saplogon.getDestinationByName(
destName);
Console.WriteLine("Connecting to '" + destName + "'");
dest.Client = 1;
Console.Write("User: ");
dest.Username = Console.ReadLine();
Console.Write("Password: ");
dest.Password = Console.ReadLine();
dest.Language = "EN";
SAPReader.TableReader tabReader = new TableReader(dest);
tabReader.QueryTable = "DD02L";
tabReader.RowSkip = 10;
tabReader.RowCount = 6;
tabReader.NoData = false;
SAPReader.SAPKernel.RFC_DB_FLD tabNameField =
tabReader.addQueryField("TABNAME");
SAPReader.SAPKernel.RFC_DB_FLD tabClassField =
tabReader.addQueryField("TABCLASS");
string pattern = "NP%";
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' )");
string abapException = tabReader.read();
if(abapException == null){
SAPReader.ResultSet RS = tabReader.getResultSet();
Console.WriteLine("\n\nTableData read:");
Console.WriteLine(tabNameField.Fieldname + "\t\t" +
tabClassField.Fieldname);
Console.WriteLine(
"-----------------------------------------------------");
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();
}
}
else{
Console.WriteLine("ABAP-Exception: " + abapException );
}
}
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.
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:
Collapse Copy Code
public SAP.Connector.Destination getDestinationByName(string name){
string destName = this.GetDestinationNameFromPrintName(name);
if(destName == null || destName == "" ){
Console.WriteLine(this.GetType().ToString()
+ ".getDestinationByName: Destination " + name +
" does not exist."
);
Console.WriteLine("Available Destinations are: ");
this.printAvailableDestinations(Console.Out);
Environment.Exit(0);
}
this.DestinationName = destName;
return (SAP.Connector.Destination)this;
}
The RFC call is performed by TableReader
this way:
Collapse Copy Code
public string read(){
if(this.proxy.connected() == false){
this.proxy.connectSAP();
}
try {
this.proxy.SAPProxy.Rfc_Read_Table(this.delim, this.noData,
this.qTable, this.rowCount, this.rowSkip,
ref this.data, ref this.fields, ref this.options);
if( this.NoData == false && this.data.Count == 0 ){
return "No data found";
}
if(this.NoData == true){
for( int i = 0; i < this.fields.Count; i++){
this.results.addEntry(this.fields[i], "NoData");
}
}
else if(this.data.Count > 0 ){
for( int i = 0; i < this.data.Count; i++){
for(int j = 0; j < this.fields.Count; j++){
string val = this.parseTableRow(this.fields[j], this.data[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:
Collapse Copy Code
public string parseTableRow(SAPReader.SAPKernel.RFC_DB_FLD field,
SAPReader.SAPKernel.TAB512 dataRow ){
int len = int.Parse(field.Length);
int offset = int.Parse(field.Offset);
string row = dataRow.Wa;
string retValue = "";
try{
if(offset < row.Length){
if(offset + len > row.Length)
retValue = dataRow.Wa.Substring(offset);
else
retValue = dataRow.Wa.Substring(offset,len);
}
}catch(System.ArgumentOutOfRangeException e){
Console.WriteLine(e.ToString());
Environment.Exit(0);
}
return retValue;
}