最近在做网站是遇到了用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。
错误代码:
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。