没有结果返回给读者,但相同的查询可以通过 SQL Management Studio 进行

发布于 2024-08-08 02:33:12 字数 1209 浏览 5 评论 0原文

我在某些全文搜索查询中遇到了奇怪的行为,尤其是那些包含多个单词的查询。这些在通过 Management Studio 执行时工作正常,但在从代码调用时不返回任何结果。我执行了 SQL 跟踪来查看从我的应用程序发送了哪些命令,并且从 Management Studio 执行时会产生完全相同的命令,但使用 ExecuteReader 方法从我的应用程序调用时却没有结果。

此调用:

exec dbo.FullTextSearch_Articles @ftsQuery=N' FORMSOF (INFLECTIONAL, moravec) '

将返回我的应用程序和 Management Studio 的数据,而此命令:

exec dbo.FullTextSearch_Articles @ftsQuery=N'( FORMSOF (INFLECTIONAL, jan)  AND  FORMSOF (INFLECTIONAL, moravec) )'

仅在从 Management Studio 执行时返回数据。我直接从跟踪日志复制/粘贴这些查询。

在代码方面,我使用的是 Enterprise Library,但总的来说,我的数据库调用非常简单:

using (var dataReader = (SqlDataReader)db.ExecuteReader(cmd))
{
   if (dataReader.HasRows)
   {
       var results = new List<IFullTextSearchItem>();
       while (dataReader.Read())
       {
          results.Add(CreateArticleSearchFromReader(dataReader));
       }
       return results;
    }
    return null;
}

在第二种情况下,dataReader.HasRows 由于某种原因为 false,但同样,当从 Management Studio 执行这些查询时,两者都会返回一些数据。

我认为这可能是由于返回了许多行(第二个查询返回了更大的结果集),但随后成功测试了单字搜索,返回了更多行。

如果知道 DataReader 的行为与简单的 Management Studio 查询执行不同的原因,我们将不胜感激。

谢谢,

安东尼

I'm experiencing weird behavior with some of full text search queries, especially those with multiple words. These are working fine when executed thru Management Studio but returning no results when called from a code. I did a SQL Trace to see what commands are being sent from my app and exactly same command come with results when executed from Management Studio but come with no results when called from my app using ExecuteReader method.

This call:

exec dbo.FullTextSearch_Articles @ftsQuery=N' FORMSOF (INFLECTIONAL, moravec) '

will return data for both my app and Management Studio while this command:

exec dbo.FullTextSearch_Articles @ftsQuery=N'( FORMSOF (INFLECTIONAL, jan)  AND  FORMSOF (INFLECTIONAL, moravec) )'

will only return data when executed from Management Studio. I copy/pasted these queries directly from a trace log.

On a code side, I'm using Enterprise Library, but overall my DB call is really simple:

using (var dataReader = (SqlDataReader)db.ExecuteReader(cmd))
{
   if (dataReader.HasRows)
   {
       var results = new List<IFullTextSearchItem>();
       while (dataReader.Read())
       {
          results.Add(CreateArticleSearchFromReader(dataReader));
       }
       return results;
    }
    return null;
}

In the second case, dataReader.HasRows is false for some reason, but again, when those queries are executed from a Management Studio, both returns some data.

I thought it might be due to a number of rows returned (second query returns much bigger set of results) but then sucessfully tested single word search with even more rows returned.

Any idea why DataReader would behave different from a simple Management Studio query execution would be appreciated.

Thanks,

Antonin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2024-08-15 02:33:12

好吧,这很有趣。我做了更多调试,发现 dataReader.HasRows 属性不准确 - 由于某种原因,即使结果视图集合中有数据,在某些情况下该属性也会设置为 false。

我想知道 reader.HasRows 是否在某种程度上依赖于客户端应用程序和服务器之间传输的数据量 - 因此,在数据很多的情况下,一旦调用 ExecuteReader 方法,此属性不会立即更新。

无论如何,只需删除 reader.HasRows 的检查即可解决此问题。

OK, this is interesting. I did a bit more debugging and found that dataReader.HasRows property is not accurate - for some reason this is set to false in some case even when there are data in Results View collection.

I wonder whether reader.HasRows is somehow dependable on the amount of data being transferred between client app and server - so in the case of lot of data, this property is not updated immediately once ExecuteReader method is called.

Anyway, simply by removing check for reader.HasRows solves this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文