Bulk Data Loading
In DuckDB Appender can be used to efficiently add rows to the database. Appender always appends to a single table in the database.
To use an appender in .NET, call the CreateCreateRow
and AppendValue
methods to create rows and append data. Make sure to Dispose
the appender to avoid data loss.
Caution
Data types MUST match the length of the database types exactly. For example when inserting into a UBIGINTEGER column, a ulong such as 0UL
must be used. Writing just 0
will cause data corruption by writing adjacent memory to the database.
Example
using var connection = new DuckDBConnection("DataSource=:memory:");
connection.Open();
using (var duckDbCommand = connection.CreateCommand())
{
var table = "CREATE TABLE AppenderTest(foo INTEGER, bar INTEGER);";
duckDbCommand.CommandText = table;
duckDbCommand.ExecuteNonQuery();
}
var rows = 100;
using (var appender = connection.CreateAppender("AppenderTest"))
{
for (var i = 0; i < rows; i++)
{
var row = appender.CreateRow();
row.AppendValue(i).AppendValue(i+2).EndRow();
}
}
For importing data from CSV, Parquet, JSON and other file types see the DuckDB documentation for Data Importing.