Docs / Use Bulk Insert

Use Bulk Insert

What this page covers#

This page explains the practical bulk API: WriteToTableAsync(...).

Typed rows example#

</> C#
var rows = new[]
{
    new UserImportRow
    {
        Email = "ada@example.com",
        DisplayName = "Ada Lovelace",
        Age = 36,
        IsActive = true,
        CreatedUtc = new DateTime(2024, 01, 02, 03, 04, 05),
        UpdatedUtc = null
    }
};

await db.Bulk.WriteToTableAsync("dbo.tbl_User", rows);

DataTable example#

</> C#
using System.Data;

var table = new DataTable();
table.Columns.Add("Email", typeof(string));
table.Columns.Add("DisplayName", typeof(string));
table.Columns.Add("Age", typeof(int));
table.Columns.Add("IsActive", typeof(bool));
table.Columns.Add("CreatedUtc", typeof(DateTime));
table.Columns.Add("UpdatedUtc", typeof(DateTime));

table.Rows.Add("ada@example.com", "Ada Lovelace", 36, true, new DateTime(2024, 01, 02, 03, 04, 05), DBNull.Value);

await db.Bulk.WriteToTableAsync("dbo.tbl_User", table);

With options#

</> C#
var options = new MooBulkOptions
{
    PreparationSql = "DELETE FROM [dbo].[tbl_User];",
    CleanupSql = "UPDATE [dbo].[tbl_User] SET [DisplayName] = [DisplayName] + N' Imported';"
};

await db.Bulk.WriteToTableAsync("dbo.tbl_User", rows, options);