SqlClient: “Internal connection fatal error” from TdsParser when calling native compiled stored procedure in SQL Server 2019

Describe the bug

I am having huge problems with the current SQL Server 2019 (15.0.2070.41) and native compiled stored procedures when calling them from the Microsoft.Data.SqlClient (1.1.0 and lower).

It is working when either using SQL Server 2017 or System.Data.SqlClient, or just not using a native compiled procedure.

Apparently the TdsParser is receiving data it doesn’t expect from the SQL Server connection and is throwing an exception during the parsing.

Exception message: System.InvalidOperationException: Internal connection fatal error.
Stack trace: at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)
at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader()

To reproduce

using System;
using System.Data;
using Microsoft.Data.SqlClient;

namespace SqlDbTest
{
    class Program
    {
        static void Main()
        {
            const string connString = "Server=localhost;Database=TestDb;Trusted_Connection=True";    
            const string prod = @"CREATE OR ALTER PROCEDURE dbo.NativeProdForTesting WITH NATIVE_COMPILATION, SCHEMABINDING, EXECUTE AS OWNER
                AS BEGIN ATOMIC WITH ( TRANSACTION ISOLATION LEVEL = SNAPSHOT,  LANGUAGE = N'us_english')
                    SELECT NULL AS testcol
                RETURN 0
                END";

            using (var con = new SqlConnection(connString))
            {
                using var createSp = con.CreateCommand();
                createSp.CommandText = prod;
                con.Open();
                createSp.ExecuteNonQuery();
            }

            try
            {
                using var con = new SqlConnection(connString);
                using var cmd = con.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "[dbo].[NativeProdForTesting]";
                cmd.Connection.Open();
                using var reader = cmd.ExecuteReader();
                Console.WriteLine("NO ERROR!");
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

Expected behavior

No exception and the reader should provide the result of the query.

Further technical details

Microsoft.Data.SqlClient version: 1.1.0 and lower .NET target: Core 3.1, Framework 4.7.2 SQL Server version: SQL Server 2019 (15.0.2070.41) Operating system: Windows 10, Windows Server 2016 Standard

Additional context I have the same problem with JDBC and it’s TDSParser (7.4.1.jre8), see here. I also posted the problem on Stackoverflow

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 26 (11 by maintainers)

Most upvoted comments

@jumpingjackson That is due to a bug in SQLClient when using Data Classification with Bulk Copy. I believe that the bug was fixed in SQLCLient V2.

I’m also getting this error, I have an Azure SQL, and running a stored procedure through .net core 3.1 / EF Core (3.1.2) Works locally but not in Azure.

The code is simple, something like this: var result = context.Database .ExecuteSqlInterpolated($"EXEC [dbo].[ConvertUser] {convertUserId}"); ;