Part 2 - Connecting to RTDB

This article is a part of the "Creating an equipment model" tutorial

Next, we need to connect to the database. We can start by simply initializing ABB.Vtrin.cDataLoader object.

// Initialize new dataloader
var dataloader = new ABB.Vtrin.cDataLoader();

A database driver can now be created by calling the data loader's Connect method. It will take the database address, username, and password, and return the database driver.

// Initialize the database driver
var driver = dataloader.Connect(
    data_source,
    db_username,
    db_password,
    false);

Implementing the full method

The following code snippets assume that we have defined couple of class properties - RTDBDriver, RTDBHost, RTDBUsername, and RTDBPassword. The defining can be done in the following way. Please note that you should never push credentials into the repository.

private static ABB.Vtrin.Drivers.cDriverSkeleton RTDBDriver;

private static readonly string RTDBHost = "wss://localhost/history";
private static readonly string RTDBUsername = "username";
private static readonly string RTDBPassword = "password";

Next, we will write a helper method, which helps us to connect to the database, and throws an exception if necessary. Note that dataloader.Connect won't throw any exceptions by itself. We recommend using System.Diagnostics.TraceListener to catch any error messages while making the connection to the database. Let's write a private helper method ConnectOrThrow and add it to our example project.

private static void ConnectOrThrow(
    ABB.Vtrin.cDataLoader dataloader,
    string rtdbHost,
    string rtdbUsername,
    string rtdbPassword)
{
    // Set up a memory stream to catch exceptions
    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        var listener = new System.Diagnostics.TextWriterTraceListener(memoryStream, "connectlistener");
        System.Diagnostics.Trace.Listeners.Add(listener);

        // Set connection options
        dataloader.ConnectOptions =
            ABB.Vtrin.cDataLoader.cConnectOptions.AcceptNewServerKeys
            | ABB.Vtrin.cDataLoader.cConnectOptions.AcceptServerKeyChanges;

        // Initialize the database driver
        RTDBDriver = dataloader.Connect(
            rtdbHost,
            rtdbUsername,
            rtdbPassword,
            false);

        // Unbind the connect listener
        System.Diagnostics.Trace.Listeners.Remove("connectlistener");

        // Case: driver is null, something went wrong
        // > throw an error
        if (RTDBDriver == null)
        {
            // Read stack trace from the memorystream buffer
            string msg = System.Text.Encoding.UTF8.GetString(memoryStream.GetBuffer());
            throw new System.ApplicationException(msg);
        }
    }
}

Now, we can connect to the database following way:

static void Main(string[] args)
{
    var dataloader = new ABB.Vtrin.cDataLoader();

    try
    {
        // Try to connect to the database
        ConnectOrThrow(
            dataloader: dataloader,
            rtdbHost: RTDBHost,
            rtdbUsername: RTDBUsername,
            rtdbPassword: RTDBPassword);

        System.Console.WriteLine("Connection successful!");
    }

    // Case: Something went wrong
    // > Log the error
    catch (System.Exception e)
    {
        System.Console.WriteLine(e.ToString());
    }

    finally
    {
        // Dispose dataloader if necessary
        if (dataloader != null)
            dataloader.Dispose();
    }
}

And that's it! What we have is a code that helps us connect to the RTDB.

📘

Compare your code

This tutorial is following the code that can be found from our GitHub repository. Checking out the full example code is highly recommended.