gpt4 book ai didi

C#:SQL 从 SqlDataReader 获取最后一行?

转载 作者:行者123 更新时间:2023-11-29 04:40:53 29 4
gpt4 key购买 nike

我需要对表格列中的日期进行排序,并从阅读器中获取最后一行(即:最新日期)。我尝试了以下方法:

using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("Select * From ActivityTable
ORDER BY CONVERT(DATE, Date) ASC", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();

//I need to skip reading all the rows UNTIL the last row. I only need the last row
while (rdr.Read())
{
locationOfPrevious = rdr["Location"].ToString();
nodeid_previous = rdr["NodeID"].ToString();

}
rdr.Close();
}

我相信查询是正确的,但我无法从查询结果中获取最后一行(即根据排序记录的最后日期)。你们有什么建议?谢谢大家:)

最佳答案

我建议按 desc 排序并占据第一行 - 效率更高

关于C#:SQL 从 SqlDataReader 获取最后一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28815257/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com