一般的数据分页只能展示数据,当数据修改后,分页将不起作用;我现在采用数据存储来实现分页,缩短程序加载数据的时间,数据修改后,刷新,任然能实现数据分页
==================存储过程====================================
--查询预约所以数据并分页处理
CREATE PROCEDURE Pagination
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1 -- 页码
AS
declare @strSQL varchar(5000) -- 主语句
set @strSQL ='select top ' +str(@PageSize) +' * from sickinfo where id>(select isnull(max(id),0) from(select top '+ str((@PageIndex-1)*@PageSize) +' id from sickinfo order by id )as T)order by id '
if(@PageIndex =1)
begin
set @strSQL='select top '+str(@PageSize)+' * from sickinfo order by id desc '
end
else
begin
set @strSQL ='select top ' +str(@PageSize) +' * from sickinfo where id<(select isnull (min(id),0) from(select top '+str((@PageIndex-1)*@PageSize) +' id from sickinfo order by id desc )as T)order by id desc '
end
exec ( @strSQL)
GO
==========================================================
类文件
//利用存储过程来查询出所有的记录并进行分页处理
public DataSet ExecuteDataSetProc(string strSQL,int pagesize ,int pageindex)
{
CheckConnection();
DataSet ds1 = new DataSet();
try
{
cmd = new SqlCommand(strSQL, con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] para ={ new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@PageIndex", SqlDbType.Int) };
para[0].Value = pagesize;
para[1].Value = pageindex;
cmd.Parameters.AddRange(para);
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
dap.Fill(ds1, "sickinfo");
}
catch (Exception e)
{
e.Message.ToString();
}
finally
{
con.Close();
}
return ds1;
}
窗体上调用
public void BindData()
{
this.dataGridView1.DataSource = db.ExecuteDataSetProc("Pagination",PageSize, NowPage).Tables["sickinfo"];
}