sql - ExecuteReader 不返回任何结果,当检查的查询不返回时

标签 sql ado.net

考虑以下代码:

        StringBuilder textResults = new StringBuilder();
        using(SqlConnection connection = new SqlConnection(GetEntityConnectionString()))
        {
            connection.Open();
            m.Connection = connection;
            SqlDataReader results = m.ExecuteReader();
            while (results.Read())
            {
                textResults.Append(String.Format("{0}", results[0]));
            }
        }

我在数据库上的 Sql Server Mgmt Studio 中使用事件监视器来检查正在发送的确切查询。然后,我将该查询文本复制到 SSMS 中的查询编辑器窗口,该查询返回了预期的结果。然而,SqlDataReader results始终为空,表示“枚举没有返回结果”。

我的怀疑是以某种方式没有正确返回结果,这让我认为上面的代码有问题,而不是查询本身被传递。

在上面的代码中有什么会导致这种情况吗?还是我忽略了什么?

编辑:

这是 SQLCommand 对象指示的查询:
SELECT DISTINCT StandardId,Number 
FROM vStandardsAndRequirements 
WHERE StandardId IN ('@param1','@param2','@param3') 
ORDER BY StandardId

这是出现在事件监视器中的查询:
SELECT DISTINCT StandardId,Number 
FROM vStandardsAndRequirements 
WHERE StandardId IN ('ABC-001-0','ABC-001-0.1','ABC-001-0') 
ORDER BY StandardId

该查询针对单个 View 工作。

当我对数据库运行第二个查询时,它返回了 3 行。

SqlDataReader 指示 0 行。

最佳答案

你确定是

WHERE StandardId IN ('@param1','@param2','@param3') 

而不是这个?
WHERE StandardId IN (@param1,@param2,@param3) 

参数不应被引用,不在 SQLCommand 对象中。

关于sql - ExecuteReader 不返回任何结果,当检查的查询不返回时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4940350/

相关文章:

sql - 是否可以在多个更新中使用 WITH 子句而不将其复制到每个更新中?

c# - 从 C# 的 .txt 文件中读取 SQL 查询

c# - 异步 odbc 似乎是同步的

php - Laravel SQL 无法创建表

mysql - SQL 查询 INNER JOIN 4 个表

mysql - 什么会导致 InnoDB 表的单个 UPDATE 性能非常低?

.net - 与DataReader一起使用时,输出参数不可读

sql - t-sql 对连续分区或运行进行编号(字段中具有相同值的序列)

sql-server - "System.AccessViolationException: Attempted to read or write protected memory"填充数据表时

c# - 通过 Ado.Net 3.5 和 C# 多次插入 SQL Server 2008