dotnet / dotnet/SqlClient

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

Open
#142 2 comments 4 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

### 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](https://gist.github.com/mburbea/015d5f5c5349f6e209f82580bdf62439) to do just that.

Which means I end writing something like::
```cs
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.
```cs
public class SqlBulkCopy
{
public Task WriteToServerAsync(IEnumerable rows, CancellationToken cancellationToken = default);
public void WriteToServer(IEnumerable 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`
```cs
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.