Controlling an AS/400 or System i with c#

Search for a command to run...

No comments yet. Be the first to comment.
Just a very short blog post... for everyone who is not yet known to it. Since .Net Framework 4.6 for two immutable types we got new static fields in the framework: Array.Empty<T> string.Empty // instead of string foo = ""; byte[] bar = new byte[0...

Introduction In one of my older projects, I often had to synchronize and work with different parallel threads. Using async, parallel or 'real' threads does not make a real difference when it is coming to thread safety. As a very short short instructi...

In part III we take a closer look at where to upgrade our UserInterface from WindowsForms. As NyxSql was created as a learning project more than a decade ago, there is no MVVM or anything else implemented, which will make the upgrade more hard as the...

When using package manager with sources which require to authenticate yourself (e. g. internal sources or paid subscriptions), it may happen that you need to change your password or just entered the wrong credentials. Now, it is kinda like 'evil', be...

When working with multiple workstations (workstation / laptop) due to pandemic, we suddenly experienced the following error when opening solutions: Current solution contains incorrect configurations mappings. It may cause projects to not work correct...

Some time ago we had to develop and deploy a new system side application for our customers. As our customer base is using mainly Windows and System i, and our whole new system configurations have been developed in c# we had to decide which way we go. Either rewrite the current codebase in c++ and port it to System i or find another solution. As c++ is very specific on the System i, it would have been an own development on its own.
After some digging in the internet I came across some articles (sadly I haven't saved them, so I cannot reference these) which described the execution of scripts by using sql (strsql) on the System i.
I installed the IBM i Access client solution on my dev workstation, including the ODBC-Drivers for DB2 and started up Visual Studio. As a side note: at this time there was no native .net DB2 driver and later on I never got it really running - however as we want to control the System i and not transfer insert/update many data in the database, I did not care about using ODBC.
The whole integration ended up in using:
A specific connection string should be used, to set the required library list:
Driver={Client Access ODbC Driver (32-bit)};System=i5system;Uid=user;Pwd=password;DefaultLibraries=ITLIB1,ITLIB2
By executing a normal 'non query' and QSYS.QCMDEXC we can execute any command. Here we will execute a CL procedure in ITLIB2 which is called TESTCN. The CL procedure which we call will receive 2 parameters and the 'native' call on the System i is stored in the variable clCmd:
string clCmd = "CALL PGM(ITLIB2/TESTCN) PARM('1stParam' '2condParam')";
The call itself, please note that we need to escape the single quotes ' when using them in the sql query. Furthermore, QCMDEXC does require the length of the command string in 15,5 packed format.
string pgmParm = $"CALL QSYS.QCMDEXC('{cmdtext.Replace("'", "''")}', {clCmd.Length.ToString("0000000000.00000").Replace(',', '.')})";
An error at calling will throw an exception (OdbcException) which can then be examined for the exact error code.
For reading spool files with SQL, SYSTOOLS with SPOOLED_FILE_DATA is available.
Naturally, a native port would have been much better implementation BUT by creating some CL procedures and building up an architecture where the platform calls are handled by specific procedures/functions we got a fluent integration which can run from any windows operating system.