Table of Contents

Getting Started

stable next patch vnext build

The best way to use Npgsql is to install its nuget package.

Npgsql aims to be fully ADO.NET-compatible, its API should feel almost identical to other .NET database drivers.

Here's a basic code snippet to get you started:

var connectionString = "Host=myserver;Username=mylogin;Password=mypass;Database=mydatabase";
await using var dataSource = NpgsqlDataSource.Create(connectionString);

// Insert some data
await using (var cmd = dataSource.CreateCommand("INSERT INTO data (some_field) VALUES ($1)"))
{
    cmd.Parameters.AddWithValue("Hello world");
    await cmd.ExecuteNonQueryAsync();
}

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

You can find more info about the ADO.NET API in the MSDN docs or in many tutorials on the Internet.