我在 ASP.NET 中使用 SqlDataReader,在 load_page 事件中有问题

I am using SqlDataReader in ASP.NET and at load_page event have as issue

protected void Page_Load(object sender, EventArgs e)
{
    con = new SqlConnection(cnString);
    con.Open();

    cmd = new SqlCommand("select * from about_us", con);
    SqlDataReader reader = cmd.ExecuteReader();

    if (!reader.HasRows)
    {
        lblservicedesc.Text = "No Records";
        lblshortdesc.Text = "No Records";
    }
    else
    {
        reader.Read();
        lblshortdesc.Text = reader["AboutShortDescription"].ToString();
        lblservicedesc.Text = reader["ServicesDescription"].ToString();

        reader.Close();
        con.Close();
    }
}

将您的代码更改为如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    con = new SqlConnection(cnString);
    con.Open();

    cmd = new SqlCommand("select * from about_us", con);
    SqlDataReader reader = cmd.ExecuteReader();

    lblservicedesc.Text = "No Records";
    lblshortdesc.Text = "No Records";

    while (reader.Read())
    {
        lblshortdesc.Text = reader["AboutShortDescription"].ToString();
        lblservicedesc.Text = reader["OnGoingProjectsDescription"].ToString();
    }

    reader.Close();
    con.Close();
}

请记住,如果您有多行,您的标签只会显示最后一行的值。