You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
⚠️ 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.
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.
Activity
SubhankarPriyaranjan commentedon Nov 20, 2024
using (var dr = cmd.ExecuteReader())
{
do
{
if (dr.HasRows)
{
while (dr.Read())
{
Console.WriteLine(dr[0]);
}
}
} while (dr.NextResult());
}
MichelZ commentedon Nov 24, 2024
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?
wilfre commentedon Nov 26, 2024
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.
14 remaining items
priyankatiwari08 commentedon Sep 17, 2026
/triage
github-actions commentedon Sep 17, 2026
🔍 Triage Summary (on-demand re-triage)
Area\Netfx(root cause identified innetfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs; likely also affects the equivalent unified/netcore code path)mainno 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 bugAnalysis
SqlDataReader.HasRowsincorrectly returnsfalsewhen one or moreINFOtokens are interleaved between the column metadata token and the first row token in the TDS response stream. The reporter's original repro (PRINTstatements before aSELECT) traces to anifvswhileloop bug in the netfxSqlDataReader.cstoken-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:HasRowsgiving 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
if/whiletoken-consumption logic in the unifiedsrc/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDataReader.cs(not just the legacynetfx/srcpath flagged in the issue), and (3) verify the fix against the specific INFO-token-before-row-data scenario paulmedynski reproduced.Bump Microsoft.Data.SqlClient from 6.1.1 to 6.1.8 (#104)