dotnet / dotnet/SqlClient

HasRows fails to detect rows

Open
#3,018 5 comments 0 reactions 1 assignee Claimed by @paulmedynski View on GitHub
Repro Available :heavy_check_mark:
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

### 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](https://github.com/dotnet/SqlClient/blob/main/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs#L4888) should probably be a `while` instead of an `if` statement.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.