Execute IBM i (AS/400) CL Command in C# (.NET) with NTi

Introduction

This tutorial shows how to run a CL command on an IBM i (AS/400) from a C# (.NET) application using NTi Data Provider.

CL (Control Language) commands allow you to interact directly with the IBM i system to automate tasks such as creating libraries, managing objects or running batch jobs.

With NTi, these commands can be executed without going through a 5250 interface, directly from modern .NET code.


Step 1 - Open the connection

Declare a NTiConnection instance and open the connection:

using var conn = new NTiConnection("server=serverName;user=userName;password=password");
conn.Open();

💡 Usin using var ensures the connection is automatically closed and released at the end of the block, even if an error occurs.


Step 2 - Run a CL command

Use the ExecuteClCommand() method of NTiConnection to run a CL command:

conn.ExecuteClCommand("CRTLIB LIB(MYLIB) TEXT('My new library')");

Step 3 - Handle errors

If an error occurs, NTi throws a NTiException containing the IBM i error message. Wrap the call in a try/catch block to catch it:

try
{
    conn.ExecuteClCommand("CRTLIB LIB(MYLIB) TEXT('My new library')");
}
catch (NTiException ex)
{
    Console.WriteLine(ex.Message);
}

Summary

Complete code to run a CL command from .NET with NTi:

using var conn = new NTiConnection("server=serverName;user=userName;password=password");
conn.Open();

try
{
    conn.ExecuteClCommand("CRTLIB LIB(MYLIB) TEXT('My new library')");
}
catch (NTiException ex)
{
    Console.WriteLine(ex.Message);
}

What's next?