-
Notifications
You must be signed in to change notification settings - Fork 316
Fix | Allow SqlBulkCopy to operate on hidden columns #3590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
benrr101
merged 9 commits into
dotnet:main
from
edwardneal:issues/sqlbulkcopy-hidden-columns
Oct 3, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
772e2b2
Allow SqlBulkCopy to operate on hidden columns
edwardneal 2a7565b
Investigate test failures
edwardneal f6df23d
Merge branch 'main' into issues/sqlbulkcopy-hidden-columns
edwardneal f7f3484
Handle SQL Graph tables
edwardneal 048c5d7
First round of code review
edwardneal bc342cc
Merge branch 'main' into issues/sqlbulkcopy-hidden-columns
edwardneal 5fb11e9
Correct post-merge build error
edwardneal 9abef0e
Simplify exception message in HiddenTargetColumn testing
edwardneal 0444e6e
Syntax correction!
edwardneal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/HiddenTargetColumn.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // 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; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SqlBulkCopyTests | ||
| { | ||
| public class HiddenTargetColumn | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))] | ||
| public void WriteToServer_CopyToHiddenTargetColumn_ThrowsSqlException() | ||
| { | ||
| string connectionString = DataTestUtility.TCPConnectionString; | ||
| string destinationTable = DataTestUtility.GetShortName("HiddenTargetColumn"); | ||
| string destinationHistoryTable = DataTestUtility.GetShortName("HiddenTargetColumn_History"); | ||
|
|
||
| using (SqlConnection dstConn = new SqlConnection(connectionString)) | ||
| using (SqlCommand dstCmd = dstConn.CreateCommand()) | ||
| { | ||
| dstConn.Open(); | ||
|
|
||
| try | ||
| { | ||
| DataTestUtility.CreateTable(dstConn, destinationTable, $""" | ||
| ( | ||
| Column1 int primary key not null, | ||
| Column2 nvarchar(10) not null, | ||
| [Employee's First Name] varchar(max) null, | ||
| ValidFrom datetime2 generated always as row start hidden not null, | ||
| ValidTo datetime2 generated always as row end hidden not null, | ||
| period for system_time (ValidFrom, ValidTo) | ||
| ) | ||
| with (system_versioning = on(history_table = dbo.{destinationHistoryTable})); | ||
| """); | ||
|
|
||
| using (SqlConnection srcConn = new SqlConnection(connectionString)) | ||
| using (SqlCommand srcCmd = new SqlCommand("select top 5 EmployeeID, FirstName, LastName, HireDate, sysdatetime() as CurrentDate from employees", srcConn)) | ||
| { | ||
| srcConn.Open(); | ||
|
|
||
| using (DbDataReader reader = srcCmd.ExecuteReader()) | ||
| using (SqlBulkCopy bulkcopy = new SqlBulkCopy(dstConn)) | ||
| { | ||
| bulkcopy.DestinationTableName = destinationTable; | ||
| SqlBulkCopyColumnMappingCollection ColumnMappings = bulkcopy.ColumnMappings; | ||
|
|
||
| ColumnMappings.Add("EmployeeID", "Column1"); | ||
| ColumnMappings.Add("LastName", "Column2"); | ||
| ColumnMappings.Add("FirstName", "Employee's First Name"); | ||
| ColumnMappings.Add("HireDate", "ValidFrom"); | ||
| ColumnMappings.Add("CurrentDate", "ValidTo"); | ||
|
|
||
| SqlException sqlEx = Assert.Throws<SqlException>(() => bulkcopy.WriteToServer(reader)); | ||
|
|
||
| Assert.Equal(13536, sqlEx.Number); | ||
| Assert.StartsWith("Cannot insert an explicit value into a GENERATED ALWAYS column in table", sqlEx.Message); | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| DataTestUtility.RunNonQuery(connectionString, $""" | ||
| alter table {destinationTable} set (system_versioning = off); | ||
| alter table {destinationTable} drop period for system_time; | ||
| """); | ||
| DataTestUtility.DropTable(dstConn, destinationTable); | ||
| DataTestUtility.DropTable(dstConn, destinationHistoryTable); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/SqlGraphTables.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // 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; | ||
| using System.Data.Common; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests.SqlBulkCopyTests | ||
| { | ||
| public class SqlGraphTables | ||
benrr101 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| [ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureSynapse))] | ||
| public void WriteToServer_CopyToSqlGraphNodeTable_Succeeds() | ||
| { | ||
| string connectionString = DataTestUtility.TCPConnectionString; | ||
| string destinationTable = DataTestUtility.GetShortName("SqlGraphNodeTable"); | ||
|
|
||
| using SqlConnection dstConn = new SqlConnection(connectionString); | ||
| using DataTable nodes = new DataTable() | ||
| { | ||
| Columns = { new DataColumn("Name", typeof(string)) } | ||
| }; | ||
|
|
||
| dstConn.Open(); | ||
|
|
||
| for (int i = 0; i < 5; i++) | ||
| { | ||
| nodes.Rows.Add($"Name {i}"); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| DataTestUtility.CreateTable(dstConn, destinationTable, "(Id INT PRIMARY KEY IDENTITY(1,1), [Name] VARCHAR(100)) AS NODE"); | ||
|
|
||
| using SqlBulkCopy nodeCopy = new SqlBulkCopy(dstConn); | ||
|
|
||
| nodeCopy.DestinationTableName = destinationTable; | ||
| nodeCopy.ColumnMappings.Add("Name", "Name"); | ||
| nodeCopy.WriteToServer(nodes); | ||
| } | ||
| finally | ||
| { | ||
| DataTestUtility.DropTable(dstConn, destinationTable); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.