Integrating RPG and CL Programs by Using The Microsoft OLE DB Provider For DB2
Integrating RPG and CL Programs by Using The Microsoft OLE DB Provider For DB2
Applies To:
Host Integration Server 2000
Abstract
This document describes how to use DB2 stored procedures to call RPG (Report Program
Generator) and CL (Control Language) programs (running on an IBM iSeries or AS/400 system)
with the Microsoft® OLE DB Provider for DB2. Samples of RPG and CL server programs are
provided, as well as sample Microsoft Visual Basic® 6, Microsoft Visual Basic .NET, and Microsoft
Visual C#® .NET client-side code to connect to those programs.
Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2 1
Introduction
Information technology (IT) departments running IBM iSeries or AS/400 systems have a critical
need for application integration between these systems and computers running Microsoft
Windows®.
Microsoft Host Integration Server 2000 provides several ways to perform application integration
between these systems, including DB2 database access, printer emulation, data queue access,
and shared folders.
In addition, many customers are looking for ways to integrate Windows-based applications with
their existing RPG and CL programs on host platforms. You can do this with Host Integration
Server 2000 by wrapping an existing RPG or CL application with a DB2 stored procedure, and then
using the Microsoft OLE DB Provider for DB2 to call this stored procedure. This solution is
described in detail later in this document.
2 Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2
A Sample CL Program
The following CL code is a simple application that takes two input parameter strings and swaps
them:
PGM (&STR1 &STR2) /*SWAPSTR*/
DCL VAR(&STR1) TYPE(*CHAR) LEN(10)
DCL VAR(&STR2) TYPE(*CHAR) LEN(10)
DCL VAR(&TMPSTR) TYPE(*CHAR) LEN(10)
CHGVAR &TMPSTR &STR1
CHGVAR &STR1 &STR2
CHGVAR &STR2 &TMPSTR
ENDPGM
After this code is compiled and available for execution, the next step is to define a stored
procedure that “wraps” this CL program, calling into it with the appropriate parameters. You can
use the following SQL statement to do this:
CREATE PROCEDURE MYLIB/SWAPSTR(INOUT PARAM1 CHAR (10 ), INOUT
PARAM2 CHAR (10 )) LANGUAGE CL NOT DETERMINISTIC NO SQL EXTERNAL
NAME MYLIB/SWAPSTR PARAMETER STYLE GENERAL
Note The syntax used in the preceding statement to qualify the collection name is
COLLECTIONNAME/OBJECTNAME. Use this syntax when executing commands from an
interactive SQL session (STRSQL) on the iSeries. When using the OLE DB Provider for DB2,
use COLLECTIONNAME.OBJECTNAME, using a period rather than a forward slash.
Notice that the CL program takes two 10-character strings as input parameters. It uses a third
local variable to swap the values of the two input strings, and then ends. If you examine the
stored procedure, you see that it takes two 10-character input/output parameters, which it passes
to the MYLIB/SWAPSTR application.
Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2 3
* ---------------------
D TempVar S 10A Inz Temp Var to Switch
**************************************************************************
* *ENTRY PARAMETERS
C *Entry Plist
C Parm ValueOne 10 Character Value One
C Parm ValueTwo 10 Character Value Two
**************************************************************************
* MAINLINE
**************************************************************************
C Eval TempVar = ValueTwo
*
C Eval ValueTwo = ValueOne
C Eval ValueOne = TempVar
*
C Eval *InLr = *On
**************************************************************************
This code performs exactly the same function as the sample CL program. It switches the values of
the two input parameters. The following is the stored procedure definition used to call this RPGLE
application:
CREATE PROCEDURE MYLIB/SWAPRPG(INOUT PARM1 CHAR (10), INOUT
PARM2 CHAR (10)) LANGUAGE RPGLE NOT DETERMINISTIC NO
SQL EXTERNAL NAME MYLIB/SWAPRPG PARAMETER STYLE GENERAL
4 Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2
'setup the actual command and command type
Dim myCommand As New ADODB.Command
myCommand.CommandText = "CALL MYLIB.SWAPSTR(?,?)"
myCommand.CommandType = adCmdText
myCommand.ActiveConnection = myConnection
'execute
myCommand.Execute
myConnection.Close
There are a few key points to understand in this code sample:
• The syntax of the SQL statement that calls the DB2 stored procedure is simply the CALL
keyword followed by the stored procedure name. Question marks are used as placeholders for
each of the parameters that will be passed in by the application.
• The CommandType of the ADODB.Command object is set to adCmdText rather than
adCmdStoredProc. This is necessary because ADO inserts an EXEC keyword rather than the
CALL keyword if adCmdStoredProc is used.
• Notice how two parameters are created that match the specification of the DB2 stored
procedure, and then these are appended to the ADODB.Command object. It is important
that the parameters defined in the sample code match the stored procedure to ensure that
data is passed between the two applications correctly.
The following is a sample of Visual Basic .NET code demonstrating the same functionality:
'setup required connection object
Dim ConnString As String
ConnString = "Provider=DB2OLEDB;Password=SNA;Persist Security Info=True;User
ID=SNA;Initial Catalog=MYHOST;Data Source=MYHOST_IP;Network Transport
Library=TCPIP;Network Address=MYHOST;Package Collection=MYLIB;Default Schema=MYLIB"
Dim myConnection As New OleDb.OleDbConnection(ConnString)
Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2 5
'setup the actual command and command type
Dim myCommand As New OleDb.OleDbCommand("CALL MYLIB.SWAPSTR(?,?)", myConnection)
myCommand.CommandType = CommandType.Text
'execute
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Catch e As Exception
Console.WriteLine(e.Message)
End Try
And finally, the following is a Visual C# version demonstrating the same functionality:
//setup required connection object
string ConnString = "Provider=DB2OLEDB;Password=SNA;Persist Security Info=True;User
ID=SNA;Initial Catalog=MYHOST;Data Source=MYHOST_IP;Network Transport
Library=TCPIP;Network Address=MYHOST;Package Collection=MYLIB;Default
Schema=MYLIB";
OleDbConnection myConnection = new OleDbConnection(ConnString);
6 Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2
OleDbCommand myCommand = new OleDbCommand("CALL MYLIB.SWAPSTR(?,?)", myConnection);
myCommand.CommandType = CommandType.Text;
//execute
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Conclusion
This document demonstrates not only that you can use Host Integration Server 2000 to integrate
existing RPG and a CL application with Windows-based applications, but also that it is a
straightforward process.
This integration is not limited only to RPG and CL applications. DB2 stored procedures support
several other types of external programs including C, COBOL, FORTRAN, and REXX.
Integrating RPG and CL Programs by Using the Microsoft OLE DB Provider for DB2 7