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#.
The OS used in the following example is MacOS. If you use other types of OS, change the code accordingly.
1. Prepare the environment
-
Download C# from its official website and then install the .Net Core SDK as instructed.
tipSkip this step if C# is installed in your environment.
-
Add the dotnet path to the PATH environment variable:
export PATH=/usr/local/share/dotnet:$PATH
source ~/.bash_profile
2. Create a C# program
-
Create a C# project:
dotnet new console -o relyt_demo
-
Go to the relyt_demo directory:
cd relyt_demo
-
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.
Variable Description endpoint_domain The public endpoint for connecting to the Relyt DW service unit. endpoint_port The port for connecting to the Relyt DW service unit. The default port is 5432. dw_user The name of the DW user that connects to the Relyt DW service unit. dw_user_password The password of the DW user. database The 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.
-
Add Npgsql to the project:
dotnet add package Npgsql
3. Run the program
Run the program:
dotnet run