; {
BindGrid();
}
}
protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGrid();
}
public void BindGrid()
{
this.AspNetPager1.RecordCount = Int32.Parse(db.GetAllCount().ToString());
int pageIndex = this.AspNetPager1.CurrentPageIndex - 1;
int pageSize = this.AspNetPager1.PageSize =5;
Repeater1.DataSource = db.GetCurrentPage(pageIndex, pageSize);
Repeater1.DataBind();
}
//前台已经OK了,就差后台数据库连接及分页需要的方法,本项目数据库示例为PUBS数据库[安装SQL SERVER就有的],分页的表为jobs表
using System.Data.SqlClient;
public class DBAccess
{
private SqlConnection con;
private string DBName = "pubs";
//创建连接对象并打开
public void Open()
{
if (con == null)
con = new SqlConnection("server=(local);uid=sa;pwd=sa;database="+DBName);
if (con.State == ConnectionState.Closed)
con.Open();
}
//创建一个命令对象并返回该对象
public SqlCommand CreateCommand(string sqlStr)
{
Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlStr;
cmd.Connection = con;
return cmd;
}
//生成一个对象并返回该结果集第一行第一列
public object GetScalar(string sqlStr)
{
SqlCommand cmd = CreateCommand(sqlStr);
object obj = cmd.ExecuteScalar();
//CommadnBehavior.CloseConnection是将于DataReader的数据库链接关联起来
//当关闭DataReader对象时候也自动关闭链接
return obj;
}
//执行数据库查询并返回一个数据集 [当前页码,每页记录条数]
public DataSet GetCurrentPage(int pageIndex, int pageSize)
{
|