jonwagner / Insight.Database

Fast, lightweight .NET micro-ORM
Other
861 stars 145 forks source link

'Internal connection fatal error' when calling `sys.*` queries. #498

Closed nickpreston24 closed 7 months ago

nickpreston24 commented 1 year ago

'Internal connection fatal error' when calling sys.* queries.

When trying to run QueryAsync or any repo stored procedure, I get an exception that says:

System.InvalidOperationException: Internal connection fatal error.
   at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__126_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Insight.Database.DBConnectionExtensions.GetReaderAsync(IDbCommand command, CommandBehavior commandBehavior, CancellationToken cancellationToken)
   at Insight.Database.DBConnectionExtensions.ExecuteAsyncAndAutoClose[T](IDbConnection connection, Object parameters, Func`2 getCommand, Boolean callGetReader, Func`3 translate, Comma
ndBehavior commandBehavior, CancellationToken cancellationToken, Object outputParameters)
   at vespin_insights.Pages.Sandbox.IndexModel.OnGetSprocs()
   at vespin_insights.Pages.Sandbox.IndexModel.OnGetSprocs()

Steps to reproduce

 string sproc_search_query = """
    --- This originally searched stored procedures, but I'm shortening this to show that it's `sys.objects` with an issue
          SELECT
         *
      FROM
          sys.objects
  """;

  var procs_found = await connection.QuerySqlAsync<SprocMetadata>(sproc_search_query);
  procs_found.Dump("found");

Expected behavior

The standard sys.object columns and row to be returned.

This also happens with sys.parameters, sys.comments, sql_expression_dependencies and perhaps a few others. Even stuffing this code in a stored procedure doesn't solve the issue.

On the other hand, sys.sql_modules works just fine! Can't imagine why only a couple of sys.* work while most do not.

nickpreston24 commented 1 year ago

Ok, so.... running

    SELECT name as procedure_name FROM sys.objects

Instead of

SELECT * FROM sys.objects

makes it work.

Why? Idk. But I'm using it for now.

Thanks for reading.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

jonwagner commented 7 months ago

I added this test case for .net8/Microsoft.Data.SqlClient. Seems to be working:

        [Test]
        public void TestSysProcs()
        {
            // Issue #498
            var results = Connection().QuerySql<SprocMetadata>("SELECT * FROM sys.objects");
            ClassicAssert.Greater(results.Count, 0);
        }

Please reopen if v8 doesn't fix it.