[英]C#: SQL “Select Top..” query does not return any rows?
我需要:
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);
之后 -將限制添加到查詢的末尾,而不是像MS SQL Server的“頂部”命令那樣在列表的開頭選擇列表
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);
最后,您必須使用“限制1”。 MYSQL中沒有“ TOP 1”。 我猜這是SQLServer命令。
做:
SqlCommand cmd = new SqlCommand("SELECT NodeID FROM ActivityTable WHERE NodeParent='" + nodeid_previous + "'" + " ORDER BY Time DESC Limit 1", con);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.