DuckDB.NET.Data 0.4.0.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package DuckDB.NET.Data --version 0.4.0.10                
NuGet\Install-Package DuckDB.NET.Data -Version 0.4.0.10                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DuckDB.NET.Data" Version="0.4.0.10" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DuckDB.NET.Data --version 0.4.0.10                
#r "nuget: DuckDB.NET.Data, 0.4.0.10"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DuckDB.NET.Data as a Cake Addin
#addin nuget:?package=DuckDB.NET.Data&version=0.4.0.10

// Install DuckDB.NET.Data as a Cake Tool
#tool nuget:?package=DuckDB.NET.Data&version=0.4.0.10                

DuckDB.NET

DuckDB bindings for C#

GitHub Workflow Status Coveralls License Ko-Fi

alternate text is missing from this package README image alternate text is missing from this package README image

Project Icon

Note: The library is in early stage and contributions are more than wellcome.

Usage

Before you Begin

Before you begin, make sure that duckdb binary is available on your machine and accessible to your project. The easiest option is to put the library in your project directory and set Copy to Output Directory to Copy Always. Make sure to use the correct binary for your platform: duckdb.dll on Windows, duckdb.so on Linux and duckdb.dylib on macOS.

Getting Started

There are two ways to work with DuckDB from C#

Using ADO.NET Provider or using 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 way for working with DuckDB.

Using ADO.NET Provider

dotnet add package DuckDB.NET.Data
using (var duckDBConnection = new DuckDBConnection("Data Source=file.db"))
{
  duckDBConnection.Open();

  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, 8);";
  executeNonQuery = command.ExecuteNonQuery();

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

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

  PrintQueryResults(reader);
}

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

  Console.WriteLine();

  while (queryResult.Read())
  {
    for (int ordinal = 0; ordinal < queryResult.FieldCount; ordinal++)
    {
      var val = queryResult.GetInt32(ordinal);
      Console.Write(val);
      Console.Write(" ");
    }

    Console.WriteLine();
  }
}
Parameterized queries and DuckDB native types.

Starting from version 0.4.0.10, DuckDB.NET.Data supports executing parameterized queries and reading all built-in native duckdb types:

using var connection = new DuckDBConnection("DataSource=:memory:");
connection.Open();

var command = connection.CreateCommand();
command.CommandText = "SELECT * from integers where foo > ?;";
command.Parameters.Add(new new DuckDBParameter(3));

using var reader = command.ExecuteReader();

To read DuckDB specific native types use DuckDBDataReader.GetFieldValue<T> method. The following table shows the mapping between DuckDB native type and DuckDB.NET.Data .Net type:

DuckDB Type .Net Type
INTERVAL DuckDBInterval
DATE DuckDBDateOnly
TIME DuckDBTimeOnly
HUGEINT BigInteger

Dapper

You can also use Dapper to query data:

var item = duckDBConnection.Query<FooBar>("SELECT foo, bar FROM integers");

In-Memory database

For in-memory database use Data Source=:memory: connection string. When using in-memory database no data is persisted on disk.

Use low level bindings library

dotnet add package DuckDB.NET.Bindings
var result = Startup.DuckDBOpen(null, out var database);

using (database)
{
  result = Startup.DuckDBConnect(database, out var connection);
  using (connection)
  {
    result = Query.DuckDBQuery(connection, "CREATE TABLE integers(foo INTEGER, bar INTEGER);", out var queryResult);
    result = Query.DuckDBQuery(connection, "INSERT INTO integers VALUES (3, 4), (5, 6), (7, 8);", out queryResult);
    result = Query.DuckDBQuery(connection, "SELECT foo, bar FROM integers", out queryResult);

    PrintQueryResults(queryResult);

    result = Query.DuckDBPrepare(connection, "INSERT INTO integers VALUES (?, ?)", out var insertStatement);

    using (insertStatement)
    {
      result = Query.DuckDBBindInt32(insertStatement, 1, 42); // the parameter index starts counting at 1!
      result = Query.DuckDBBindInt32(insertStatement, 2, 43);

      result = Query.DuckDBExecutePrepared(insertStatement, out var _);
    }


    result = Query.DuckDBPrepare(connection, "SELECT * FROM integers WHERE foo = ?", out var selectStatement);

    using (selectStatement)
    {
      result = Query.DuckDBBindInt32(selectStatement, 1, 42);

      result = Query.DuckDBExecutePrepared(selectStatement, out queryResult);
    }

    PrintQueryResults(queryResult);

    // clean up
    Query.DuckDBDestroyResult(out queryResult);
  }
}

private static void PrintQueryResults(DuckDBResult queryResult)
{
  var columnCount = Query.DuckDBColumnCount(queryResult);
  for (var index = 0; index < columnCount; index++)
  {
    var columnName = Query.DuckDBColumnName(queryResult, index).ToManagedString(false);
    Console.Write($"{columnName} ");
  }

  Console.WriteLine();

  for (long row = 0; row < queryResult.RowCount; row++)
  {
    for (long column = 0; column < queryResult.ColumnCount; column++)
    {
      var val = Types.DuckDBValueInt32(queryResult, column, row);
      Console.Write(val);
      Console.Write(" ");
    }

    Console.WriteLine();
  }
}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on DuckDB.NET.Data:

Package Downloads
DuckDB.InteractiveExtension

DuckDB support for Polyglot Notebooks

Microsoft.SemanticKernel.Connectors.DuckDB

DuckDB connector for Semantic Kernel plugins and semantic memory

Microsoft.DotNet.Interactive.DuckDB

Microsoft.DotNet.Interactive.Kernel implementation for DuckDB

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on DuckDB.NET.Data:

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
dotnet/interactive
.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
Version Downloads Last updated
1.1.3 764 11/7/2024
1.1.2.1 10,596 10/21/2024
1.1.2-alpha.5 50 10/10/2024
1.1.1 7,548 9/24/2024
1.1.0.1 988 9/9/2024
1.0.2 3,365 7/22/2024
1.0.1 44,252 6/22/2024
1.0.0 4,902 6/3/2024
0.10.3 741 5/22/2024
0.10.2 19,613 4/21/2024
0.10.1 567 3/18/2024
0.9.2 131,543 11/14/2023
0.9.1 27,160 10/13/2023
0.9.0.3 5,377 10/4/2023
0.9.0 1,053 10/2/2023
0.8.1 97,847 7/8/2023
0.8.0 16,756 5/17/2023
0.7.1 390 3/4/2023
0.6.1 264 1/8/2023
0.4.0.10 1,293 9/3/2022
0.4.0.1 1,482 7/6/2022
0.4.0 468 6/27/2022
0.2.9.1 2,976 9/24/2021
0.2.9 375 9/15/2021
0.1.3 377 9/3/2021
0.1.2 431 4/12/2021
0.1.1 384 4/9/2021
0.1.0 688 11/12/2020

Added support for parameterized queries, reading and writing all supported DuckDB types