Skip to content

ADO.NET like API

Daniel Frantík edited this page May 12, 2026 · 8 revisions

Second option is to use ADO.NET like API. This API tries to be very similar to the database API used in the .NET world. It allows you to create ITikCommand commands, use parameters, and other features like ADO.NET does.

The main entry point is the MikroTik router connection object (ITikConnection). An instance of the connection object should be instantiated via a factory (ConnectionFactory). The ITikCommand instance should be created via ITikConnection methods.

using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);
ITikCommand command = connection.CreateCommand("/system/identity/print");
string identity = cmd.ExecuteScalar();
Console.WriteLine("Router identity: {0}", identity);

##How to create command

  • connection.CreateCommand
  • connection.CreateCommandAndParameters
ITikCommand command = connection.CreateCommand("/system/identity/print");

##Command methods

  • ExecuteNonQuery - executes command on router and ensures that the operation was successful.
  • ExecuteScalar - executes command on router and ensures that operation returns one value (=ret parameter), which is returned as result.
  • ExecuteSingleRow - executes command on router and ensures that operation returns exactly one row (1x !re and 1x !done) as result.
  • ExecuteList - executes command on router and returns all result sentences (all !re sentences) as result.
  • ExecuteListWithDuration - Executes command on router and returns all result sentences (all !re sentences) which are returned during given duration. After this period, command is automatically stopped via Cancel command.
  • ExecuteAsync - executes command on router asynchronously. Response is returned via callback when it is read from mikrotik (for tag, which has been dynamically assigned). Running command should be later canceled via Cancel method.

##How to use parameters Parameters are similar to DB ones. Instance of ITikCommandParameter should be created via connection.CreateParameter method.

var cmd = connection.CreateCommand("/log/" + logLevelCommandSufix,
    connection.CreateParameter("message", message));
cmd.ExecuteNonQuery();

##Support for asynchronous commands Sometimes you need to perform a command for a longer time and read response messages. A typical example is using torch. For this reason the command.ExecuteAsync method is provided. It handles all .tag magic (see mikrotik wiki) in the background.

Async command support methods:

  • command.ExecuteAsync
  • command.Cancel
  • command.CancelAndJoin
ITikCommand torchCmd = connection.CreateCommand("/tool/torch", 
    connection.CreateParameter("interface", "ether1"), 
    connection.CreateParameter("port", "any"),
    connection.CreateParameter("src-address", "0.0.0.0/0"),
    connection.CreateParameter("dst-address", "0.0.0.0/0")
);
torchCmd.ExecuteAsync(response => Console.WriteLine("Row: " + response.GetResponseField("tx")));

Console.WriteLine("Press ENTER");
Console.ReadLine();
torchCmd.CancelAndJoin();

Clone this wiki locally