针对SQL Server 2008上运行时是否有SqlDataReader.HasRows一个错误?错误、Server、SQL、HasRows

2023-09-03 02:21:01 作者:琉璃纸鸢

看看这两个查询:

-- #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

查询#1返回20行,当我在Management Studio中运行它。 查询#2返回相同的20行,但我也看到消息选项卡下面的:

Query #1 returns 20 rows when I run it in Management Studio. Query #2 returns the same 20 rows, but I also see the following in the Messages tab:

信息化:全文检索条件包含干扰词(S)

Informational: The full-text search condition contained noise word(s).

到目前为止,太无聊了 - 正是我所期待的发生

So far, so boring - exactly what I'd expect to happen.

现在,来看看这个C#代码段:

Now, take a look at this C# snippet:

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 1的一切行为与预期 - 在做一些有用的东西一节被击中了每个20行

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.

当我运行对查询#2,没有任何反应 - 在做一些有用的东西一节永远达不到

When I run it against query #2, nothing happens - the "do something useful" section is never reached.

现在,这里的事情变得更有趣... 的

如果我删除 HasRows 检查则一切正常 - 在做一些有用的东西一节被击中了每个20行,不管是哪个查询使用

If I remove the HasRows 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.

好像不是填充 HasRows 属性是正确的,如果SQL Server生成的消息。结果返回,并且可以通过使用阅读()的方法,但 HasRows 属性将是错误的迭代。

It seems that the HasRows property isn't populated correctly if SQL Server generates a message. The results are returned and can be iterated through using the Read() method but the HasRows property will be false.

这是在.NET和/或SQL Server一个已知的bug,或者有我错过了一些东西明显? 我使用VS2008SP1,.NET3.5SP1和SQL2008。

Is this a known bug in .NET and/or SQL Server, or have I missed something obvious? I'm using VS2008SP1, .NET3.5SP1 and SQL2008.

编辑:我AP preciate我的问题是非常相似的this 之一,这几乎肯定是同一个问题的一种表现,但这个问题已经陷入了三个月,没有明确的答案。

I appreciate that my question is very similar to this one, 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.

推荐答案

我的refernced问题(丢失登录)原始的海报,从来没有设法弄明白。最后,我把它归结为糟糕的巫术,牺牲整洁和去与像

I'm the original poster of the refernced question (lost login) and never managed to figure it out. In the end I put it down to bad voodoo, sacrificed neatness and went with something like

bool readerHasRows=false;
while(reader.reader())
{
   readerHasRows=true;
   doStuffOverAndOver();
}
if (!readerHasRows)
{
   probablyBetterShowAnErrorMessageThen();
}

什么是真正奇怪的是,它的工作在一个aspx页面,而不是在一个又一个,尽管code块是几乎相同的酒吧在存储过程中使用。

What was really weird was that it worked in one aspx page and not in a another despite the code blocks being almost identical bar the stored procedure used.

不用说我回避.HasRows从现在开始;)

Needless to say I'm avoiding .HasRows from now on ;)

修改 - Management Studio中也表示在我的项目在这个问题过程中的消息标签的邮件。使得似乎是问题的原因。但是,为什么它鸡奸了.HasRows ??

EDIT - Management Studio shows messages in the messages tab on the problem procedure in my project too. So that seems to be the cause of the problem. But why would it bugger up .HasRows??

EDIT2 - 确认,改变了查询,以避免该警告消息,.hasrows现在是真正的

EDIT2 - Confirmed, altered the query to avoid the warning messages and .hasrows is now true.