动软实现分页源代码分析

1、DAL 中的代码更改为以下代码:
        /// <summary>
        /// 分页获取数据列表
        /// IsCount=0 返回记录总数,否则,返回记录集
        /// 分页存储过程UP_GetRecordByPage
        /// </summary>
        public DataSet GetList(int IsCount, int PageIndex, string strWhere)
        {
            //这里,为了不更改动软生成的代码太多部分,
            //将原来的第一个参数PageSize 作为一个标志使用
            //如果PageSize=0 ,即参数IsCount=0
            //则为计算总数
            //在传递给存储过程时,@IsCount=1
            //否则,就接收到了PageSize 的值

            string tableName = "LWord";    //排序表名***********
            string colName = "LWordID";    //排序字段***********

            //得到记录总数,如果保存统一的一个 PageSize,则可以从全局设置中直接取。
            int PageSize = IsCount;
            if (IsCount == 0)     //如果IsCount =0 则,返回记录总数
            {
                SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                };
                parameters[0].Value = tableName;
                parameters[1].Value = colName;
                parameters[2].Value = PageSize;
                parameters[3].Value = PageIndex;
                parameters[4].Value = 1;      //@IsReCount 1 (非)返回总数,否则,返回记录集
                parameters[5].Value = 1;
                parameters[6].Value = strWhere;

                return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
            }
            else
            {
                SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };

                parameters[0].Value = tableName;
                parameters[1].Value = colName;
                parameters[2].Value = PageSize;
                parameters[3].Value = PageIndex;
                parameters[4].Value = 0;     //@IsReCount  (非)返回总数,返回记录集
                parameters[5].Value = 1;
                parameters[6].Value = strWhere;

                return DbHelperSQL.RunProcedure("UP_GetRecordByPage", parameters, "ds");
            }
        }

2、在数据库中写如下存储过程:UP_GetRecordByPage(就用这个名字,因为 DAL 中都用的是这个。)

------------------------------------
--用途:支持任意排序的分页存储过程
--说明:
------------------------------------
CREATE PROCEDURE [dbo].[UP_GetRecordByPageOrder]

@tblName varchar(255), -- 表名
@fldName varchar(255), -- 显示字段名
@OrderfldName varchar(255), -- 排序字段名
@StatfldName varchar(255), -- 统计字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100) -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @OrderfldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @OrderfldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['
+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @OrderfldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + '] where [' + @OrderfldName + ']' + @strTmp + '(['
+ @OrderfldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @OrderfldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp = ''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' ' + @fldName + ' from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end

if @IsReCount != 0
set @strSQL = @strSQL+' select count(1) as Total from [' + @tblName + ']'
if @strWhere!=''
set @strSQL = @strSQL+' where ' + @strWhere
exec (@strSQL)

2,
------------------------------------
--用途:分页存储过程(对有主键的表效率极高)
--说明:
------------------------------------
CREATE PROCEDURE [dbo].[UP_GetRecordByPage]
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@IsReCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '' -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100) -- 临时变量(查询条件过长时可能会出错,可修改100为1000)
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
if @PageIndex = 1
begin
set @strTmp =''
if @strWhere != ''
set @strTmp = ' where ' + @strWhere
set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
+ @tblName + ']' + @strTmp + ' ' + @strOrder
end
if @IsReCount != 0
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
exec (@strSQL)

你可能感兴趣的:(源代码)