简体   繁体   English

为什么 DataReader 给出“枚举没有结果”?

[英]Why is DataReader giving “Enumeration Yielded No Results”?

My SQL db stored proc returns results but datareader shows “Enumeration Yielded No Results”?我的 SQL db 存储过程返回结果,但 datareader 显示“枚举产生无结果”?

this is my code:这是我的代码:

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())
                        {
                        …
                        }
                    }
                 }

Stored proc存储过程

CREATE PROCEDURE Getnpida创建程序 Getnpida

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

ance:)安斯:)

That's cause mostly your stored procedure returned no data with those input values.这主要是因为您的存储过程没有返回带有这些输入值的数据。 You might want to check if the reader has any rows using the HasRows property您可能想使用HasRows属性检查阅读器是否有任何行

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

Well looks like you are already checking for rows as in below (from your posted code).看起来您已经在检查下面的行(来自您发布的代码)。 Then in that case, I believe surely you are getting that error from debugger while debugging那么在这种情况下,我相信您肯定会在调试时从调试器中收到该错误

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

if that's not the case then you should post your procedure code as well to gain more insight.如果不是这种情况,那么您也应该发布您的程序代码以获得更多洞察力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM