Skip to content

SqlBulkCopy: Add RowsCopied Property #136

@fowl2

Description

@fowl2

It is is difficult to find out how many rows SqlBulkCopy.WriteToServer[Async] has copied. This is a problem when using streaming as DbDataReader doesn't provide this information either.

Describe the solution you'd like

class SqlBulkCopy
{
...
  public long RowsCopied { get; }
...
}

It would be cleaner to change the return type of WriteToServer[Async] to long, but I think this would technically be a breaking change? This would also have the advantage of not having to worry about multiple calls or reads before writing.

Describe alternatives you've considered

  • setting NotifyAfter to 1, subscribing to SqlRowsCopied and then storing it somewhere from the event handler. Cons: overhead of calling a delegate, plus the allocation of EventArgs per row.
  • fully buffer to a DataTable. Cons: whole dataset in memory.
  • wrapping IDataReader and keeping count of the number of times Read() is called. Cons:
    doesn't work for async readers.
  • wrapping DbDataReader. Cons: lots of boilerplate, can't call protected methods on the source leading to unintended behaviour divergence.
  • SELECT COUNT(*) FROM DESTINATION. Cons: network round trip, concurrency issues.
  • Reflecting on the private _rowsCopied field. Cons: reflection, brittle.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions