Skip to content

HasRows fails to detect rows #3018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

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

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions