-
Notifications
You must be signed in to change notification settings - Fork 94
CRUD examples for all APIs
This page is tutorial how to create/receive(load)/update/delete items on mikrotik router with all APIs.
The connection to mikrotik router must be opened before performing other operastions:
using (ITikConnection connection = ConnectionFactory.CreateConnection(TikConnectionType.Api_v2))
{
connection.Open(HOST, USER, PASS);
Create IP address via Low-level API
var response = connection.CallCommandSync("/ip/address/add", "=address=192.168.1.1/24", "=interface=eth1");
string id = ((ITikDoneSentence)response.Single()).GetResponseWord();
Create IP address via ADO.NET like API
var createCommand = connection.CreateCommandAndParameters("/ip/address/add",
"address", "192.168.1.1/24",
"interface", "eth1");
var id = createCommand.ExecuteScalar();
Create IP address via Highlevel O/R mapper like API
using tik4net.Objects;
// ...
var address = new Objects.Ip.IpAddress()
{
Address = "192.168.1.1/24",
Interface = "eth1"
};
connection.Save(address);
Load 192.168.1.1/24 IP address details via Low-level API
var response = connection.CallCommandSync("/ip/address/print", "?=address=192.168.1.1/24");
var itemId = response.OfType<ITikReSentence>().Single().GetId();
var itemInterface = response.OfType<ITikReSentence>().Single().GetResponseField("interface");
Load 192.168.1.1/24 IP address details via ADO.NET like API
var loadCmd = connection.CreateCommandAndParameters("/ip/address/print", "address", "192.168.1.1/24");
var response = loadCmd.ExecuteList();
var itemId = response.Single().GetId();
var itemInterface = response.Single().GetResponseField("interface");
Load 192.168.1.1/24 IP address details via Highlevel O/R mapper like API
using tik4net.Objects;
// ...
var ipAddress = connection.LoadSingle<Objects.Ip.IpAddress>(Connection.CreateParameter("address", "192.168.1.1/24"));
var itemId = ipAddress.Id;
var itemInterface = ipAddress.Interface;
Update IP address with given .id - set the comment via Low-level API
// expectation: we know item .id in format *A
var response = connection.CallCommandSync("/ip/address/set", "=comment=test comment", "=.id=192.168.1.1/24");
// should return single ITikDoneSentence sentence, returns ITikTrapSentence sentence when there is a problem
Update IP address with given .id - set the comment via ADO.NET like
// expectation: we know item .id in format *A
var updateCmd = connection.CreateCommandAndParameters("/ip/address/set",
"comment", "test comment",
TikSpecialProperties.Id, id);
updateCmd.ExecuteNonQuery();
// throws exception when there is a problem
Update IP address with given .id - set the comment via Highlevel O/R mapper like API
using tik4net.Objects;
// ...
// expectation: we know item .id in format *A
var address = connection.LoadById<Objects.Ip.IpAddress>(id);
address.Comment = "test comment";
Connection.Save(address);
// throws exception when there is a problem
Delete IP address with given .id via Low-level API
// expectation: we know item .id in format *A
var response = Connection.CallCommandSync("/ip/address/remove", $"=.id={id}");
// should return single ITikDoneSentence sentence, returns ITikTrapSentence sentence when there is a problem
Delete IP address with given .id via ADO.NET like API
// expectation: we know item .id in format *A
var deleteCmd = connection.CreateCommandAndParameters("/ip/address/remove", TikSpecialProperties.Id, id);
deleteCmd.ExecuteNonQuery();
// throws exception when there is a problem
Delete IP address with given .id via Highlevel O/R mapper like API
using tik4net.Objects;
// ...
// expectation: we know item .id in format *A
var ipAddress = connection.LoadById<Objects.Ip.IpAddress>(id);
Connection.Delete(ipAddress);
// throws exception when there is a problem
Delete IP address 192.168.1.1/24 via Low-level API
var findResponse = connection.CallCommandSync("/ip/address/print", "?=address=192.168.1.1/24");
var id = findResponse.OfType<ITikReSentence>().Single().GetId();
var deleteResponse = Connection.CallCommandSync("/ip/address/remove", $"=.id={id}");
// should return single ITikDoneSentence sentence, returns ITikTrapSentence sentence when there is a problem
Delete IP address 192.168.1.1/24 via ADO.NET like API
var loadCmd = connection.CreateCommandAndParameters("/ip/address/print", "address", "192.168.1.1/24");
var findResponse = loadCmd.ExecuteList();
var id = findResponse.Single().GetId();
var deleteCmd = connection.CreateCommandAndParameters("/ip/address/remove", TikSpecialProperties.Id, id);
deleteCmd.ExecuteNonQuery();
// throws exception when there is a problem
Delete IP address 192.168.1.1/24 via Highlevel O/R mapper like API
using tik4net.Objects;
// ...
var ipAddress = connection.LoadSingle<Objects.Ip.IpAddress>(Connection.CreateParameter("address", "192.168.1.1/24"));
Connection.Delete(ipAddress);
// throws exception when there is a problem
- Mikrotik API WIKI
- CRUDTest.cs in sources