How-To

Connecting C# via ADO.NET


Userlevel 6
Badge
  • Community Manager
  • 22 replies

his is a short example showing how to connect to Data Virtuality from C# using the Npgsql - an open source ADO.NET Data Provider for PostgreSQL. 

 

Prerequisites

Install the Npgsql using the NuGet Package Manager in Visual Studio or follow one of the alternative methods outlined in the official Npgsql installation guide.

 

Sample code

This sample is just an illustration and is by no means a production ready code. The official Npgsql documentation brings many more examples for Connection string parameters and Basic usage snippets using asynchronous calls.

The sample connects to a Data Virtuality platform and lists the configured Data Sources with some of their properties. To get the connection parameters such as username and port for your Data Virtuality Platform Saas, log into the platform and click on the Account > Preferences in the top-right corner.

 

using System;using System.Data;using System.Data.Odbc;using System.Data.Common;using Npgsql;class Data_Virtuality_ADO_NET_Test{    static void Main(string[] args)    {        if (args.Length != 5)        {            Console.WriteLine("Usage: dvtest -host <hostname> -port <ODBC port> -username <User name> -password <Password> ");            Console.WriteLine("Example: dvtest -host beta.platform.datavirtuality.com -port 45009 -username myusername -password mypassword");        }        else        {            string host = "", port = "", username = "", password = "";            for (int i = 1; i < args.Length; i++)            {                switch (args[i])                {                    case "-host":                        host = args[++i];                        break;                    case "-port":                        port = args[++i];                        break;                    case "-username":                        username = args[++i];                        break;                    case "-password":                        password = args[++i];                        break;                    default:                        Console.WriteLine("Unknown option: " + args[i]);                        Environment.Exit(1);                    break;                }            }            var connectionString = "Host=" + host + ";" +                                   "Port=" + port + ";" +                                   "Username=" + username + ";" +                                   "Password=" + password + ";" +                                   "Database=datavirtuality;" +                                   "SSL Mode=Require;" +                                   "Include Error Detail=true;" +                                   "Trust Server Certificate=true;" +                                   "Server Compatibility Mode=NoTypeLoading";            using var dataSource = NpgsqlDataSource.Create(connectionString);            using var connection = dataSource.OpenConnection();            using var command = new NpgsqlCommand("SELECT id, name, translator, modelProperties, translatorProperties FROM SYSADMIN.DataSources", connection);            using var reader = command.ExecuteReader();            Console.WriteLine("Results:");            while (reader.Read())            {                Console.WriteLine($"ID: {reader["id"]} Source name: {reader["name"]} Translator: {reader["translator"]} Model Properties: {reader["modelProperties"]}");            }         }    }}

0 replies

Be the first to reply!

Reply