如何用存储过程实现在表的分页浏览,传入参数pageindex和pagesize,

zj2050 2008-10-21 12:40:55
如何用存储过程实现在表的分页浏览,传入参数pageindex和pagesize = 16,

表中数据量大,希望每次通过传入页参数得到分页结果

谢谢
...全文
427 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chendi0301 2009-01-10
  • 打赏
  • 举报
回复
学习
zj2050 2008-10-21
  • 打赏
  • 举报
回复
fcuandy你好,
感谢你的回复,我现在就是希望在你上次解答的题目
http://topic.csdn.net/u/20081011/09/54a1b9be-db31-412a-b26b-d5065fdd1fa2.html
的基础上实现分页显示,针对原来的那个题目该如何重写为带分页的存储过程
谢谢,查询到的数据可能较大
zj2050 2008-10-21
  • 打赏
  • 举报
回复
你好,

我用的是sql server2005,那么还需要这么复杂么?

[Quote=引用 1 楼 fcuandy 的回复:]
在网上随便搜搜分页存储过程能搜一堆。

简意就是:

select top @pagesize * from tb where id>
(select max(id) from
(select top @pagesize * (@pageindex-1) id from tb)
)

sql2000下不持top接变量,所以用动态语句去拼的。
[/Quote]
等不到来世 2008-10-21
  • 打赏
  • 举报
回复

declare @pageindex int,@pagesize int
select * from
(
select *,rn=row_number() over(order by id)
from tb
)t
where rn between (@pageindex-1)*@pagesize+1 and @pageindex*@pagesize

fcuandy 2008-10-21
  • 打赏
  • 举报
回复
在网上随便搜搜分页存储过程能搜一堆。

简意就是:

select top @pagesize * from tb where id>
(select max(id) from
(select top @pagesize * (@pageindex-1) id from tb)
)

sql2000下不持top接变量,所以用动态语句去拼的。
rucypli 2008-10-21
  • 打赏
  • 举报
回复
select *,from row_number() over(order by XXX) as row_num
from table
where row_num between (@pageindex-1)*@pagesize and @pageindex*@pageindex
水族杰纶 2008-10-21
  • 打赏
  • 举报
回复
DECLARE @pagenum AS INT, @pagesize AS INT 
SET @pagenum = 2
SET @pagesize = 3
SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY newsid DESC) AS rownum,
newsid, topic, ntime, hits
FROM news) AS D
WHERE rownum BETWEEN (@pagenum-1)*@pagesize+1 AND @pagenum*@pagesize
ORDER BY newsid DESC
概况: 1、 适用于.net项目,基于MVC架构模式 2、 按照数据库结构生成基础开发类库 3、 数据库支持SQL2000和SQL2005两个版本 4、 类库为分层框架工厂模式,依次分为:数据库交互层(SqlHelper)、业务体层(Model)、数据处理层(DAL)、数据接口层(IDAL)、数据工厂层(DALFactory)、业务层(BLL)6层 5、 业务体层与业务层直接与UI交互 6、 生成类库的同时系统自动生成相应的存储过程脚本(Proc文件夹)。 一、配置 打开SQLHelper.cs文件,将相应的数据库连接配置信息添加到配置文件中即可。 二、存储过程 打开Proc文件夹,将Proc.sql在相应SQL命令执行窗口中打开,然后执行即可。 二、业务体层 体类名为数据名称(首字母大写)加上Model后缀组成,类属性为相应字段名称(首字母大写),包含相应构造函数。 三、业务层 以下介绍以CalendarNotes为例,主键字段为id。 1、插入数据 public string Insert(CalendarNotesModel model) 插入单条数据,即业务体对象 public string Insert(List list) 插入多条数据,即泛型集合(集合元素为业务体对象) public string Insert(CalendarNotesModel model, object objTrans) 在指定事务控制中插入单条数据 public string Insert(List list, object objTrans) 在指定事务控制中插入多条数据 2、插入并返回所插入的记录的自增ID 适用范围为相应数据中存在标识字段,可同步获取到新插入数据的标识字段值。 public string Insert_Identity(CalendarNotesModel model) 插入单条数据 public string Insert_Identity(CalendarNotesModel model, object objTrans) 在指定事务控制中插入单条数据 3、更新数据 public string Update(CalendarNotesModel model) 更新单条数据,使用过程是:先取出相应业务体,然后修改字段值(非主键字段),然后调用此方法更新数据库。 public string Update(CalendarNotesModel model, object objTrans) 在指定事务控制中更新单条数据,调用过程同上。 4、删除数据 public string Delete(string id) 根据主键删除单条数据。 public string Delete(CalendarNotesModel model) 根据指定条件批量删除数据,调用过程:先例化一个CalendarNotesModel对象,然后设置删除条件(即给相应属性字段赋值),然后调用此方法。 public string Delete(string id, object objTrans) 在指定事务中删除单条数据。 public string Delete(CalendarNotesModel model, object objTrans) 在指定事务中批量删除数据。 5、查询数据 public List SelectAll() 查询出当前数据所有数据。 public CalendarNotesModel Select(string id) 根据主键查询单条数据。 public List Select(CalendarNotesModel model, string querytype) 根据指定条件查询数据集合,调用过程:先例化一个CalendarNotesModel对象,然后设置筛选条件(即给相应属性字段赋值),然后调用此方法,可通过querytype参数设置筛选出的数据集的排序,如” UpdateTime DESC”,默认可为空(””)。 public CalendarNotesModel Select(string id, object objTrans) 在指定事务控制中根据主键查询单条数据。 public List Select(CalendarNotesModel model, string querytype, object objTrans) 在指定事务控制中根据指定条件查询数据集合。
压缩包内有最近修改说明。以下为使用方法: 1、 适用于.net项目,基于MVC架构模式 2、 按照数据库结构生成基础开发类库 3、 数据库支持SQL2000和SQL2005两个版本 4、 类库为分层框架工厂模式,依次分为:数据库交互层(SqlHelper)、业务体层(Model)、数据处理层(DAL)、数据接口层(IDAL)、数据工厂层(DALFactory)、业务层(BLL)6层 5、 业务体层与业务层直接与UI交互 6、 生成类库的同时系统自动生成相应的存储过程脚本(Proc文件夹)。 一、配置 打开SQLHelper.cs文件,将相应的数据库连接配置信息添加到配置文件中即可。 二、存储过程 打开Proc文件夹,将Proc.sql在相应SQL命令执行窗口中打开,然后执行即可。 二、业务体层 体类名为数据名称(首字母大写)加上Model后缀组成,类属性为相应字段名称(首字母大写),包含相应构造函数。 三、业务层 以下介绍以CalendarNotes为例,主键字段为id。 1、插入数据 public string Insert(CalendarNotesModel model) 插入单条数据,即业务体对象 public string Insert(List list) 插入多条数据,即泛型集合(集合元素为业务体对象) public string Insert(CalendarNotesModel model, object objTrans) 在指定事务控制中插入单条数据 public string Insert(List list, object objTrans) 在指定事务控制中插入多条数据 2、插入并返回所插入的记录的自增ID 适用范围为相应数据中存在标识字段,可同步获取到新插入数据的标识字段值。 public string Insert_Identity(CalendarNotesModel model) 插入单条数据 public string Insert_Identity(CalendarNotesModel model, object objTrans) 在指定事务控制中插入单条数据 3、更新数据 public string Update(CalendarNotesModel model) 更新单条数据,使用过程是:先取出相应业务体,然后修改字段值(非主键字段),然后调用此方法更新数据库。 public string Update(CalendarNotesModel model, object objTrans) 在指定事务控制中更新单条数据,调用过程同上。 4、删除数据 public string Delete(string id) 根据主键删除单条数据。 public string Delete(CalendarNotesModel model) 根据指定条件批量删除数据,调用过程:先例化一个CalendarNotesModel对象,然后设置删除条件(即给相应属性字段赋值),然后调用此方法。 public string Delete(string id, object objTrans) 在指定事务中删除单条数据。 public string Delete(CalendarNotesModel model, object objTrans) 在指定事务中批量删除数据。 5、查询数据 public List SelectAll() 查询出当前数据所有数据。 public CalendarNotesModel Select(string id) 根据主键查询单条数据。 public List Select(CalendarNotesModel model, string querytype) 根据指定条件查询数据集合,调用过程:先例化一个CalendarNotesModel对象,然后设置筛选条件(即给相应属性字段赋值),然后调用此方法,可通过querytype参数设置筛选出的数据集的排序,如” UpdateTime DESC”,默认可为空(””)。 public CalendarNotesModel Select(string id, object objTrans) 在指定事务控制中根据主键查询单条数据。 public List Select(CalendarNotesModel model, string querytype, object objTrans) 在指定事务控制中根据指定条件查询数据集合。 6、分页查询 /// /// 分页查询,返回List数据集合 /// /// /// 查询条件 /// /// 排序条件 /// /// 查询页码 /// /// 每页记录条数 /// /// 输出参数:总记录数 /// 当前页数据集合 public List Select_Page(CalendarNotesModel model, string querytype, int PageIndex, int PageSize, out int TotalNumber) 按照指定条件分页查询数据集合,返回List集合,查询指定页。具体参数参照BLL层注释。 /// /// 分页查询,返回Json数据源 /// /// /// 查询条件 /// /// 排序条件 /// /// 查询页码 /// /// 每页记录条数 /// 当前页数据的JSON数据源(JSON格式参考Json转换方法) public string Select_Page(CalendarNotesModel model, string querytype, int PageIndex, int PageSize) 按照指定条件分页查询数据集合,返回JSON数据源,查询指定页。具体参数参照BLL层注释。 7、范围分页查询 /// /// 范围分页查询,查询介于model_min与model_max之间的体集合,两个model中都有值的属性为范围筛选,仅model_min中有值的属性为等于筛选 /// /// /// 起始model /// /// 结束model /// /// 比较标识:1为小于等于;默认为小于 /// /// 排序条件,如:field1 desc /// 符合条件的体集合 public List Select_Range_Page(CalendarNotesModel model_min, CalendarNotesModel model_max, string queryflag, string querytype, int PageIndex, int PageSize, out int TotalNumber) 按指定范围条件查询符合条件的数据集合,返回指定页的数据集合。 8、范围查询 /// /// 范围查询,查询介于model_min与model_max之间的体集合,两个model中都有值的属性为范围筛选,仅model_min中有值的属性为等于筛选 /// /// /// 起始model /// /// 结束model /// /// 比较标识:1为小于等于;默认为小于 /// /// 排序条件,如:field1 desc /// 符合条件的体集合 public List Select_Range(CalendarNotesModel model_min, CalendarNotesModel model_max, string queryflag, string querytype) 9、获取记录数 public int GetRowCount(CalendarNotesModel model) 获取符合条件的记录条数,没有符合条件的返回0。 10、判断是否存在符合条件的记录 public bool IsExists(string id) 根据主键判断是否存在符合条件的记录。 public bool IsExists(CalendarNotesModel model) 根据指定条件判断是否存在符合条件的记录。 11、格式化model对象(私有方法,不对外调用) private CalendarNotesModel InitModel_DefaultValue(CalendarNotesModel model) 按照数据库中数据字段的默认值设置初始化业务体。 private CalendarNotesModel InitModel_NullValue(CalendarNotesModel model) 按照空值字符串(生成代码时指定,默认为-1)初始化业务体。 12、XML数据源相关 注:XML数据源中元素标签为小写数据名称,各属性key为相应小写字段名称。 public CalendarNotesModel GetModelByXML(string xml) 解析xml数据源为业务体对象。 public string GetXMLByModel(CalendarNotesModel model) 解析业务体对象为xml数据源。 public List GetListByXML(string xml) 解析xml数据源为List数据集合。 public string GetXMLByList(List list) 解析List数据集合为xml数据源。 public string Insert(string xml) 将单条记录xml数据插入数据库 public string Update(string xml) 将单条记录xml数据更新到数据库 13、JSON数据源相关 注:json数据源中totalProperty属性为数据源总记录条数,root属性下是具体数据记录信息,数据记录信息中各key为小写字段名称,值均为字符串类型。 示例: { 'totalProperty':10, 'root':[ {'id':'001','name':'user1'}, {'id':'002','name':'user2'} ] } json字符串传至客户端后在javascript脚本下需将字符串转换为json数据对象,示例如下: var objJson = eval('('+strJson+')'); public string GetJsonByModel(CalendarNotesModel model) 解析业务体对象为json数据源。 public string GetJsonByList(List list) 解析List数据集合为json数据源,json数据源记录总条数即为List集合的元素个数。 public string GetJsonByList(List list, int count) 解析List数据集合为json数据源,json数据源记录总条数为传入count参数值,用于取分页数据。 四、事务控制 事务控制调用示例: CauCWareLibrary.BLL.BLLCalendarNotes objBLL = new CauCWareLibrary.BLL.BLLCalendarNotes(); try { //发起事务 object trans1 = CauCWareLibrary.SqlHelper.SQLHelper.StartTransaction("trans1"); //业务操作 CalendarNotesModel model1 = CauCWareLibrary.BLL.BLLCalendarNotes.Select("184", trans1); //提交事务 CauCWareLibrary.SqlHelper.SQLHelper.CommitTrancaction(trans1); } catch { //回滚事务 CauCWareLibrary.SqlHelper.SQLHelper.RollbackTrancaction(trans1); } 五、其他说明 1、各类文件均为分布类 易于扩展,避免项目开发过程中数据库结构调整所引起大量的基础类库代码维护工作,避免多个人维护同一个类时引起代码紊乱。 2、扩展存储过程说明 步骤如下: 1) 在数据库中新建存储过程; 2) 在DAL文件夹下新建分布类文件,添加相应using信息,类声明如下: public partial class DALCalendarNotes : IDALCalendarNotes 3) 添加public方法,调用存储过程,具体参考其他自动生成的代码; 4) 在IDAL文件夹下新建分布类文件,添加相应using信息,接口声明如下: public partial interface IDALCalendarNotes 5) 添加新增方法的接口声明; 6) 在BLL文件夹下新建分布类文件,添加相应using信息,类声明如下: public partial class BLLCalendarNotes 7) 添加新增业务方法即可。
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Configuration; using System.Data; using System.Data.SQLite; namespace DAL { public class Sqlite { /// /// 获得连接对象 /// /// public static SQLiteConnection GetSQLiteConnection() { return new SQLiteConnection("Data Source=" + System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["db"].ToString())); } private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Parameters.Clear(); cmd.Connection = conn; cmd.CommandText = cmdText; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 30; if (p != null) { foreach (object parm in p) cmd.Parameters.AddWithValue(string.Empty, parm); //for (int i = 0; i < p.Length; i++) // cmd.Parameters[i].Value = p[i]; } } public static DataSet ExecuteDataset(string cmdText, params object[] p) { DataSet ds = new DataSet(); SQLiteCommand command = new SQLiteCommand(); using (SQLiteConnection connection = GetSQLiteConnection()) { PrepareCommand(command, connection, cmdText, p); SQLiteDataAdapter da = new SQLiteDataAdapter(command); da.Fill(ds); } return ds; } public static DataRow ExecuteDataRow(string cmdText, params object[] p) { DataSet ds = ExecuteDataset(cmdText, p); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) return ds.Tables[0].Rows[0]; return null; } /// /// 返回受影响的行数 /// /// a /// 传入参数 /// public static int ExecuteNonQuery(string cmdText, params object[] p) { SQLiteCommand command = new SQLiteCommand(); using (SQLiteConnection connection = GetSQLiteConnection()) { PrepareCommand(command, connection, cmdText, p); return command.ExecuteNonQuery(); } } /// /// 返回SqlDataReader对象 /// /// /// 传入参数 /// public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p) { SQLiteCommand command = new SQLiteCommand(); SQLiteConnection connection = GetSQLiteConnection(); try { PrepareCommand(command, connection, cmdText, p); SQLiteDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); return reader; } catch { connection.Close(); throw; } } /// /// 返回结果集中的第一行第一列,忽略其他行或列 /// /// /// 传入参数 /// public static object ExecuteScalar(string cmdText, params object[] p) { SQLiteCommand cmd = new SQLiteCommand(); using (SQLiteConnection connection = GetSQLiteConnection()) { PrepareCommand(cmd, connection, cmdText, p); return cmd.ExecuteScalar(); } } /// /// 分页 /// /// /// /// /// /// /// /// public static DataSet ExecutePager(ref int recordCount, int pageIndex, int pageSize, string cmdText, string countText, params object[] p) { if (recordCount < 0) recordCount = int.Parse(ExecuteScalar(countText, p).ToString()); DataSet ds = new DataSet(); SQLiteCommand command = new SQLiteCommand(); using (SQLiteConnection connection = GetSQLiteConnection()) { PrepareCommand(command, connection, cmdText, p); SQLiteDataAdapter da = new SQLiteDataAdapter(command); da.Fill(ds, (pageIndex - 1) * pageSize, pageSize, "result"); } return ds; } } }

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧