From f1d3eb7177934d5761825a2f26cf85ccfc92e2cd Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 4 Feb 2020 13:39:58 -0800 Subject: [PATCH 1/2] Add "RowsCopied" property to SqlBulkCopy --- .../Microsoft.Data.SqlClient/SqlBulkCopy.xml | 19 ++++++++++++ .../Microsoft/Data/SqlClient/SqlBulkCopy.cs | 9 ++++++ .../Microsoft/Data/SqlClient/SqlBulkCopy.cs | 31 ++++++++++++------- .../SQL/SqlBulkCopyTest/Bug84548.cs | 3 +- .../SQL/SqlBulkCopyTest/Bug85007.cs | 2 ++ .../SQL/SqlBulkCopyTest/Bug98182.cs | 3 +- .../SQL/SqlBulkCopyTest/CopyAllFromReader1.cs | 2 ++ .../SqlBulkCopyTest/CopyAllFromReaderAsync.cs | 4 ++- 8 files changed, 59 insertions(+), 14 deletions(-) diff --git a/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml b/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml index 6c643017da..e6cfd6ef37 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml @@ -471,6 +471,25 @@ and faster to use a Transact-SQL `INSERT … SELECT` statement to copy the data. ]]> + + + The number of rows processed in the ongoing bulk copy operation. + + + The integer value of the + + property. + + + event and does not imply that these many rows have been sent to the server or committed. + +During the execution of a bulk copy operation, this value can be accessed, but it cannot be changed. Any attempt to change it will throw an . +]]> + + Releases all resources used by the current instance of the diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs index 22297b4dc7..726ff3427c 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs @@ -395,6 +395,15 @@ public event SqlRowsCopiedEventHandler SqlRowsCopied } } + /// + public int RowsCopied + { + get + { + return _rowsCopied; + } + } + internal SqlStatistics Statistics { get diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs index ca46138662..72db681eb0 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs @@ -471,6 +471,15 @@ public event SqlRowsCopiedEventHandler SqlRowsCopied } + /// + public int RowsCopied + { + get + { + return _rowsCopied; + } + } + internal SqlStatistics Statistics { get @@ -2906,18 +2915,18 @@ private void CopyBatchesAsyncContinuedOnError(bool cleanupParser) { tdsReliabilitySection.Start(); #endif //DEBUG - if ((cleanupParser) && (_parser != null) && (_stateObj != null)) - { - _parser._asyncWrite = false; - Task task = _parser.WriteBulkCopyDone(_stateObj); - Debug.Assert(task == null, "Write should not pend when error occurs"); - RunParser(); - } + if ((cleanupParser) && (_parser != null) && (_stateObj != null)) + { + _parser._asyncWrite = false; + Task task = _parser.WriteBulkCopyDone(_stateObj); + Debug.Assert(task == null, "Write should not pend when error occurs"); + RunParser(); + } - if (_stateObj != null) - { - CleanUpStateObject(); - } + if (_stateObj != null) + { + CleanUpStateObject(); + } #if DEBUG } finally diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug84548.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug84548.cs index 35ecee4374..b41feb55fc 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug84548.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug84548.cs @@ -62,7 +62,8 @@ public static void Test(string srcConstr, string dstConstr, string targettable) ColumnMappings.Add("CustomerID", "CustomerID"); bulkcopy.WriteToServer(reader); - bulkcopy.Close(); + + DataTestUtility.AssertEqualsWithDescription(bulkcopy.RowsCopied, 1, "Unexpected number of rows."); } } } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug85007.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug85007.cs index 833b996637..f9983bc8d7 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug85007.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug85007.cs @@ -90,6 +90,8 @@ public static void Test(string srcConstr, string dstConstr, string dstTable) ColumnMappings.Add("ShipCountry", "ShipCountry"); bulkcopy.WriteToServer(reader); + + DataTestUtility.AssertEqualsWithDescription(bulkcopy.RowsCopied, 830, "Unexpected number of rows."); } Helpers.VerifyResults(dstConn, dstTable, 14, 830); } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug98182.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug98182.cs index b831679815..c6e629b40a 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug98182.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/Bug98182.cs @@ -47,8 +47,9 @@ public static void Test(string constr, string dstTable) ColumnMappings.Add("col 2", "[col 2]"); bulkcopy.WriteToServer(reader); + + DataTestUtility.AssertEqualsWithDescription(bulkcopy.RowsCopied, 1, "Unexpected number of rows."); } - Helpers.VerifyResults(dstConn, dstTable, 2, 1); } } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader1.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader1.cs index 3e8f41a1cb..2e43147789 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader1.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader1.cs @@ -34,6 +34,8 @@ public static void Test(string srcConstr, string dstConstr, string dstTable) ColumnMappings.Add("FirstName", "col3"); bulkcopy.WriteToServer(reader); + + DataTestUtility.AssertEqualsWithDescription(bulkcopy.RowsCopied, 5, "Unexpected number of rows."); } Helpers.VerifyResults(dstConn, dstTable, 3, 5); } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReaderAsync.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReaderAsync.cs index 99e25c5af4..8f55cb1710 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReaderAsync.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReaderAsync.cs @@ -49,8 +49,10 @@ private static async Task TestAsync(string srcConstr, string dstConstr, string d bulkcopy.DestinationTableName = dstTable; await bulkcopy.WriteToServerAsync(reader); + await outputSemaphore.WaitAsync(); + + DataTestUtility.AssertEqualsWithDescription(bulkcopy.RowsCopied, 5, "Unexpected number of rows."); } - await outputSemaphore.WaitAsync(); Helpers.VerifyResults(dstConn, dstTable, 3, 5); } } From eff495e80068b8b9e4dc65a4bc26ff9ba489613e Mon Sep 17 00:00:00 2001 From: Cheena Malhotra Date: Tue, 11 Feb 2020 11:38:23 -0800 Subject: [PATCH 2/2] Address wording --- doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml b/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml index e6cfd6ef37..31e025fa4e 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml @@ -484,7 +484,7 @@ and faster to use a Transact-SQL `INSERT … SELECT` statement to copy the data. event and does not imply that these many rows have been sent to the server or committed. +This value is incremented during the event and does not imply that this number of rows has been sent to the server or committed. During the execution of a bulk copy operation, this value can be accessed, but it cannot be changed. Any attempt to change it will throw an . ]]>