Skip to content

Allow SqlBulkCopy across source and destination collations - #5157

Open
cheenamalhotra wants to merge 3 commits into
mainfrom
dev/cheena/standing-issue-fixes
Open

cheenamalhotra wants to merge 3 commits into
mainfrom
dev/cheena/standing-issue-fixes

Conversation

@cheenamalhotra

Copy link
Copy Markdown
Member

Description

SqlBulkCopy threw InvalidOperationException when a SqlDataReader source 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.BulkLoadLcidMismatch helper. The destination COLLATE and encoding are unchanged.

Docs: SqlBulkCopy remarks now explain cross-collation behavior (unrepresentable characters in char/varchar destinations may be replaced), plus a new sample SqlBulkCopy_CrossCollation.cs.

No public API changes.

Issues

Fixes #3479

Testing

  • Unit: 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.
  • Manual: sync and async WriteToServer*_DifferentCollations_PreservesTextAndDestinationBytes in ColumnCollation.cs (same code page, different code pages, streamed varchar(max) -> nvarchar(max), streaming on/off). Verifies text, stored bytes, and nulls.
    • Compiled and discovered locally; not run — no reachable SQL Server. Relying on CI.

Guidelines

Please review the contribution guidelines before submitting a pull request:

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>
@cheenamalhotra
cheenamalhotra requested a review from a team as a code owner November 30, 2026 17:53
Copilot AI balanced review requested due to automatic review settings November 30, 2026 17:53
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Nov 30, 2026
@cheenamalhotra cheenamalhotra added this to the 8.1.0-preview1 milestone Nov 30, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 18:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 18:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Cross-collation byte preservation and streamed conversions still need SQL Server runtime validation, as manual tests were not run and current-head SQL CI remains pending.

No open issues 🎉

Review effort: Balanced

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 70.48%. Comparing base (b3eefc2) to head (0971615).
⚠️ Report is 2 commits behind head on main.

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     
Flag Coverage Δ
CI-SqlClient ?
sqlclient-pr 70.48% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Why sqlbulkcopy failed when collation are differents only with sqlserver sources

4 participants