Table of Contents

Class DataBulkCopy

Namespace
FluentCommand.Bulk
Assembly
FluentCommand.SqlServer.dll

Provides a fluent API for performing SqlBulkCopy operations to efficiently copy large amounts of data into a SQL Server table.

public class DataBulkCopy : DisposableBase, IDisposable, IAsyncDisposable, IDataBulkCopy
Inheritance
DataBulkCopy
Implements
Inherited Members

Constructors

DataBulkCopy(IDataSession, string)

Initializes a new instance of the DataBulkCopy class for the specified data session and destination table.

public DataBulkCopy(IDataSession dataSession, string destinationTable)

Parameters

dataSession IDataSession

The data session used for the bulk copy operation.

destinationTable string

The name of the destination table in SQL Server.

Methods

AutoMap(bool)

Automatically creates column mappings using the DataTable column names for both source and destination. Only supported for overloads that take DataTable or IDataReader.

public IDataBulkCopy AutoMap(bool value = true)

Parameters

value bool

true to automatically create mapping columns; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

BatchSize(int)

Sets the number of rows in each batch. At the end of each batch, the rows in the batch are sent to the server.

public IDataBulkCopy BatchSize(int value)

Parameters

value int

The number of rows in each batch.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

BulkCopyTimeout(int)

Sets the number of seconds for the operation to complete before it times out.

public IDataBulkCopy BulkCopyTimeout(int value)

Parameters

value int

The timeout duration, in seconds.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

CheckConstraints(bool)

Checks constraints while data is being inserted. By default, constraints are not checked.

public IDataBulkCopy CheckConstraints(bool value = true)

Parameters

value bool

true to check constraints; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

EnableStreaming(bool)

Enables or disables streaming data from an IDataReader object.

public IDataBulkCopy EnableStreaming(bool value = true)

Parameters

value bool

true to enable streaming; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

FireTriggers(bool)

Causes the server to fire the insert triggers for the rows being inserted into the database.

public IDataBulkCopy FireTriggers(bool value = true)

Parameters

value bool

true to fire insert triggers; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Ignore(int)

Ignores the specified source column by removing it from the mapped columns collection.

public IDataBulkCopy Ignore(int sourceColumnOrdinal)

Parameters

sourceColumnOrdinal int

The ordinal position of the source column within the data source.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Ignore(string)

Ignores the specified source column by removing it from the mapped columns collection.

public IDataBulkCopy Ignore(string sourceColumn)

Parameters

sourceColumn string

The source column to remove from mapping.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

KeepIdentity(bool)

Preserves source identity values. When not specified, identity values are assigned by the destination.

public IDataBulkCopy KeepIdentity(bool value = true)

Parameters

value bool

true to preserve source identity values; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

KeepNulls(bool)

Preserves null values in the destination table regardless of the settings for default values. When not specified, null values are replaced by default values where applicable.

public IDataBulkCopy KeepNulls(bool value = true)

Parameters

value bool

true to preserve null values; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Mapping(int, int)

Creates a new column mapping using column ordinals to refer to source and destination columns.

public IDataBulkCopy Mapping(int sourceColumnOrdinal, int destinationOrdinal)

Parameters

sourceColumnOrdinal int

The ordinal position of the source column within the data source.

destinationOrdinal int

The ordinal position of the destination column within the destination table.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Mapping(int, string)

Creates a new column mapping using a column ordinal to refer to the source column and a column name for the target column.

public IDataBulkCopy Mapping(int sourceColumnOrdinal, string destinationColumn)

Parameters

sourceColumnOrdinal int

The ordinal position of the source column within the data source.

destinationColumn string

The name of the destination column within the destination table.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Mapping(string, int)

Creates a new column mapping using a column name to refer to the source column and a column ordinal for the target column.

public IDataBulkCopy Mapping(string sourceColumn, int destinationOrdinal)

Parameters

sourceColumn string

The name of the source column within the data source.

destinationOrdinal int

The ordinal position of the destination column within the destination table.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Mapping(string, string)

Creates a new column mapping using column names to refer to source and destination columns.

public IDataBulkCopy Mapping(string sourceColumn, string destinationColumn)

Parameters

sourceColumn string

The name of the source column within the data source.

destinationColumn string

The name of the destination column within the destination table.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Mapping<TEntity>(Action<DataBulkCopyMapping<TEntity>>)

Creates a new column mapping using a strongly typed builder for mapping entity properties to destination columns.

public IDataBulkCopy Mapping<TEntity>(Action<DataBulkCopyMapping<TEntity>> builder) where TEntity : class

Parameters

builder Action<DataBulkCopyMapping<TEntity>>

The entity mapping builder delegate.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

Type Parameters

TEntity

The type of the entity.

NotifyAfter(int)

Sets the number of rows to be processed before generating a notification event.

public IDataBulkCopy NotifyAfter(int value)

Parameters

value int

The number of rows to process before notification.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

TableLock(bool)

Obtains a bulk update lock for the duration of the bulk copy operation. When not specified, row locks are used.

public IDataBulkCopy TableLock(bool value = true)

Parameters

value bool

true to obtain a bulk update lock; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

UseInternalTransaction(bool)

Specifies that each batch of the bulk-copy operation will occur within a transaction.

public IDataBulkCopy UseInternalTransaction(bool value = true)

Parameters

value bool

true to use an internal transaction for each batch; otherwise, false.

Returns

IDataBulkCopy

The same IDataBulkCopy instance for fluent chaining.

WriteToServer(DataRow[])

Copies all rows from the supplied DataRow array to the destination table using bulk copy.

public void WriteToServer(DataRow[] rows)

Parameters

rows DataRow[]

An array of DataRow objects to be copied to the destination table.

WriteToServer(DataTable)

Copies all rows in the supplied DataTable to the destination table using bulk copy.

public void WriteToServer(DataTable table)

Parameters

table DataTable

A DataTable whose rows will be copied to the destination table.

WriteToServer(DataTable, DataRowState)

Copies only rows that match the supplied row state in the supplied DataTable to the destination table using bulk copy.

public void WriteToServer(DataTable table, DataRowState rowState)

Parameters

table DataTable

A DataTable whose rows will be copied to the destination table.

rowState DataRowState

A value from the DataRowState enumeration. Only rows matching the row state are copied to the destination.

WriteToServer(IDataReader)

Copies all rows in the supplied IDataReader to the destination table using bulk copy.

public void WriteToServer(IDataReader reader)

Parameters

reader IDataReader

An IDataReader whose rows will be copied to the destination table.

WriteToServer<TEntity>(IEnumerable<TEntity>)

Copies all items in the supplied IEnumerable<T> to the destination table using bulk copy.

public void WriteToServer<TEntity>(IEnumerable<TEntity> data) where TEntity : class

Parameters

data IEnumerable<TEntity>

An enumerable collection of entities to be copied to the destination table.

Type Parameters

TEntity

The type of the data elements.