From ed90d66e48b138bea38d04bd88f564daaeb8d9db Mon Sep 17 00:00:00 2001 From: mgravell Date: Fri, 30 Aug 2019 14:19:23 +0100 Subject: [PATCH] add missing APIs to BulkCopy --- Dapper.ProviderTools/BulkCopy.cs | 26 +++++++++++++++++-- .../Internal/DynamicBulkCopy.cs | 4 +++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Dapper.ProviderTools/BulkCopy.cs b/Dapper.ProviderTools/BulkCopy.cs index 38e496b0a..7808f31c3 100644 --- a/Dapper.ProviderTools/BulkCopy.cs +++ b/Dapper.ProviderTools/BulkCopy.cs @@ -30,6 +30,20 @@ public abstract class BulkCopy : IDisposable return DynamicBulkCopy.Create(obj); } + /// + /// Create a BulkCopy instance for the connection provided + /// + public static BulkCopy Create(DbConnection connection) + { + var bcp = TryCreate(connection); + if (bcp == null) + { + if (connection == null) throw new ArgumentNullException(nameof(connection)); + throw new NotSupportedException("Unable to create BulkCopy for " + connection.GetType().FullName); + } + return bcp; + } + /// /// Provide an external registration for a given connection type /// @@ -74,15 +88,23 @@ private static readonly ConcurrentDictionary?> /// /// Write a set of data to the server /// + public abstract void WriteToServer(DataRow[] source); + /// + /// Write a set of data to the server + /// public abstract void WriteToServer(IDataReader source); /// /// Write a set of data to the server /// - public abstract Task WriteToServerAsync(DbDataReader source, CancellationToken cancellationToken); + public abstract Task WriteToServerAsync(DbDataReader source, CancellationToken cancellationToken = default); + /// + /// Write a set of data to the server + /// + public abstract Task WriteToServerAsync(DataTable source, CancellationToken cancellationToken = default); /// /// Write a set of data to the server /// - public abstract Task WriteToServerAsync(DataTable source, CancellationToken cancellationToken); + public abstract Task WriteToServerAsync(DataRow[] source, CancellationToken cancellationToken = default); /// /// Add a mapping between two columns by name /// diff --git a/Dapper.ProviderTools/Internal/DynamicBulkCopy.cs b/Dapper.ProviderTools/Internal/DynamicBulkCopy.cs index 36188b9f2..2505b4119 100644 --- a/Dapper.ProviderTools/Internal/DynamicBulkCopy.cs +++ b/Dapper.ProviderTools/Internal/DynamicBulkCopy.cs @@ -32,6 +32,8 @@ public override void AddColumnMapping(int sourceColumn, int destinationColumn) public override void WriteToServer(DataTable source) => _wrapped.WriteToServer(source); + public override void WriteToServer(DataRow[] source) + => _wrapped.WriteToServer(source); public override void WriteToServer(IDataReader source) => _wrapped.WriteToServer(source); @@ -41,6 +43,8 @@ public override Task WriteToServerAsync(DbDataReader source, CancellationToken c public override Task WriteToServerAsync(DataTable source, CancellationToken cancellationToken) => _wrapped.WriteToServer(source, cancellationToken); + public override Task WriteToServerAsync(DataRow[] source, CancellationToken cancellationToken) + => _wrapped.WriteToServer(source, cancellationToken); protected override void Dispose(bool disposing) {