title: "Internal connection fatal error" from TdsParser when calling native compiled stored procedure in SQL Server 2019
body: <p>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 <code>Microsoft.Data.SqlClient</code> (1.1.0 and lower).</p>
  
  <p>It <em>is</em> working when either using SQL Server 2017 or <code>System.Data.SqlClient</code>.
  <a href="https://stackoverflow.com/questions/59559872/tdsparser-encountered-unexpected-unknown-token-0xa3">I am also having the same problem in Java with the JDBC driver.</a></p>
  
  <p>Apparently the <code>TdsParser</code> is receiving data it doesn't expect from the SQL Server connection and is throwing an exception during the parsing.</p>
  
  <blockquote>
    <p>System.InvalidOperationException: Internal connection fatal error.</p>
    
    <p>at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)<br>
    at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()<br>
    at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()<br>
    at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)<br>
    at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task&amp; task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)<br>
    at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean&amp; usedCache, Boolean asyncWrite, Boolean inRetry, String method)<br>
    at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)<br>
    at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)<br>
    at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader()</p>
  </blockquote>
  
  <p>Example code which should reproduce the problem:</p>
  
  <pre><code>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);
              }
  
          }
      }
  }
  </code></pre>