Skip to content

Commit 638a2ab

Browse files
committed
Feedback Changes
1 parent a3075fb commit 638a2ab

4 files changed

Lines changed: 82 additions & 3 deletions

File tree

doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopyOptions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ To see how the option changes the way the bulk load works, run the sample with t
7878
</para>
7979
<para>
8080
The cache is automatically invalidated when <see cref="P:Microsoft.Data.SqlClient.SqlBulkCopy.DestinationTableName" /> is changed to a different table.
81+
Changing <see cref="P:Microsoft.Data.SqlClient.SqlBulkCopy.ColumnMappings" /> between operations does not require cache invalidation because the cached metadata describes only the destination table schema, not the source-to-destination column mapping.
8182
</para>
8283
</summary>
8384
</CacheMetadata>

src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTesting.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@
311311
<ProjectReference Include="SQL\UdtTest\UDTs\Utf8String\Utf8String.csproj">
312312
<Name>Utf8String</Name>
313313
</ProjectReference>
314-
<ProjectReference Include="$(TestsPath)tools\TDS\TDS.End.Point\TDS.End.Point.csproj" />
314+
<ProjectReference Include="$(TestsPath)tools\TDS\TDS.EndPoint\TDS.EndPoint.csproj" />
315315
<ProjectReference Include="$(TestsPath)tools\TDS\TDS.Servers\TDS.Servers.csproj" />
316316
<ProjectReference Include="$(TestsPath)tools\TDS\TDS\TDS.csproj" />
317317
<ProjectReference Include="$(TestsPath)tools\Microsoft.Data.SqlClient.TestUtilities\Microsoft.Data.SqlClient.TestUtilities.csproj" />

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

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public static void Test(string dstConstr, string dstTable)
233233
{
234234
string initialQuery = string.Format(initialQueryTemplate, dstTable);
235235

236-
DataTable sourceData = new();
236+
using DataTable sourceData = new();
237237
sourceData.Columns.Add("col1", typeof(int));
238238
sourceData.Columns.Add("col2", typeof(string));
239239
sourceData.Columns.Add("col3", typeof(string));
@@ -267,6 +267,78 @@ public static void Test(string dstConstr, string dstTable)
267267
}
268268
}
269269

270+
public class CacheMetadataColumnMappingsChange
271+
{
272+
private static readonly string initialQueryTemplate = "create table {0} (col1 int, col2 nvarchar(50), col3 nvarchar(50))";
273+
274+
// Test that changing ColumnMappings between WriteToServer calls works correctly with CacheMetadata.
275+
// The cached metadata describes the destination table schema, not the column mappings,
276+
// so modifying mappings between calls should work without cache invalidation.
277+
public static void Test(string dstConstr, string dstTable)
278+
{
279+
string initialQuery = string.Format(initialQueryTemplate, dstTable);
280+
281+
using DataTable sourceData = new DataTable();
282+
sourceData.Columns.Add("id", typeof(int));
283+
sourceData.Columns.Add("firstName", typeof(string));
284+
sourceData.Columns.Add("lastName", typeof(string));
285+
sourceData.Rows.Add(1, "Alice", "Smith");
286+
sourceData.Rows.Add(2, "Bob", "Jones");
287+
288+
using SqlConnection dstConn = new(dstConstr);
289+
using SqlCommand dstCmd = dstConn.CreateCommand();
290+
dstConn.Open();
291+
292+
try
293+
{
294+
Helpers.TryExecute(dstCmd, initialQuery);
295+
296+
using SqlBulkCopy bulkcopy = new(dstConn, SqlBulkCopyOptions.CacheMetadata, null);
297+
bulkcopy.DestinationTableName = dstTable;
298+
299+
// First write: map firstName -> col2, lastName -> col3.
300+
bulkcopy.ColumnMappings.Add("id", "col1");
301+
bulkcopy.ColumnMappings.Add("firstName", "col2");
302+
bulkcopy.ColumnMappings.Add("lastName", "col3");
303+
bulkcopy.WriteToServer(sourceData);
304+
Helpers.VerifyResults(dstConn, dstTable, 3, 2);
305+
306+
// Verify first mapping: col2 should contain firstName values.
307+
using (SqlCommand verifyCmd = new("select col2 from " + dstTable + " where col1 = 1", dstConn))
308+
{
309+
object result = verifyCmd.ExecuteScalar();
310+
Assert.Equal("Alice", result);
311+
}
312+
313+
// Change mappings: swap col2 and col3 targets.
314+
bulkcopy.ColumnMappings.Clear();
315+
bulkcopy.ColumnMappings.Add("id", "col1");
316+
bulkcopy.ColumnMappings.Add("firstName", "col3");
317+
bulkcopy.ColumnMappings.Add("lastName", "col2");
318+
bulkcopy.WriteToServer(sourceData);
319+
Helpers.VerifyResults(dstConn, dstTable, 3, 4);
320+
321+
// Verify second mapping: col3 should now contain firstName values for the new rows.
322+
using (SqlCommand verifyCmd = new("select col3 from " + dstTable + " where col1 = 1 order by col2", dstConn))
323+
{
324+
using SqlDataReader reader = verifyCmd.ExecuteReader();
325+
326+
// First row (from first write): col3 = "Smith" (lastName).
327+
Assert.True(reader.Read());
328+
Assert.Equal("Smith", reader.GetString(0));
329+
330+
// Second row (from second write): col3 = "Alice" (firstName).
331+
Assert.True(reader.Read());
332+
Assert.Equal("Alice", reader.GetString(0));
333+
}
334+
}
335+
finally
336+
{
337+
Helpers.TryExecute(dstCmd, "drop table " + dstTable);
338+
}
339+
}
340+
}
341+
270342
public class CacheMetadataCombinedWithKeepNulls
271343
{
272344
private static readonly string initialQueryTemplate = "create table {0} (col1 int, col2 nvarchar(50) default 'DefaultVal', col3 nvarchar(50))";
@@ -276,7 +348,7 @@ public static void Test(string dstConstr, string dstTable)
276348
{
277349
string initialQuery = string.Format(initialQueryTemplate, dstTable);
278350

279-
DataTable sourceData = new();
351+
using DataTable sourceData = new();
280352
sourceData.Columns.Add("col1", typeof(int));
281353
sourceData.Columns.Add("col2", typeof(string));
282354
sourceData.Columns.Add("col3", typeof(string));

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ public void CacheMetadataWithDataTableTest()
339339
CacheMetadataWithDataTable.Test(_connStr, AddGuid("SqlBulkCopyTest_CacheMetadataDT"));
340340
}
341341

342+
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
343+
public void CacheMetadataColumnMappingsChangeTest()
344+
{
345+
CacheMetadataColumnMappingsChange.Test(_connStr, AddGuid("SqlBulkCopyTest_CacheMetadataColMap"));
346+
}
347+
342348
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
343349
public void CacheMetadataCombinedWithKeepNullsTest()
344350
{

0 commit comments

Comments
 (0)