Skip to content

HasRows fails to detect rows #3018

Closed
3 of 3 issues completed
Closed

Description

@wilfre

Describe the bug

For queries that do have records, SqlDataReader.HasRows method returns false when the DB returns more than 1 InfoMessageToken, before sending a RowToken, HasRows assumes no records are left, and returns false even though it should have been true.

Using a db that returns two infomessages in the beginning is enough to reproduce the behavior.

string connectionString = "....";
var conn = new SqlConnection(connectionString);
conn.Open();

conn.InfoMessage += (sender, e) => Console.WriteLine(e.Message);

var cmd = conn.CreateCommand();
cmd.CommandText = "PRINT('1');SELECT * FROM t1";
using (var dr = cmd.ExecuteReader())
{
    Console.WriteLine($"HasRows: {dr.HasRows}"); // will return false if 2 infoMessages are sent by the DB.
    while (dr.Read())
    {
        Console.WriteLine(dr[0]);
    }

}

Expected behavior

HasRows shoud return true.

Additional context
When trying to identify the issue we encounter that this should probably be a while instead of an if statement.

Activity

  1. SubhankarPriyaranjan commented on Nov 20, 2024

    @SubhankarPriyaranjan

    using (var dr = cmd.ExecuteReader())
    {
    do
    {
    if (dr.HasRows)
    {
    while (dr.Read())
    {
    Console.WriteLine(dr[0]);
    }
    }
    } while (dr.NextResult());
    }

  2. MichelZ commented on Nov 24, 2024

    @MichelZ
    Contributor

    Can you elaborate on this?
    I have not been able to reproduce this.

    Can you modify the following unit test so it fails for what you want to show?

    
    [Fact]
    public void Test1()
    {
        int rows = 0;
        int infoMessages = 0;
        using (var conn = new SqlConnection("Data Source=tcp:localhost;Initial Catalog=Northwind;User ID=sa;Password=Just1Testing;Encrypt=False;Connect Timeout=300"))
        {
            conn.Open();
    
            conn.InfoMessage += (sender, e) => infoMessages++;
    
            var cmd = conn.CreateCommand();
            cmd.CommandText = "PRINT('1'); PRINT('2'); SELECT * FROM Customers";
            using (var dr = cmd.ExecuteReader())
            {
                Assert.True(dr.HasRows);
                while (dr.Read())
                {
                    Assert.True(dr.HasRows);
                    rows++;
                }
    
            }
    
            Assert.True(rows > 0);
            Assert.Equal(2, infoMessages);
        }
    }
    
  3. wilfre commented on Nov 26, 2024

    @wilfre
    Author

    Hi, sorry for the confusion; but the code I pasted there actually does not reproduce the bug, it would be reproduced if the database could return two INFO tokens, similar to: Statement IDs.

  4. moved this from To triage to Backlog in SqlClient Boardon Oct 28, 2025
  5. added this to the 7.0.0-preview3 milestone on Nov 4, 2025
  6. moved this from Backlog to In progress in SqlClient Boardon Nov 4, 2025
  7. 14 remaining items

  8. moved this from In progress to Backlog in SqlClient Boardon Feb 17, 2026
  9. removed this from the 7.1.0-preview1 milestone on Feb 17, 2026
  10. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  11. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment ⚠️ Missing: SqlClient version, .NET target framework, SQL Server version, OS (original report did not specify; repro steps and expected vs actual behavior are provided)
    Area Area\Netfx (root cause identified in netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs; likely also affects the equivalent unified/netcore code path)
    Duplicates None found (only this issue matched)
    Regression Inconclusive — a maintainer (paulmedynski) noted in comments that current main no longer reproduces the original repro with INFO tokens appearing before column metadata/row tokens, but a related variant (INFO tokens between column metadata and row data) still reproduces the bug

    Analysis

    SqlDataReader.HasRows incorrectly returns false when one or more INFO tokens are interleaved between the column metadata token and the first row token in the TDS response stream. The reporter's original repro (PRINT statements before a SELECT) traces to an if vs while loop bug in the netfx SqlDataReader.cs token-consumption logic. A maintainer has since reproduced a narrower variant (INFO tokens appearing specifically between column metadata and row data) via draft PR #3744, which adds targeted unit tests crafting TDS responses with 0–10 INFO tokens. This is a P2 correctness bug: HasRows giving a false negative can cause callers to skip valid result processing, which is a meaningful functional defect though not a crash/data-loss issue.

    Next Steps

    • Since environment details (SqlClient version, TFM, SQL Server version, OS) were never fully captured on this long-running issue, and given active maintainer investigation with a concrete reproduction and draft fix already in progress (PR [DRAFT] Add tests for HasRows + Mutliple INFO tokens #3744), recommend prioritizing review/completion of PR [DRAFT] Add tests for HasRows + Mutliple INFO tokens #3744 over requesting further environment info from the original reporter.
    • Assign to Copilot coding agent to: (1) finish the crafted-TDS-response unit tests in PR [DRAFT] Add tests for HasRows + Mutliple INFO tokens #3744 covering INFO tokens positioned between column metadata and row tokens, (2) locate and fix the analogous if/while token-consumption logic in the unified src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs (not just the legacy netfx/src path flagged in the issue), and (3) verify the fix against the specific INFO-token-before-row-data scenario paulmedynski reproduced.
    • Recommend reviewing PR [DRAFT] Add tests for HasRows + Mutliple INFO tokens #3744 directly as the primary artifact for resolving this issue rather than duplicating investigation.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #3018 · copilot · auto · 50.2 AIC · ⌖ 13.2 AIC · ⊞ 11.9K · ◷

  12. moved this from Backlog to Investigating in SqlClient Boardon Oct 1, 2026
  13. removed their assignment
    on Oct 6, 2026
  14. added this to the 8.0.0-preview1 milestone on Oct 19, 2026
  15. moved this from In progress to Done in SqlClient Boardon Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Repro Available ✔️Issues that are reproducible with repro provided.

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions