Skip to main content

C#

Relyt supports the PostgreSQL connection protocol, which means any tools or software designed for connecting to PostgreSQL can also be used for Relyt connections.

This guide will walk you through the process of connecting to Relyt using C#.

info

The OS used in the following example is MacOS. If you use other types of OS, change the code accordingly.

1. Prepare the environment

  1. Download C# from its official website and then install the .Net Core SDK as instructed.

    tip

    Skip this step if C# is installed in your environment.

  2. Add the dotnet path to the PATH environment variable:

    export PATH=/usr/local/share/dotnet:$PATH
    source ~/.bash_profile

2. Create a C# program

  1. Create a C# project:

    dotnet new console -o relyt_demo
  2. Go to the relyt_demo directory:

    cd relyt_demo
  3. Replace the content in file Program.cs with the following code:

    using System;
    using Npgsql;

    class Program
    {
    static void Main()
    {
    var cs = "Host=<endpoint-domain>;Username=<dw_user>;Password=<dw_user_password>;Database=<db_name>";

    using var con = new NpgsqlConnection(cs);
    con.Open();

    using var cmd = new NpgsqlCommand();
    cmd.Connection = con;

    cmd.CommandText = "DROP TABLE IF EXISTS items;";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "CREATE TABLE IF NOT EXISTS items (id INTEGER, name VARCHAR(50));";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "INSERT INTO items (id, name) VALUES (1, 'item1'), (2, 'item2');";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "SELECT * FROM items;";
    using NpgsqlDataReader rdr = cmd.ExecuteReader();

    while (rdr.Read())
    {
    Console.WriteLine($"{rdr.GetInt32(0)} {rdr.GetString(1)}");
    }
    }
    }

    The following table describes the related variables. Replace them with the actual values when in actual use.

    VariableDescription
    endpoint_domainThe public endpoint for connecting to the Relyt DW service unit.
    endpoint_portThe port for connecting to the Relyt DW service unit. The default port is 5432.
    dw_userThe name of the DW user that connects to the Relyt DW service unit.
    dw_user_passwordThe password of the DW user.
    databaseThe name of the Relyt database to connect.

    The SQL commands provided in the example above are for reference only. You can replace them with any SQL commands you want to run.

  4. Add Npgsql to the project:

    dotnet add package Npgsql

3. Run the program

Run the program:

dotnet run