判断SqlDataReader 记录集是否为空的应用(原)

最近在做网站是遇到了用SqlDataReader 绑定ListView少一条记录的问题。记录下来以备查用:
错误代码:
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rs = cmd.ExecuteReader();
if (rs.Read())
{
      ListView_cat.DataSource = rs;
      ListView_cat.DataBind();
}
正确代码:
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rs = cmd.ExecuteReader();
if (rs.HasRows)
{
      ListView_cat.DataSource = rs;
      ListView_cat.DataBind();
}
使用rs.Read()记录就会读取一条所以在此要用rs.HasRows。
posted @ 2009-08-05 15:14  .net 学习  阅读(1203)  评论(0编辑  收藏  举报