为什么 DataReader 给出"枚举未产生任何结果"?



我的 SQL 数据库存储的过程返回结果,但数据读取器显示"枚举未产生任何结果"?

这是我的代码:

var sqlFastProd = String.Format("Getnpidataforencryption");
using (SqlConnection conn = new SqlConnection(connectionString.ToString()))
{
SqlCommand myCommand = new SqlCommand(sqlFastProd, conn);
myCommand.CommandType = System.Data.CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@StartingId", startId));
myCommand.Parameters.Add(new SqlParameter("@countOfRecords", CountofRecords));
conn.Open();
SqlDataReader reader = myCommand.ExecuteReader();
List<DataElement> dataElements = new List<DataElement>();
var recordsFetchTime = DateTime.Now;
TimeSpan t = serviceStartTime - recordsFetchTime;
if (reader.HasRows)
{
while (reader.Read())
{
…
}
}
}

存储过程

创建过程 格腾皮达

taforencryption @StartingId     BIGINT,--1       
@countOfRecords BIGINT -- 101       
AS       
BEGIN       
SELECT textid,       
originaltextdata as textdata,       
keyname       
FROM   npidataencryption       
WHERE IsEncrypted is null and id BETWEEN @StartingId AND @countOfRecords      
END 
need help, thanks in adv

安:(

这是因为大多数情况下,您的存储过程没有返回具有这些输入值的数据。您可能希望使用HasRows属性检查读取器是否有任何行

if(!reader.HasRows)
{
// return empty response model
}

看起来您已经在检查如下所示的行(来自您发布的代码(。那么在这种情况下,我相信您在调试时肯定会从调试器收到该错误

if (reader.HasRows)
{
while (reader.Read())

如果不是这种情况,那么您应该发布您的过程代码以获得更多见解。

最新更新