Link Search Menu Expand Document

BulkUpdate


This method updates existing rows in the database in bulk, matched by the defined qualifiers. It is supported for Db2.

This page documents the Db2-specific arguments and examples. For the SQL Server implementation, see BulkUpdate (SQL Server).

Call Flow Diagram

The diagram below shows the flow when calling this operation.

flowchart TD
    Client["Client<br/>(RepoDB)"] -->|BulkUpdate| Source["Entities /<br/>DataTable /<br/>DbDataReader"]
    Source --> Pseudo["Create Pseudo Table<br/>(Physical)"]
    Pseudo --> BulkCopy["DB2BulkCopy"]
    BulkCopy -->|Write| PseudoTable[("Pseudo Table")]
    PseudoTable -->|"MERGE INTO ... USING ...<br/>WHEN MATCHED THEN UPDATE<br/>(no WHEN NOT MATCHED)"| Table[("Target Table")]
    PseudoTable -->|Drop| Cleanup(["Pseudo Table Dropped"])

Use Case

Use this method to update rows at high speed. It leverages the native bulk operation from the IBM Data Server .NET Provider via the DB2BulkCopy class.

For updating 1,000 or more rows, prefer this method over UpdateAll.

A pseudo (staging) table is created under a transaction context. The library writes to it via BulkInsert internally, then cascades the changes to the target table via a MERGE statement with no WHEN NOT MATCHED branch — staged rows with no matching target row are left as-is, not inserted. See Operations (Db2) for the underlying mechanics.

Special Arguments

The qualifiers, mappings, bulkCopyTimeout, batchSize and pseudoTableType arguments are available for this operation.

qualifiers defines the fields used to match existing rows, corresponding to the WHERE clause. Defaults to the primary column if not specified.

mappings (via Db2BulkInsertMapItem) defines explicit column mappings between the source properties and the destination columns, with an optional DB2Type override per mapping. When omitted, columns are auto-mapped by name (case-insensitive).

bulkCopyTimeout overrides the command timeout, in seconds.

batchSize overrides the number of rows sent to the server per batch. When not set, all items are sent at once.

pseudoTableType (via Db2BulkImportPseudoTableType) controls the kind of staging table used internally.

Every pseudoTableType value currently resolves to Physical at runtime — see Operations (Db2) for details.

Caveats

This operation creates a pseudo-temporary table internally under a transaction context. The database user must have permission to create tables, or a DB2Exception will be thrown.

The DB2BulkCopy load into the staging table does not participate in the caller-supplied transaction — only the surrounding staging-table DDL and the final MERGE against the real table do.

Usability

Given a list of Person models, the following example bulk-updates rows in the Person table.

using (var connection = new DB2Connection(connectionString))
{
    var updatedRows = connection.BulkUpdate(people);
}

To specify a batch size:

using (var connection = new DB2Connection(connectionString))
{
    var updatedRows = connection.BulkUpdate(people, batchSize: 100);
}

When batchSize is not set, all rows are sent to the server in a single batch.

DataTable

using (var connection = new DB2Connection(connectionString))
{
    var table = ConvertToDataTable(people);
    var updatedRows = connection.BulkUpdate("Person", table);
}

Dictionary/ExpandoObject

using (var sourceConnection = new DB2Connection(sourceConnectionString))
{
    var result = sourceConnection.QueryAll("Person");
    using (var destinationConnection = new DB2Connection(destinationConnectionString))
    {
        var updatedRows = destinationConnection.BulkUpdate("Person", result,
            qualifiers: Field.From("LastName", "DateOfBirth"));
    }
}

DataReader

using (var sourceConnection = new DB2Connection(sourceConnectionString))
{
    using (var reader = sourceConnection.ExecuteReader("SELECT * FROM Person WHERE (IsActive = 1)"))
    {
        using (var destinationConnection = new DB2Connection(destinationConnectionString))
        {
            var rows = destinationConnection.BulkUpdate("Person", reader);
        }
    }
}

To bulk-update via DataEntityDataReader:

using (var connection = new DB2Connection(connectionString))
{
    var people = GetPeople(10000);
    using (var reader = new DataEntityDataReader<Person>(people))
    {
        var updatedRows = connection.BulkUpdate("Person", reader);
    }
}

Field Qualifiers

By default, the primary column is used as the qualifier. To override, pass a list of Field objects in the qualifiers argument.

using (var connection = new DB2Connection(connectionString))
{
    var people = GetPeople(10000);
    var updatedRows = connection.BulkUpdate<Person>(people,
        qualifiers: e => new { e.LastName, e.DateOfBirth });
}

Use indexed columns from the target table as qualifiers to maximize performance.

Column Mappings

Add column mappings using the Db2BulkInsertMapItem class.

var mappings = new List<Db2BulkInsertMapItem>();

// Add the mappings
mappings.Add(new Db2BulkInsertMapItem("SourceId", "DestinationId"));
mappings.Add(new Db2BulkInsertMapItem("SourceName", "DestinationName"));
mappings.Add(new Db2BulkInsertMapItem("SourceIsActive", "DestinationIsActive"));
mappings.Add(new Db2BulkInsertMapItem("SourceDateInsertedUtc", "DestinationDateInsertedUtc"));

// Execute
using (var connection = new DB2Connection(connectionString))
{
    var people = GetPeople(10000);
    var updatedRows = connection.BulkUpdate(people,
        mappings: mappings);
}

Targeting a Table

To target a specific table, pass the literal table name.

using (var connection = new DB2Connection(connectionString))
{
    var people = GetPeople(10000);
    var updatedRows = connection.BulkUpdate("Person", people);
}

Async Method

An equivalent BulkUpdateAsync method is also available.

using (var connection = new DB2Connection(connectionString))
{
    var updatedRows = await connection.BulkUpdateAsync(people);
}