diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs index 337752a068..6af6ae7734 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6140,6 +6140,12 @@ internal bool TryReadSqlValue(SqlBuffer value, SqlMetaDataPriv md, int length, T // call to decrypt column keys has failed. The data wont be decrypted. // Not setting the value to false, forces the driver to look for column value. // Packet received from Key Vault will throws invalid token header. + if (stateObj.HasPendingData) + { + // Drain the pending data now if setting the HasPendingData to false. + // SqlDataReader.TryCloseInternal can not drain if HasPendingData = false. + DrainData(stateObj); + } stateObj.HasPendingData = false; } throw SQL.ColumnDecryptionFailed(columnName, null, e); diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index afa5220563..0c74f8f61b 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -6935,6 +6935,12 @@ internal bool TryReadSqlValue(SqlBuffer value, // call to decrypt column keys has failed. The data wont be decrypted. // Not setting the value to false, forces the driver to look for column value. // Packet received from Key Vault will throws invalid token header. + if (stateObj.HasPendingData) + { + // Drain the pending data now if setting the HasPendingData to false. + // SqlDataReader.TryCloseInternal can not drain if HasPendingData = false. + DrainData(stateObj); + } stateObj.HasPendingData = false; } throw SQL.ColumnDecryptionFailed(columnName, null, e);