当检查的查询返回结果时,ExecuteReader 不返回结果
考虑以下代码:
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
该查询针对单个视图进行操作。
当我对数据库运行第二个查询时,它返回 3 行。
SqlDataReader 指示 0 行。
Consider the following code:
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]));
}
}
I used Activity Monitor within Sql Server Mgmt Studio on the database to inspect the exact query that was being sent. I then copied that query text to a query editor window within SSMS, and the query returned the expected results. However, SqlDataReader results
is always empty, indicating "The enumeration returned no results."
My suspicion is that somehow the results are not being returned correctly, which makes me think there's something wrong with the code above, and not the query itself being passed.
Is there anything that would cause this in the code above? Or something I've overlooked?
EDIT:
Here is the query as indicated by the SQLCommand object:
SELECT DISTINCT StandardId,Number
FROM vStandardsAndRequirements
WHERE StandardId IN ('@param1','@param2','@param3')
ORDER BY StandardId
Here is the query as it appears in Activity Monitor:
SELECT DISTINCT StandardId,Number
FROM vStandardsAndRequirements
WHERE StandardId IN ('ABC-001-0','ABC-001-0.1','ABC-001-0')
ORDER BY StandardId
The query is working against a single view.
When I ran the second query against the database, it returned 3 rows.
The SqlDataReader indicates 0 rows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用 Sqldata 适配器而不是 sqldatreader。
问候。
try to use Sqldata adapter instead of sqldatreader.
Regards.
你确定是它
而不是这个吗?
参数不应加引号,也不应位于 SQLCommand 对象中。
Are you sure it is
instead of this?
Parameters should not be quoted, not in the SQLCommand object.
我观察到了非常好的行为,
我在代码中查找错误:
并发现“dr.Read”工作正常,但是......
当我将鼠标悬停在“dr”上查找数据时,返回值消失了!
Very nice behavior I've observed
I looked for errors in code:
and found that 'dr.Read' works fine, but...
when I mouseover on 'dr', to lookup for data, return values disappeared !
检查您的连接字符串并确保您没有作为用户实例进行连接。
http://msdn.microsoft.com/en-us/library/ms254504.aspx
Check your connection string and make sure you are not connecting as a user instance.
http://msdn.microsoft.com/en-us/library/ms254504.aspx