Skip to content

Commit

Permalink
Add "RowsCopied" property to SqlBulkCopy (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheenamalhotra authored Mar 10, 2020
1 parent a735ee4 commit cb7eda7
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 14 deletions.
19 changes: 19 additions & 0 deletions doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,25 @@ and faster to use a Transact-SQL `INSERT … SELECT` statement to copy the data.
]]></format>
</remarks>
</SqlRowsCopied>
<RowsCopied>
<summary>
The number of rows processed in the ongoing bulk copy operation.
</summary>
<value>
The integer value of the
<see cref="P:Microsoft.Data.SqlClient.SqlBulkCopy.RowsCopied" />
property.
</value>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This value is incremented during the <xref:Microsoft.Data.SqlClient.SqlBulkCopy.SqlRowsCopied> 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 <xref:System.InvalidOperationException>.
]]></format>
</remarks>
</RowsCopied>
<System.IDisposable.Dispose>
<summary>
Releases all resources used by the current instance of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ public event SqlRowsCopiedEventHandler SqlRowsCopied
}
}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml' path='docs/members[@name="SqlBulkCopy"]/RowsCopied/*'/>
public int RowsCopied
{
get
{
return _rowsCopied;
}
}

internal SqlStatistics Statistics
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,15 @@ public event SqlRowsCopiedEventHandler SqlRowsCopied

}

/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml' path='docs/members[@name="SqlBulkCopy"]/RowsCopied/*'/>
public int RowsCopied
{
get
{
return _rowsCopied;
}
}

internal SqlStatistics Statistics
{
get
Expand Down Expand Up @@ -2927,18 +2936,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit cb7eda7

Please sign in to comment.