Table of Contents

Class NpgsqlBinaryImporter

Namespace
Npgsql
Assembly
Npgsql.dll

Provides an API for a binary COPY FROM operation, a high-performance data import mechanism to a PostgreSQL table. Initiated by BeginBinaryImport(string)

public sealed class NpgsqlBinaryImporter : IDisposable, IAsyncDisposable
Inheritance
NpgsqlBinaryImporter
Implements
Inherited Members

Remarks

Properties

Timeout

Current timeout

public TimeSpan Timeout { set; }

Property Value

TimeSpan

Methods

Close()

Terminates the ongoing binary import and puts the connection back into the idle state, where regular commands can be executed.

Note that if Complete() hasn't been invoked before calling this, the import will be cancelled and all changes will be reverted.

public void Close()

CloseAsync(CancellationToken)

Async terminates the ongoing binary import and puts the connection back into the idle state, where regular commands can be executed.

Note that if CompleteAsync(CancellationToken) hasn't been invoked before calling this, the import will be cancelled and all changes will be reverted.

public ValueTask CloseAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask

Complete()

Completes the import operation. The writer is unusable after this operation.

public ulong Complete()

Returns

ulong

CompleteAsync(CancellationToken)

Completes the import operation. The writer is unusable after this operation.

public ValueTask<ulong> CompleteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask<ulong>

Dispose()

Terminates the ongoing binary import and puts the connection back into the idle state, where regular commands can be executed.

Note that if Complete() hasn't been invoked before calling this, the import will be cancelled and all changes will be reverted.

public void Dispose()

DisposeAsync()

Async terminates the ongoing binary import and puts the connection back into the idle state, where regular commands can be executed.

Note that if CompleteAsync(CancellationToken) hasn't been invoked before calling this, the import will be cancelled and all changes will be reverted.

public ValueTask DisposeAsync()

Returns

ValueTask

StartRow()

Starts writing a single row, must be invoked before writing any columns.

public void StartRow()

StartRowAsync(CancellationToken)

Starts writing a single row, must be invoked before writing any columns.

public Task StartRowAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

WriteAsync<T>(T, NpgsqlDbType, CancellationToken)

Writes a single column in the current row as type npgsqlDbType.

public Task WriteAsync<T>(T value, NpgsqlDbType npgsqlDbType, CancellationToken cancellationToken = default)

Parameters

value T

The value to be written

npgsqlDbType NpgsqlDbType

In some cases T isn't enough to infer the data type to be written to the database. This parameter can be used to unambiguously specify the type. An example is the JSONB type, for which T will be a simple string but for which npgsqlDbType must be specified as Jsonb.

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task

Type Parameters

T

The .NET type of the column to be written.

WriteAsync<T>(T, string, CancellationToken)

Writes a single column in the current row as type dataTypeName.

public Task WriteAsync<T>(T value, string dataTypeName, CancellationToken cancellationToken = default)

Parameters

value T

The value to be written

dataTypeName string

In some cases T isn't enough to infer the data type to be written to the database. This parameter and be used to unambiguously specify the type.

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task

Type Parameters

T

The .NET type of the column to be written.

WriteAsync<T>(T, CancellationToken)

Writes a single column in the current row.

public Task WriteAsync<T>(T value, CancellationToken cancellationToken = default)

Parameters

value T

The value to be written

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

Returns

Task

Type Parameters

T

The type of the column to be written. This must correspond to the actual type or data corruption will occur. If in doubt, use Write<T>(T, NpgsqlDbType) to manually specify the type.

WriteNull()

Writes a single null column value.

public void WriteNull()

WriteNullAsync(CancellationToken)

Writes a single null column value.

public Task WriteNullAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

WriteRow(params object?[])

Writes an entire row of columns. Equivalent to calling StartRow(), followed by multiple Write<T>(T) on each value.

public void WriteRow(params object?[] values)

Parameters

values object[]

An array of column values to be written as a single row

WriteRowAsync(CancellationToken, params object?[])

Writes an entire row of columns. Equivalent to calling StartRow(), followed by multiple Write<T>(T) on each value.

public Task WriteRowAsync(CancellationToken cancellationToken = default, params object?[] values)

Parameters

cancellationToken CancellationToken

An optional token to cancel the asynchronous operation. The default value is None.

values object[]

An array of column values to be written as a single row

Returns

Task

Write<T>(T)

Writes a single column in the current row.

public void Write<T>(T value)

Parameters

value T

The value to be written

Type Parameters

T

The type of the column to be written. This must correspond to the actual type or data corruption will occur. If in doubt, use Write<T>(T, NpgsqlDbType) to manually specify the type.

Write<T>(T, NpgsqlDbType)

Writes a single column in the current row as type npgsqlDbType.

public void Write<T>(T value, NpgsqlDbType npgsqlDbType)

Parameters

value T

The value to be written

npgsqlDbType NpgsqlDbType

In some cases T isn't enough to infer the data type to be written to the database. This parameter can be used to unambiguously specify the type. An example is the JSONB type, for which T will be a simple string but for which npgsqlDbType must be specified as Jsonb.

Type Parameters

T

The .NET type of the column to be written.

Write<T>(T, string)

Writes a single column in the current row as type dataTypeName.

public void Write<T>(T value, string dataTypeName)

Parameters

value T

The value to be written

dataTypeName string

In some cases T isn't enough to infer the data type to be written to the database. This parameter and be used to unambiguously specify the type.

Type Parameters

T

The .NET type of the column to be written.