Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i

HashSet<string> destColumnNames = new HashSet<string>();

Dictionary<string, bool> columnMappingStatusLookup = new Dictionary<string, bool>();
// Loop over the metadata for each column
_SqlMetaDataSet metaDataSet = internalResults[MetaDataResultId].MetaData;
_sortedColumnMappings = new List<_ColumnMapping>(metaDataSet.Length);
Expand All @@ -569,9 +570,16 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
int assocId;
for (assocId = 0; assocId < _localColumnMappings.Count; assocId++)
{
if (!columnMappingStatusLookup.ContainsKey(_localColumnMappings[assocId].DestinationColumn))
{
columnMappingStatusLookup.Add(_localColumnMappings[assocId].DestinationColumn, false);
}

if ((_localColumnMappings[assocId]._destinationColumnOrdinal == metadata.ordinal) ||
(UnquotedName(_localColumnMappings[assocId]._destinationColumnName) == metadata.column))
{
columnMappingStatusLookup[_localColumnMappings[assocId].DestinationColumn] = true;

if (rejectColumn)
{
nrejected++; // Count matched columns only
Expand Down Expand Up @@ -703,6 +711,7 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
break;
}
}

if (assocId == _localColumnMappings.Count)
{
// Remove metadata for unmatched columns
Expand All @@ -713,7 +722,17 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
// All columnmappings should have matched up
if (nmatched + nrejected != _localColumnMappings.Count)
{
throw (SQL.BulkLoadNonMatchingColumnMapping());
List<string> unmatchedColumns = new List<string>();

foreach(KeyValuePair<string, bool> keyValuePair in columnMappingStatusLookup)
{
if (!keyValuePair.Value)
{
unmatchedColumns.Add(keyValuePair.Key);
}
}

throw SQL.BulkLoadNonMatchingColumnName(unmatchedColumns);
}

updateBulkCommandText.Append(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i

HashSet<string> destColumnNames = new HashSet<string>();

Dictionary<string, bool> columnMappingStatusLookup = new Dictionary<string, bool>();
// Loop over the metadata for each column
_SqlMetaDataSet metaDataSet = internalResults[MetaDataResultId].MetaData;
_sortedColumnMappings = new List<_ColumnMapping>(metaDataSet.Length);
Expand All @@ -604,9 +605,16 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
int assocId;
for (assocId = 0; assocId < _localColumnMappings.Count; assocId++)
{
if (!columnMappingStatusLookup.ContainsKey(_localColumnMappings[assocId].DestinationColumn))
{
columnMappingStatusLookup.Add(_localColumnMappings[assocId].DestinationColumn, false);
}

if ((_localColumnMappings[assocId]._destinationColumnOrdinal == metadata.ordinal) ||
(UnquotedName(_localColumnMappings[assocId]._destinationColumnName) == metadata.column))
{
columnMappingStatusLookup[_localColumnMappings[assocId].DestinationColumn] = true;

if (rejectColumn)
{
nrejected++; // Count matched columns only
Expand Down Expand Up @@ -754,7 +762,17 @@ private string AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleResultSet i
// All columnmappings should have matched up
if (nmatched + nrejected != _localColumnMappings.Count)
{
throw (SQL.BulkLoadNonMatchingColumnMapping());
List<string> unmatchedColumns = new List<string>();

foreach(KeyValuePair<string, bool> keyValuePair in columnMappingStatusLookup)
{
if (!keyValuePair.Value)
{
unmatchedColumns.Add(keyValuePair.Key);
}
}

throw SQL.BulkLoadNonMatchingColumnName(unmatchedColumns);
}

updateBulkCommandText.Append(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,10 @@ internal static Exception BulkLoadNonMatchingColumnName(string columnName)
{
return BulkLoadNonMatchingColumnName(columnName, null);
}
internal static Exception BulkLoadNonMatchingColumnName(IEnumerable<string> columns)
{
return BulkLoadNonMatchingColumnName(String.Join(",", columns), null);
}
internal static Exception BulkLoadNonMatchingColumnName(string columnName, Exception e)
{
return ADP.InvalidOperation(StringsHelper.GetString(Strings.SQL_BulkLoadNonMatchingColumnName, columnName), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<Compile Include="SQL\SqlBulkCopyTest\FireTrigger.cs" />
<Compile Include="SQL\SqlBulkCopyTest\Helpers.cs" />
<Compile Include="SQL\SqlBulkCopyTest\MissingTargetColumn.cs" />
<Compile Include="SQL\SqlBulkCopyTest\MissingTargetColumns.cs" />
<Compile Include="SQL\SqlBulkCopyTest\MissingTargetTable.cs" />
<Compile Include="SQL\SqlBulkCopyTest\SqlBulkCopyTest.cs" />
<Compile Include="SQL\SqlBulkCopyTest\Transaction.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static void Test(string srcConstr, string dstConstr, string dstTable)
ColumnMappings.Add("LastName", "col2"); // this column does not exist
ColumnMappings.Add("FirstName", "col3");

string errorMsg = SystemDataResourceManager.Instance.SQL_BulkLoadNonMatchingColumnMapping;
string errorMsg = SystemDataResourceManager.Instance.SQL_BulkLoadNonMatchingColumnName;
errorMsg = string.Format(errorMsg, "col2");

DataTestUtility.AssertThrowsWrapper<InvalidOperationException>(() => bulkcopy.WriteToServer(reader), exceptionMessage: errorMsg);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Data.Common;

namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public class MissingTargetColumns
{
public static void Test(string srcConstr, string dstConstr, string dstTable)
{
using (SqlConnection dstConn = new SqlConnection(dstConstr))
using (SqlCommand dstCmd = dstConn.CreateCommand())
{
dstConn.Open();

try
{
Helpers.TryExecute(dstCmd, "create table " + dstTable + " (col1 int, col2 nvarchar(10))");

using (SqlConnection srcConn = new SqlConnection(srcConstr))
using (SqlCommand srcCmd = new SqlCommand("select top 5 EmployeeID, LastName, FirstName from employees", srcConn))
{
srcConn.Open();

using (DbDataReader reader = srcCmd.ExecuteReader())
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(dstConn))
{
bulkcopy.DestinationTableName = dstTable;
SqlBulkCopyColumnMappingCollection ColumnMappings = bulkcopy.ColumnMappings;

ColumnMappings.Add("EmployeeID", "col1");
ColumnMappings.Add("LastName", "col3"); // this column does not exist
ColumnMappings.Add("FirstName", "col4"); // this column does not exist

string errorMsg = SystemDataResourceManager.Instance.SQL_BulkLoadNonMatchingColumnName;
errorMsg = string.Format(errorMsg, "col3,col4");

DataTestUtility.AssertThrowsWrapper<InvalidOperationException>(() => bulkcopy.WriteToServer(reader), exceptionMessage: errorMsg);
}
}
}
finally
{
Helpers.TryExecute(dstCmd, "drop table " + dstTable);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public void MissingTargetColumnTest()
MissingTargetColumn.Test(_connStr, _connStr, AddGuid("SqlBulkCopyTest_MissingTargetColumn"));
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public void MissingTargetColumnsTest()
{
MissingTargetColumns.Test(_connStr, _connStr, AddGuid("SqlBulkCopyTest_MissingTargetColumns"));
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public void Bug85007Test()
{
Expand Down