The 2024 CrateDB architecture guide covering all key concepts is out.

Download now
Skip to content
Product

How to Connect with .NET

Seamlessly connect to CrateDB in .NET using the Npgsql data provider.

NET-Logo

Seamlessly connect to CrateDB using .NET with the Npgsql data provider, the open-source solution for PostgreSQL. Utilizing Npgsql, you can effortlessly establish connections and interact with the PostgreSQL server using .NET languages like C#, Visual Basic, and F#. For further details and technical specifications, explore the documentation for Npgsql

        

using Npgsql;

var connString = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase";

var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
var dataSource = dataSourceBuilder.Build();

var conn = await dataSource.OpenConnectionAsync();

// Insert some data
await using (var cmd = new NpgsqlCommand("INSERT INTO data (some_field) 
VALUES (@p)", conn))
{
    cmd.Parameters.AddWithValue("p", "Hello world");
    await cmd.ExecuteNonQueryAsync();
}

// Retrieve all rows
await using (var cmd = new NpgsqlCommand("SELECT some_field FROM data",
conn))
await using (var reader = await cmd.ExecuteReaderAsync())
{
    while (await reader.ReadAsync())
        Console.WriteLine(reader.GetString(0));
}
        
        
        

Additional resources