Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasRows fails to detect rows #3018

Open
wilfre opened this issue Nov 18, 2024 · 3 comments
Open

HasRows fails to detect rows #3018

wilfre opened this issue Nov 18, 2024 · 3 comments
Labels
✔️ Triage Done Issues that are triaged by dev team and are in investigation. 🙌 Up-for-Grabs Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label

Comments

@wilfre
Copy link

wilfre commented Nov 18, 2024

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.

@cheenamalhotra cheenamalhotra added 🆕 Triage Needed For new issues, not triaged yet. ✔️ Triage Done Issues that are triaged by dev team and are in investigation. 🙌 Up-for-Grabs Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label and removed 🆕 Triage Needed For new issues, not triaged yet. labels Nov 18, 2024
@SubhankarPriyaranjan
Copy link

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

@MichelZ
Copy link
Contributor

MichelZ commented 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?


[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);
    }
}

@wilfre
Copy link
Author

wilfre commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✔️ Triage Done Issues that are triaged by dev team and are in investigation. 🙌 Up-for-Grabs Issues that are ready to be picked up for anyone interested. Please self-assign and remove the label
Projects
None yet
Development

No branches or pull requests

4 participants