本文实例讲述了C#基于数据库存储过程的AJAX分页实现方法。分享给大家供大家参考。具体如下:
首先我们在数据库(SQL Server)中声明定义存储过程
因为是直接访问数据库的,所以我们将下面这条方法写入到DAL层中,这里我将它写入到SqlHelper中
DataTable dt = new DataTable();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(dt);//到数据库去执行存储过程,并将结果填充到dt表中
}
//等存储过程执行完毕后,存储过程会把这两个输出参数传递出来。那么我们在这里来取得这两个返回参数。
rowCount = Convert.ToInt32(parameters[2].Value);
pageCount = Convert.ToInt32(parameters[3].Value);
return dt;
}
}
}
}
}
在DAL文件夹中( 层中) 创建一个Aticel.cs类 产生一个list
namespace WebApplication1.DAL
{
public class Aticel
{
public static List<Location> GetPageListByPageIndex(int pageSize,int currentpage,out int rowCount,out int pageCount)
{
DataTable dt= SqlHelper.ExecuteProcPageList(pageSize, currentpage,out rowCount,out pageCount);
var list = new List<Location>();// 声明一个泛型对象list
if (dt != null && dt.Rows.Count > 0)
{
//将DataTable转换成一个list
list = (from p in dt.AsEnumerable() //(遍历DataTable)
select new Model.Location
{
Locid = p.Field<int>("locid"), //将DateTable里的字段赋值给Location类中的属性
LocName = p.Field<string>("locName"),
ParentId = p.Field<int>("parentId"),
LocType = p.Field<short>("locType"),
ElongCode = p.Field<string>("elongCode"),
CityCode = p.Field<string>("CityCode"),
BaiduPos = p.Field<string>("BaiduPos"),
Versions = p.Field<short>("Version")
}).ToList();
}
return list; //将这个list返回回去
}
}
}
在API这个文件夹中创建一个GetPageData.ashx 页 (BLL层) 在这里调用ADL层里的 Aticel.cs类中的GetPageListByPageIndex()方法,获取一个list 并将这个list转换成一个Json格式字符串, 共AJAX 异步请求得到。
前端页面 (将AJAX请求得到的数据展示也页面)
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答