Dot Net Fluke: Getting by on C# for iSeries RPG Developers

Useful tutorials on C# and .NET for RPG iSeries AS/400 developers. Brought to you from the folks at AranRock Consulting

2/11/08

Comparing Loops in RPG and C#

Looping operations are quite similar in C# and RPG. Note that RPG uses 'EndDo' or 'EndFor' and C# uses curly braces {} as the beginning and end of a loop (or any code block for that matter).

Here's the different loops
A = 0; B=10
'Statements' is all the code in the loop that you wish to iterate over.

RPG Loop C# Loop
DOW (A<b); statements EndDo; While (a <b)  {statements}
FOR A = 1 to 10;  statements  EndFor; For (int a=0; a<10; a++)  {statements}
DOU A > B; statements  EndDo; Do   {statements}
while (A >B);
   

 

Both languages have ways to skip statements or break out of the loop

iter; Continue;
Leave; break;

 

Here's sample code showing the C# while loop (same as RPG DOW)
Note that the While operation code has {} braces to denote the beginning and end of the loop.

The Do While loop...

            // The while loop
// The while statement is equivalent to the RPG 'DOW'
int ebayJunk = 0;
int CreditLeft = 500;
bool HaveMoney = true;
// The loop will execute as long as HaveMoney is true;
while (HaveMoney) // checks before going into the loop
{ // beginning of while loop
ebayJunk += 1;
CreditLeft -= 100;
Console.WriteLine("Useless Gadgets={0}", ebayJunk);
Console.WriteLine("Money left=${0}", CreditLeft);

if (CreditLeft <= 0) { HaveMoney = false; } // Set condition for while loop
// Entire loop is executed a
} // end of while loop (same as EndDO in RPG
Console.WriteLine("You're Broke!!");
Console.ReadLine();
And the For loop....
// For loop example. Finds the first blank space in a string
// The block of code between the curly braces gets iterated
// over. The loop decrements from the length



String field = "To the Galaxy and Beyond";



for(int i = field.Length; i > 0; i--)


{
Console.WriteLine("Letter is not blank: "+ field[i - 1]);



if(field[i - 1] == ' ')   // note how I can treat a string like an array


    break; // exit once I reach a blank space


}


 














Share this post :

Labels: , ,

2/7/08

Converting from a numeric date to a date type in C# and RPG

Let's compare one of the most common and confusing tasks for all programmers - date conversion.
Given that this is such as a common programming task I am always surprised by how complex this is in many languages.
Here's our problem. Our iSeries legacy table 'Orders' is holding its order date not in the iSeries Date type but in numeric 8,0 in ISO format YYYYMMDD. e.g. ORDATE = 20081021
We need to convert this to a date type in RPG so that we can do some date work - adding a day on it subtracting order date from ship date etc.

RPG Code
Line 1 is the D-spec entry to define the converted date.
Line 2 defines the input order date (typically this comes from a db)

D OrderDateD S d datfmt(*iso)
D ORDATE S 8 0
OrderDateD = %date(ORDATE:*ISO);
// now we cand do some date work with OrderDateD!


Now lets do the same thing in C#
C# Code


Console.WriteLine("Method 2 - ParseExact Method a little simpler");
int ORDATE = 20081210;
DateTime OrderDateD = DateTime.ParseExact(ORDATE.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
Console.WriteLine("Date as numeric entered YYYYYMMDD {0}", OrderDateD);

In C# you use the ParseExact method to convert from YYYYMMDD or any other format.
1. Create a date type OderDate with the statement 'DateTime OrderDateD'
2. Assign it the value of ORDATE after parsing it first

The ParseExact method takes 3 parameters
a) The input date in whatever format but converted to a string. We use ToString to convert
b) The fomat of the input date which is specified here as YYYYMMDD. Click here for formats
c) The 'invariant' culture property to tell the method that this is a non standard format. How geeky!

There is another method called 'Parse' but ParseExact is used here because you can specify (in the second parm) the EXACT format that may not confom to any particular standard. This is really handy for handling obscue legacy stored dates in old iSeries tables.
The Parse method though is really clever so when you are using standard date formats it can automatically figue out your date format without telling it what format you are using.

I think its fair to say that the RPG approach is simpler and easier to understand. However there is more flexibility in the C# approach.

DOWNLOADS
See the C# code as a full project which shows both Parse and ParseExact
Also here's an RPG date cheat sheet code adapated from a post by Mitchel Laman
Which fomats to use when describing your input? Click here

Labels: , , , , ,

2/1/08

Getting data off the iSeries part 2

Someone asked me to expand on the Data Access overview from .NET I gave previously.
If you want more information on this, go to the Programmer's Toolkit included with IBM's client access. It has excellent information with sample code.
Here's an overview from the Programmer's Toolkit :

.NET Framework Classes - The Managed Provider
If you are using the .NET framework to connect to the iseries use the IBM.Data.DB2.iSeries database provider for application development. This is in c:/program files/IBM/Client Access/ as a dll. This managed provider will provide better performance than using the System.Data.OleDb provider to bridge to the iSeries Access OLE DB provider, or using the Microsoft.Data.Odbc provider to bridge to the iSeries Access ODBC driver.

IBM.Data.DB2.iSeries database provider aka Managed Provider aka IBM DB2 UDB for iSeries .NET Provider. They all refer to the same integrated iSeries Access for processing DB2 using ADO.NET.
These are the interfaces supported
Provider Class Name Interface(s) supported
iDB2Connection DbConnection
iDB2Command DbCommand
iDB2DataReader DataReader; IDataRecord
iDB2DataAdapter DbDataAdapter (inherits from IDataAdapter)
iDB2Parameter DataParameter; IDbDataParameter
iDB2ParameterCollection IDataParameterCollection
iDB2Transaction DbTransaction
iDB2DbType iSeries-specific types
iDB2Error iSeries-specific errors
iDB2ErrorCollection Collection, IEnumerable
iDB2Exception Provider-specific exceptions
iDB2CommandBuilder iSeries-specific command builder
advantages

advantages
Easy to program
Allows easy integration of SQL commands and result data with other data sources
Application processes SQL commands directly into an ADO.NET DataSet object
Application places SQL result data into an ADO.NET DataSet object
Provides a minimal set of interfaces between the application and the iSeries database
Increases performance without sacrificing functionality

disadvantage
Record level access, program calls, and data queues are not supported.

ADO/OLE DB
The IBMDA400 and IBMDASQL providers allow you to send remote SQL statements to the iSeries server. There is support for calling most SQL statements on the iSeries server.
The OLE DB provider record-level access support provides the ability to do the following:

- open logical or physical files
- access records sequentially or by key
- read, insert, update, and delete records
- perform commitment control
- work with multiple file members
- work with multiple record formats

advantages
Easy to program to.
Provides SQL access and record-level access to database files.
Excellent record level access performance.

disadvantages
Limited application optimization potential due to standard interface.
Limited support for native DB2 UDB functions.



ActiveX automation objects
The iSeries Access for Windows ActiveX automation object library provides a set of automation objects to allow easy access to iSeries host systems, data queues, programs and commands and call RPG programs.
This is the CWBX.DLL library that you need to add to solution explorer.

- accessing iSeries data queues
- calling iSeries server APIs and user programs
- managing connections and validating security
- running CL commands on the iSeries server
- performing data type and code page conversions
- transferring database data to and from the iSeries server


advantages
Easy to program to.
Automatic conversion to and from many popular PC file formats.
Provides high-level object for easiest data transfers and low-level objects for customized data transfers.
Supports creating, modifying, saving, and executing data transfer request files.
Request files are interchangeable with the Data Transfer GUI and batch applications.

disadvantages
The database SQL support is limited to SELECTs and full file updates.
No support for record level access.
Slower performance in performing individual file/individual row updates than OLE DB record level access.
No future updates planned for this interface.


ODBCadvantagesApplications capable of accessing database files on servers other than iSeries (using non-iSeries Access for Windows ODBC driver) disadvantagesDifficult to program to, though easier than C API.Limited support for native DB2 UDB functions.
.
The above is summarized from IBM's Programmer's toolkit documentation.