@@ -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 ) ) ;
0 commit comments