Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a bug in SqlDataReader.HasRows when running against SQL Server 2008?
    text
    copied!<p>Take a look at these two queries:</p> <pre><code>-- #1 SELECT * FROM my_table WHERE CONTAINS(my_column, 'monkey') -- #2 SELECT * FROM my_table WHERE CONTAINS(my_column, 'a OR monkey') -- "a" is a noise word </code></pre> <p>Query #1 returns 20 rows when I run it in Management Studio.<br> Query #2 returns the same 20 rows, but I also see the following in the Messages tab:</p> <blockquote> <p>Informational: The full-text search condition contained noise word(s).</p> </blockquote> <p>So far, so boring - exactly what I'd expect to happen.</p> <p>Now, take a look at this C# snippet:</p> <pre><code>using (SqlConnection conn = new SqlConnection(...)) { SqlCommand cmd = conn.CreateCommand(); // setup the command object... conn.Open(); using (SqlDataReader dr = cmd.ExecuteReader()) { if (dr.HasRows) { // get column ordinals etc... while (dr.Read()) { // do something useful... } } } } </code></pre> <p>When I run this code against query #1 everything behaves as expected - the "do something useful" section gets hit for each of the 20 rows.</p> <p>When I run it against query #2, nothing happens - the "do something useful" section is never reached.</p> <p><em>Now here's where things get a bit more interesting...</em></p> <p>If I remove the <code>HasRows</code> check then everything works as expected - the "do something useful" section gets hit for each of the 20 rows, regardless of which query is used.</p> <p>It seems that the <code>HasRows</code> property isn't populated correctly if SQL Server generates a message. The results are returned and can be iterated through using the <code>Read()</code> method but the <code>HasRows</code> property will be false.</p> <p>Is this a known bug in .NET and/or SQL Server, or have I missed something obvious?<br> I'm using VS2008SP1, .NET3.5SP1 and SQL2008.</p> <p><strong>EDIT:</strong> I appreciate that my question is very similar to <a href="https://stackoverflow.com/questions/197220/sqldatareader-hasrows-returns-false-since-sql-2008-upgrade">this one</a>, and it's almost certainly a manifestation of the same issue, but that question has been bogged down for three months with no definitive answer.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload