Skip to content

SqlBulkCopy.WriteToServerAsync(reader) fails to write after upgrading Microsoft.Data.SqlClient to 7.0.0 #4149

@anilkmrn7

Description

@anilkmrn7

Describe the bug

After upgrading the Microsoft.Data.SqlClient to 7.0.0, SqlBulkCopy.WriteToServerAsync(reader) fails to write, however the same code works fine without any changes in 6.1.4 version.

Cannot access destination table 'dbo.TestTable'.. A variable that has been assigned in a SELECT statement cannot be included in a expression or assignment when used in conjunction with a from clause

To reproduce

Below code tries to read from the table and writes to the similar table with the same schema.

CREATE TABLE [dbo].[TestTable]
(
Column1 varchar(255) NULL,
Column2 varchar(255) NOT NULL,
PRIMARY KEY NONCLUSTERED (TableName) NOT ENFORCED
) WITH (CLUSTERED COLUMNSTORE INDEX)
GO

var table = "dbo.TestTable";
var query = "select top 1 [Column1], [Column2] from [dbo].[TestTable]";

using (var command = new SqlCommand())
{
 var sqlConnectionString = _connectionStringProvider.GetConnectionString();
 var sourceSqlConnection = new SqlConnection(sqlConnectionString);
 await sourceSqlConnection.OpenAsync();

 command.Connection = sourceSqlConnection;
 command.CommandTimeout = 3600;
 command.CommandText = query;

 using (var reader = command.ExecuteReader())
 {
	 using var targetSqlConnection = new SqlConnection(sqlConnectionString);
	 targetSqlConnection.Open();

	 using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(targetSqlConnection))
	 {
		 sqlBulkCopy.DestinationTableName = table;

		 await sqlBulkCopy.WriteToServerAsync(reader);
	 }
 }
}

Expected behavior

sqlBulkCopy.WriteToServerAsync(reader) should write the rows read from the source table or the query.

Further technical details

Microsoft.Data.SqlClient version: 7.0.0
.NET target: .Net 10
SQL Server version: Microsoft Azure SQL Data Warehouse - 10.0.31510.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Hotfix Candidate 🚑Issues/PRs that are candidate for backporting to earlier supported versions.Regression 💥Issues that are regressions introduced from earlier PRs.

    Type

    No fields configured for Bug.

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions