Integrating .NET and SAP 101
Negative opinions about SAP's ERP product probably wouldn't get past my editor. If my opinions were indicated here about performance, elegance, or the fun factor of using mySAP ERP R/3, more than likely SAP will litigate. Suffice it to say that this product must appeal to businesses and business people, and this appeal clearly has led to financial success for SAP, the company.
Sometimes, we lowly programmers just have to do what is asked of us.
Understanding the Working Scenario
Suppose, for argument sake, that you have mySAP ERP R/3 version 4.x. Suppose further that you are tasked with using SAP's data in a non-SAP environment, a .NET environment. You have VB.NET 2.0 and Visual Studio 2005, and you have SAP client tools on your workstation as will everyone using your integration application. If these things are true, you already have everything you need to get started. (If not, it's going to be hard to try the example. However, with Microsoft Excel, VBA, and SAP client tools almost identical code will work.)
SAP (which translated from German—I think they get angry if you pronounce it "sap" but the temptation is there) means Systems, Applications, and Products. The ERP product—also often called SAP—exposes its capabilities through a variety of means, one of which is BAPIs (Business Application Programming Interface) and RFCs (Remote Function Calls). It is these BAPIs through COM Interop and VB.NET that you can call. And, that is where I will limit the discussion for now.
Note: It is worth noting that there are other tools such as the ERP .NET Connector that uses a technique much like importing a Web Service. I'll talk about this component/tool another time.
Retrieving Data from an SAP Implementation
Assuming all prior conditions mentioned are met, you can begin by creating a VB.NET project—I used a console application for the demo—and following these steps:
- In VS 2005, create a VB.NET console project
- Add a reference to SAPBAPIControlLib and SAPTableFactoryCtrl. This step will use tlbimp to generate COM Interop libraries, and your solution will actually link to Interop.SAPBAPIControlLib.dll and Interop.SAPTableFactoryCtrl.dll
- Add two Imports statements (see Listing 1) for each of these interop libraries
- And, write the rest of the code shown in Listing 1
Listing 1: Integrating VB.NET and mySAP ERP R/3 version 4.x using COM Interop.
Imports SAPBAPIControlLib Imports SAPTableFactoryCtrl Module Module1 Sub Main() Dim bapi As Object Dim connection As Object bapi = CreateObject("SAP.Functions") connection = bapi.Connection If (connection.Logon(0, False)) Then Dim codes = bapi.Add("BAPI_COMPANYCODE_GETLIST") codes.call() Dim table As Table table = codes.Tables("COMPANYCODE_LIST") Dim I As Integer Dim j As Integer For I = 1 To table.RowCount For j = 1 To table.ColumnCount Console.Write(table.ColumnName(j) + ":") Console.WriteLine(table.Cell(I, j)) Next Next Console.ReadLine() End If End Sub End Module
The code in Listing 1 works because it uses late-bound, weakly typed objects. For example, bapi and connection are defined as an object, and I simply know that I can call connection.Logon to get SAP to show their login screen. The bapi.Add call indicates which BAPI function will be called and the table collection will hold the returned data table. The rest of the code just dumps the contents to the console.
There are variations you can add to this basic code. For example, you could copy the data from the table object to a DataSetor custom objects. You also could initialize the connection with pre-defined connection arguments entered literally or by a custom WindowsForm.
SAP's ERP implementation is a huge system. According to some literature and several consultants, there are upwards of 50,000 tables and probably that many BAPIs. As a result, there is ton of supporting infrastructure, but this code should get you started.
The mySAP ERP product is a generational system. If you are working on an SAP implementation, you have my sympathies. A lot of SAP's own code, comments, and tables are still in the original German. It is a mega-system in its own right, and if you have to master .NET and ABAP—one programming choice for SAP—and the SAP client tools, you have your work cut out for you.
Hopefully, this article demonstrated that integration with Windows is possible and basic operations aren't too difficult. Like everything else, though, challenges will be greater as your solution scales.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his new book, UML DeMystified from McGraw-Hill/Osborne. Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.
Copyright © 2007 by Paul T. Kimmel. All Rights Reserved.