<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="study._Default" %>
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; int pageSize = int.Parse(context.Request.Params["pagesize"]); //每页记录数 int pageIndex = int.Parse(context.Request.Params["index"]); //当前页索引 int type = int.Parse(context.Request.Params["type"]); //1为获取总页数,0为获取分页数据
if (type == 1) { int recordCount = GetRecordCount("select count(*) from MyTest"); int pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(recordCount) / pageSize)); string str = pageCount.ToString(); context.Response.Write(str); } else { string sql = string.Format("select id,Name,ClassName from ( select row_number() over (order by id) as rowNum,* from MyTest) as t " + " where rowNum>{0} and rowNum<={1}", (pageIndex - 1) * pageSize, pageIndex * pageSize); System.Data.DataTable dt = Getds(sql).Tables[0]; string str = "[" + JsonHelper.DataTableToJSON(dt) + "]"; context.Response.Write(str); } } public int GetRecordCount(string sql) { SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=test;Integrated Security=True"); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); return Convert.ToInt32(cmd.ExecuteScalar().ToString()); } public DataSet Getds(string sql) { SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=test;Integrated Security=True"); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); DataSet ds = new DataSet(); SqlDataAdapter adp = new SqlDataAdapter(cmd); adp.Fill(ds); return ds; } public bool IsReusable { get { return false; } }