Skip to content

Commit 70b8535

Browse files
committed
SqlBulkCopy: Explicitly state column name that mismatches(#3170)
1 parent 2013a71 commit 70b8535

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
572572
if ((_localColumnMappings[assocId]._destinationColumnOrdinal == metadata.ordinal) ||
573573
(UnquotedName(_localColumnMappings[assocId]._destinationColumnName) == metadata.column))
574574
{
575+
_localColumnMappings[assocId]._matchedOrRejected = true;
576+
575577
if (rejectColumn)
576578
{
577579
nrejected++; // Count matched columns only
@@ -713,7 +715,16 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
713715
// All columnmappings should have matched up
714716
if (nmatched + nrejected != _localColumnMappings.Count)
715717
{
716-
throw (SQL.BulkLoadNonMatchingColumnMapping());
718+
int assocId;
719+
for (assocId = 0; assocId < _localColumnMappings.Count; assocId++)
720+
{
721+
SqlBulkCopyColumnMapping columnMapping = _localColumnMappings[assocId];
722+
723+
if(!columnMapping._matchedOrRejected)
724+
{
725+
throw (SQL.BulkLoadNonMatchingColumnName(columnMapping.SourceColumn));
726+
}
727+
}
717728
}
718729

719730
updateBulkCommandText.Append(")");

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
607607
if ((_localColumnMappings[assocId]._destinationColumnOrdinal == metadata.ordinal) ||
608608
(UnquotedName(_localColumnMappings[assocId]._destinationColumnName) == metadata.column))
609609
{
610+
_localColumnMappings[assocId]._matchedOrRejected = true;
611+
610612
if (rejectColumn)
611613
{
612614
nrejected++; // Count matched columns only
@@ -754,7 +756,16 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
754756
// All columnmappings should have matched up
755757
if (nmatched + nrejected != _localColumnMappings.Count)
756758
{
757-
throw (SQL.BulkLoadNonMatchingColumnMapping());
759+
int assocId;
760+
for (assocId = 0; assocId < _localColumnMappings.Count; assocId++)
761+
{
762+
SqlBulkCopyColumnMapping columnMapping = _localColumnMappings[assocId];
763+
764+
if(!columnMapping._matchedOrRejected)
765+
{
766+
throw (SQL.BulkLoadNonMatchingColumnName(columnMapping.SourceColumn));
767+
}
768+
}
758769
}
759770

760771
updateBulkCommandText.Append(")");

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopyColumnMapping.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public sealed class SqlBulkCopyColumnMapping
1313
internal int _destinationColumnOrdinal;
1414
internal string _sourceColumnName;
1515
internal int _sourceColumnOrdinal;
16+
internal bool _matchedOrRejected;
1617

1718
// devnote: we don't want the user to detect the columnordinal after WriteToServer call.
1819
// _sourceColumnOrdinal(s) will be copied to _internalSourceColumnOrdinal when WriteToServer executes.

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/MissingTargetColumn.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public static void Test(string srcConstr, string dstConstr, string dstTable)
3535
ColumnMappings.Add("LastName", "col2"); // this column does not exist
3636
ColumnMappings.Add("FirstName", "col3");
3737

38-
string errorMsg = SystemDataResourceManager.Instance.SQL_BulkLoadNonMatchingColumnMapping;
38+
string errorMsg = SystemDataResourceManager.Instance.SQL_BulkLoadNonMatchingColumnName;
39+
errorMsg = string.Format(errorMsg, "LastName");
40+
3941
DataTestUtility.AssertThrowsWrapper<InvalidOperationException>(() => bulkcopy.WriteToServer(reader), exceptionMessage: errorMsg);
4042
}
4143
}

0 commit comments

Comments
 (0)