mercredi 6 novembre 2013

Créer un fichier AS400 avec C#

Inspiré de l'article http://www.experts-exchange.com/OS/AS_-_400/Q_24367101.html, voici comment créer, via C#, des fichiers sous AS400 (IBM iSeries). Ou, plus exactement, comment exécuter et paramétrer la commande CRTPF de création de fichiers.

  1. Référencer la dll cwbx.dll (cette dll est installée avec le programme IBM Client Access)
  2. Ecrire le code suivant :
AS400System as400 = new AS400System();
Command cmd = new Command();

            try
            {
                as400.Define("IP serveur AS400");
                as400.UserID = "Utilisateur";
                as400.Password = "Mot de passe";
                as400.PromptMode = cwbcoPromptModeEnum.cwbcoPromptNever;
                as400.Signon();
                cmd.system = as400;

                string command = string.Format("CRTPF FILE({0}/{1}) RCDLEN({2}) TEXT('{3}')", "Bibliothèque AS400", "Fichier à créer", "Taille du fichier", "Commentaires");
                cmd.Run(command);

                MessageBox.Show("Fichier créé", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                as400.Disconnect(cwbcoServiceEnum.cwbcoServiceAll);
            }


Voilà, c'est tout!