Getting started
Your first database connection
On this page
This example opens an existing PostgreSQL database and reads its version. You need a reachable PostgreSQL server and a URL stored in the DUBURL_DATABASE_URL environment variable.
Install the packages
dotnet add package DubUrl
dotnet add package Npgsql
Register the provider and open a connection
using System.Data.Common;
using DubUrl;
DbProviderFactories.RegisterFactory("Npgsql", Npgsql.NpgsqlFactory.Instance);
var url = Environment.GetEnvironmentVariable("DUBURL_DATABASE_URL")
?? throw new InvalidOperationException("Set DUBURL_DATABASE_URL first.");
using var connection = new ConnectionUrl(url).Open();
using var command = connection.CreateCommand();
command.CommandText = "SELECT version()";
Console.WriteLine(command.ExecuteScalar());
Use a URL such as pgsql://{user}:{password}@{host}/{database}, substituting your database’s values. The using declarations dispose the command and connection when the scope ends.