C#:SQL”;选择顶部";查询不返回任何行
本文关键字:查询 quot 任何行 返回 顶部 SQL 选择 | 更新日期: 2024-09-20 14:36:45
我需要:
1)按"NodeParent"(我提供)筛选行
2)按"时间"对行进行排序,并获得最近的条目
这是我写的:
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("SELECT TOP 1 NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
nodeid_previous = rdr["NodeID"].ToString();
break;
}
rdr.Close();
}
else
{
//so on and so forth..
}
}
然而,即使我有符合这些条件的行,这也不会返回任何结果。查询正确吗?:)
之前
SqlCommand cmd = new SqlCommand("SELECT TOP 1 NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC", con);
After-限制被添加到查询的末尾,而不是像MS SQL Server的Top命令那样添加到选择列表的开头
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);
最后必须使用"限制1"。MYSQL中没有"TOP 1"。我猜是SQLServer命令。
Do:
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);