我的SQL db存储过程返回结果,但datareader显示 "Enumeration Yielded No Results"?
这是我的代码。
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())
{
…
}
}
}
存储过程
CREATE PROCEDURE 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:)
这是因为你的存储过程没有返回这些输入值的数据。你可能会想检查阅读器是否有任何行,使用 HasRows
财产
if(!reader.HasRows)
{
// return empty response model
}
看起来你已经在检查行了,如下图所示(从你发布的代码来看)。那么在这种情况下,我相信你在调试时肯定会从调试器中得到这个错误。
if (reader.HasRows)
{
while (reader.Read())
如果不是这种情况,那么你应该把你的程序代码也贴出来,以便获得更多的信息。