针对 SQL Server 2008 运行时,SqlDataReader.HasRows 中是否存在错误?

Is there a bug in SqlDataReader.HasRows when running against SQL Server 2008?(针对 SQL Server 2008 运行时,SqlDataReader.HasRows 中是否存在错误?)
本文介绍了针对 SQL Server 2008 运行时,SqlDataReader.HasRows 中是否存在错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这两个查询:

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

当我在 Management Studio 中运行查询 #1 时,它会返回 20 行.
查询 #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:

信息性:全文搜索条件包含干扰词.

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...
            }
        }
    }
}

当我针对查询 #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.

如果 SQL Server 生成消息,似乎 HasRows 属性未正确填充.结果被返回并可以通过使用 Read() 方法进行迭代,但 HasRows 属性将为 false.

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 中的已知错误,还是我遗漏了一些明显的错误?
我正在使用 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.

感谢我的问题与 这个,几乎可以肯定是同一个问题的表现,但这个问题已经陷入了三个月的泥潭,没有明确的答案.

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.

推荐答案

我是引用问题(丢失登录)的原始发帖人,从未设法弄清楚.最后我把它归结为邪恶的巫毒教,牺牲了整洁并选择了类似的东西

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 页面中工作,而不是在另一个页面中,尽管代码块几乎与使用的存储过程相同.

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.

这篇关于针对 SQL Server 2008 运行时,SqlDataReader.HasRows 中是否存在错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)