Skip to content

Add an overload of SqlBulkCopy.WriteToServer that takes an IEnumerable<object[]> #142

Open
@mburbea

Description

@mburbea

Is your feature request related to a problem? Please describe.

Often I find myself needing to dump some content quickly into sql server, and map the columns sequentially. While I can create a DataRow[], or populate a DataTable, those remove the ability for me to stream data. Requiring me to buffer the entire dataset before sending it to the server.

The only solution that works that retains streaming is to create a DbDataReader and I often end up writing a simple adapter to do just that.

Which means I end writing something like::

await bulkCopy.WriteToServerAsync(new DbDataReaderAdapter(myData.Select(x=> new[]{
x.Field1,
...
x.ComplicatedThingy();
}, NumberOfColumns)), cancellationToken);

Describe the solution you'd like

I think adding the following methods would be ideal.

public class SqlBulkCopy
{
      public Task WriteToServerAsync(IEnumerable<object[]> rows, CancellationToken cancellationToken = default);
      public void WriteToServer(IEnumerable<object[]> rows);
}

This would save writing the adapter into my code base and get the desired result.
Ultimately, SqlBulkCopy only currently calls the following methods on a non SqlDataReader

bool Read();
int FieldCount {get;}
object GetValue(int ordinal);
bool IsDbNull(int ordinal);

Describe alternatives you've considered

There are obviously tools like SSIS and Import/Export wizard, but if I need to do this programatically (e.g. calling a webapi and dump the content), there's no easier way than sql bulk copy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancement 💡Issues that are feature requests for the drivers we maintain.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions