【发布时间】:2012-11-09 01:53:55
【问题描述】:
在 c# 控制器上编写 select 语句以进行分页的正确方法是什么。这是我想出的最好的方法,但我知道它不起作用,因为它在网格的第一页上显示了所有数据......请帮助
public JsonResult getData(int start, int limit)
{
List<MyItem> items = new List<MyItem>();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices1"].ConnectionString))
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT State, Capital FROM MYDBTABLE";
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MyItem item = new MyItem();
item.State = reader[0].ToString();
item.Capital = reader[1].ToString();
items.Add(item);
}
con.Close();
if ((start + limit) > Myitem.Count)
{
limit = Myitem.Count - start;
}
return Json(new { myTable = items }, JsonRequestBehavior.AllowGet);
}
}
【问题讨论】:
-
为什么不在查询中使用limit关键字?
-
我很抱歉 mserioli.. 我对此完全陌生...你能给我举个例子吗...
-
请参阅this article 并通读all of the comments。
-
嗨 Aaron... m 使用 sql 2008... 是一样的
-
是的,我指出的文章是为 SQL Server 2005 编写的,但它仍然适用于 SQL Server 2008。
标签: c# sql sql-server-2008 select pagination