Table of Contents

Getting Started

ADO.NET Provider and Low-Level Library

There are two ways to work with DuckDB from C#: You can use ADO.NET Provider or use a low-level bindings library for DuckDB. The ADO.NET Provider is built on top of the low-level library and is the recommended and most straightforward approach to work with DuckDB.

In both cases, two NuGet packages are available: The Full package that includes the DuckDB native library and a managed-only library that doesn't include a native library.

ADO.NET Provider Includes DuckDB library
DuckDB.NET.Bindings
DuckDB.NET.Bindings.Full
DuckDB.NET.Data
DuckDB.NET.Data.Full

Using ADO.NET Provider

dotnet add package DuckDB.NET.Data.Full

DuckDB.NET follows the ADO.NET provider model and its API. The following snippet shows how to run basic operations:

using var duckDBConnection = new DuckDBConnection("Data Source=file.db");
duckDBConnection.Open();

using var command = duckDBConnection.CreateCommand();
command.CommandText = "CREATE TABLE integers(foo INTEGER, bar INTEGER);";
var executeNonQuery = command.ExecuteNonQuery();

command.CommandText = "INSERT INTO integers VALUES (3, 4), (5, 6), (7, NULL);";
executeNonQuery = command.ExecuteNonQuery();

command.CommandText = "Select count(*) from integers";
var count = command.ExecuteScalar();

command.CommandText = "SELECT foo, bar FROM integers";
var reader = command.ExecuteReader();

for (var index = 0; index < queryResult.FieldCount; index++)
{
    var column = queryResult.GetName(index);
    Console.Write($"{column} ");
}

Console.WriteLine();

while (reader.Read())
{
    for (int ordinal = 0; ordinal < reader.FieldCount; ordinal++)
    {
        if (queryResult.IsDBNull(ordinal))
        {
            Console.WriteLine("NULL");
            continue;
        }

        var val = queryResult.GetValue<int>(ordinal);
        Console.Write(val);
        Console.Write(" ");
    }
}