SqlDataReader.Read()总是返回false [英] SqlDataReader.Read() always returning false

查看:1032
本文介绍了SqlDataReader.Read()总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

using (SqlConnection conexao = new SqlConnection(ConnectionString))
{
    SqlCommand comando = new SqlCommand(query, conexao);
    comando.Parameters.AddWithValue("id", idUsuario);
    conexao.Open();
    SqlDataReader reader = comando.ExecuteReader(CommandBehavior.SingleRow);
    if (reader.Read())
    {
        Hydrate(out entity, reader);
    }
}

所以,如果包含有效的结果和 HasRows ==真,然后阅读器.Read()应返回true,对吧?

So, if reader contains valid results and HasRows == true, then reader.Read() should return true, right?

那么,它不适合我。我不知道是怎么回事,因为水合物(出实体,读卡器); 行从未得到击中

Well, it doesn't for me. I have no idea of what is going on, because the Hydrate(out entity, reader); line is never getting hit.

是否有人可以帮助我理解?

Can someone please help me understand this?

感谢您!

推荐答案

如果它不是最后一行其实,什么情况是SqlDataReader.Read返回true。

Actually, what happens is SqlDataReader.Read returns true if it is NOT the last row.

您的行为规定SingleRow,让读者对齐读者的第一行,并说:没有经此一左行返回false。

Your behavior specifies "SingleRow", so the reader aligns the reader to the first row, and returns false saying "there are no rows left after this one".

请参阅有关此文档链接在这里:<一href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx">http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

See this link Here for the documentation on this: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

真实的,如果有更多的行;否则为false。

"true if there are more rows; otherwise false."

令人感兴趣的,但是,它们的实施​​例似乎是基于该句子他们会忽略最后一行...

Of interest, however, their examples seem like they would ignore the very last row based on that sentence...

这篇关于SqlDataReader.Read()总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆