Click here to Skip to main content
15,918,243 members
Articles / Desktop Programming / WPF
Tip/Trick

Executing AS400 Commands Programmatically using WPF and C#

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
28 Aug 2015CPOL1 min read 24.2K   258   4   3
Programmatically connecting to AS400 and executing commands

Introduction

In this tip, you are going to learn how to execute AS400 commands using one of the powerful languages, i.e., C#. Though AS400 gives flexibility to run AS400 commands, executing the commands dynamically using C# is always highly preferable. As we all know that when we do like this, we will reap the benefits from both AS400 and .NET environment.

To understand it better, we are going to take one simple example. In real-time scenario, when we have a very huge number of records, it is preferable to take the data in text files with columns delimited by some special characters like |. And then execute AS400/SQL command to transfer the data filled text file to AS400 DB2 or SQL database respectively.

Using the Code

To access AS400, the cwbx DLL (IBM AS/400 iSeries Access for Windows ActiveX Object Library) should be added to the project. This DLL is available in C:\Program Files\IBM\Client Access\Shared folder.

SQL
CREATE TABLE TFRLIB/XYZ (CODE CHAR (10 ) NOT NULL WITH DEFAULT,  
NAME CHAR (40 ) NOT NULL WITH DEFAULT,
DESG CHAR (30 ) NOT NULL  WITH DEFAULT)                           

Model table on AS400 DB2 has been created using this code.

using cwbx;

using System.Data.Odbc;

string lsConnection = "Dsn=Security;uid= KBK9898;pwd=SEP2015d;Force Translate=0;";

OdbcConnection lodbcConn = new OdbcConnection(lsConnection);

string lsInputCommand = string.Format_
	("CPYFRMIMPF FROMSTMF(''{0}'') TOFILE(TFRLIB/xyz) 
MBROPT(*ADD) RCDDLM(*CRLF) FLDDLM(''{1}'') RPLNULLVAL(*FLDDFT) ", _
	"/TESTING/inputxyz.txt", "%");

Here CPYFRMIMPF is Copy from import file command. It is highly desirable to login AS400 make the command and copy it to C# code. In the above command, inputxyz.txt is my huge records input text file which needs to be available in TESTING folder on AS400 system. And in text file, columns are delimited by %.

C#
string lsInputCmdLen = lsInputCommand.Length.ToString ();

lsInputCmdLen = (Convert.ToInt16(lsInputCmdLen) - 4).ToString(); 
//this is because we have added 4 escape characters ' in above lsInputCommand.

string lsAs400Command = string.Format
	("CALL QSYS.QCMDEXC('{0}',0000000{1}.00000)", 
lsInputCommand,Convert.ToInt16(lsInputCmdLen) < 100 ? 
	"0" + lsInputCmdLen : lsInputCmdLen);

lsInputCmdLen should be calculated correctly. If it is wrong, the program will give error as “Error occured.ERROR [HY000] [IBM][iSeries Access ODBC Driver][DB2 UDB]CPF0006 - Errors occurred in command.

C#
OdbcCommand lodbcCommand = new OdbcCommand(lsAs400Command, lodbcConn);
lodbcCommand.CommandType = CommandType.Text;
lodbcConn.Open();
lodbcCommand.ExecuteNonQuery();
lodbcConn.Close();

In real-time scenario, we may use multiple huge text files to insert into different DB2 tables that can be easily achieved by looping the above code in automated/interactive applications.

Please refer to the attachment for a workable test project.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) , Saud Bahwan Group
Oman Oman
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionFormatting fixed-width numbers Pin
John Brett3-Sep-15 0:50
John Brett3-Sep-15 0:50 
Just a suggestion, but your code to add the all-important length checks for <100, and prefixes with an extra "0".
Presumably to ensure fixed-width data.

Use {1:00000}, with however many '0's you need, to get String.Format to do the formatting for you, and pass in the numeric value.

https://msdn.microsoft.com/en-us/library/0c899ak8%28v=vs.110%29.aspx[^]

John
QuestionGreat Idea - You Could Take It Further Pin
Garth J Lancaster28-Aug-15 14:13
professionalGarth J Lancaster28-Aug-15 14:13 
AnswerRe: Great Idea - You Could Take It Further Pin
K Barakathulla Khan29-Aug-15 5:52
K Barakathulla Khan29-Aug-15 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.