Repository navigation
Allow SqlBulkCopy across source and destination collations - #5157
cheenamalhotra wants to merge 3 commits into
Conversation
Remove the SqlDataReader source/destination LCID check. Reader text is decoded before bulk copy and re-encoded with the destination encoding. Fixes #3479 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The parameterized batch creates #Source in a nested scope that ends with ExecuteNonQuery(), causing the later SELECT to fail before bulk copy runs.
1 open issue
What changed in this PR
This PR allows SqlBulkCopy to copy text from a SqlDataReader across different source and destination collations, addressing #3479 while retaining destination encoding and collation settings.
Changes:
- Removes the locale-ID mismatch check and its unused exception helper.
- Adds unit coverage and synchronous/asynchronous integration tests for cross-collation copying.
- Documents encoding behavior and adds a cross-collation sample.
| File | Description |
|---|---|
| src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlBulkCopyTests.cs | Tests differing locales while retaining destination metadata. |
| src/Microsoft.Data.SqlClient/tests/ManualTests/BulkCopy/ColumnCollation.cs | Tests text, destination bytes, nulls, and streaming across collations. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs | Removes the unused locale-mismatch exception helper. |
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs | Removes the source/destination locale-ID restriction. |
| doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml | Explains cross-collation conversion and potential character replacement. |
| doc/samples/SqlBulkCopy_CrossCollation.cs | Demonstrates copying between differently collated text columns. |
Review effort: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
Cross-collation text and byte fidelity and streamed conversions still need SQL Server validation, as integration tests were not run locally and current-head CI remains pending.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
Varchar and nvarchar destinations no longer fail with the source/destination locale mismatch, so they now follow the same conversion path as json. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5157 +/- ##
==========================================
- Coverage 71.17% 70.48% -0.69%
==========================================
Files 315 315
Lines 46140 70149 +24009
==========================================
+ Hits 32841 49447 +16606
- Misses 13299 20702 +7403
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|

Description
SqlBulkCopythrewInvalidOperationExceptionwhen aSqlDataReadersource text column had a different collation LCID than the destination column (e.g.French_100_BIN->SQL_Latin1_General_CP1_CI_AS). Other source types had no such check.The check is not needed: reader values are decoded to .NET strings, then encoded with the destination column's encoding. This PR removes the check and its unused
SQL.BulkLoadLcidMismatchhelper. The destinationCOLLATEand encoding are unchanged.Docs:
SqlBulkCopyremarks now explain cross-collation behavior (unrepresentable characters inchar/varchardestinations may be replaced), plus a new sampleSqlBulkCopy_CrossCollation.cs.No public API changes.
Issues
Fixes #3479
Testing
AnalyzeTargetAndCreateUpdateBulkCommand_DifferentLocales_UsesDestinationCollation(Char, VarChar, Text, NChar, NVarChar, NText). Failed before the fix with the LCID mismatch, passes after.dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj --framework net10.0 -p:WarningsNotAsErrors=NU1510 --filter "FullyQualifiedName~SqlBulkCopyTests&category!=failing": passed (52/52).dotnet build src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj --framework net462 -p:WarningsNotAsErrors=NU1510: passed.WriteToServer*_DifferentCollations_PreservesTextAndDestinationBytesinColumnCollation.cs(same code page, different code pages, streamed varchar(max) -> nvarchar(max), streaming on/off). Verifies text, stored bytes, and nulls.Guidelines
Please review the contribution guidelines before submitting a pull request: