VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • C# ORM学习笔记:Dapper基本用法(4)

    6.1、简单分页

复制代码
        /// <summary>
        /// 简单分页
        /// </summary>
        /// <param name="beginRowNum"></param>
        /// <param name="endRowNum"></param>
        /// <returns></returns>
        public IEnumerable<Student> GetPaging(int beginRowNum = 1, int endRowNum = 5)
        {
            string strSql =
                    "SELECT * FROM " +
                        "( " +
                            "SELECT A.*, ROW_NUMBER() OVER(ORDER BY A.StudentID) RowNum " +
                            "FROM Student AS A " +
                        ") B " +
                    "WHERE B.RowNum BETWEEN @BeginRowNum AND @EndRowNum " +
                    "ORDER BY B.RowNum ";

            using (var conn = GetCon())
            {
                return conn.Query<Student>(strSql, new { BeginRowNum = beginRowNum, EndRowNum = endRowNum });
            }
        }
复制代码

    6.2、通用分页

复制代码
CREATE PROCEDURE [dbo].[PageList]
     @TableName VARCHAR(200),       --表名
     @FieldName VARCHAR(500) = '*', --字段名
     @Where VARCHAR(100) = NULL,    --条件语句
     @GroupBy VARCHAR(100) = NULL,  --分组字段
     @OrderBy VARCHAR(100),         --排序字段
     @PageIndex INT = 1,            --当前页数
     @PageSize INT = 20,            --每页显示记录数
     @TotalCount INT = 0 OUTPUT     --总记录数
AS
BEGIN
    --SQL拼接语句
    DECLARE @SQL NVARCHAR(4000)

    --总记录数
    SET @SQL='SELECT @RecordCount=COUNT(1) FROM ' + @TableName
    IF (ISNULL(@Where,'')<>'')
        SET @SQL=@SQL+' WHERE '+@Where
    ELSE IF (ISNULL(@GroupBy,'')<>'')
        SET @SQL=@SQL+' GROUP BY '+@GroupBy

    EXEC SP_EXECUTESQL @SQL,N'@RecordCount INT OUTPUT',@TotalCount OUTPUT

    --总页数
    DECLARE @PageCount INT
    SELECT @PageCount=CEILING((@TotalCount+0.0)/@PageSize)
    
    --简单分页
    SET @SQL='SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY '+@OrderBy+') AS RowNum,' + @FieldName + ' FROM '+@TableName+' AS A'
    IF (ISNULL(@Where,'')<>'')
        SET @SQL=@SQL+' WHERE '+@Where
    ELSE IF (ISNULL(@GroupBy,'')<>'')
        SET @SQL=@SQL+' GROUP BY '+@GroupBy

    IF (@PageIndex<=0)
        SET @PageIndex=1
    IF @PageIndex>@PageCount
        SET @PageIndex=@PageCount
     
    DECLARE @BeginRowNum INT,@EndRowNum INT  
    SET @BeginRowNum=(@PageIndex-1)*@PageSize+1
    SET @EndRowNum=@BeginRowNum+@PageSize-1

    SET @SQL=@SQL + ') AS B WHERE B.RowNum BETWEEN '+CONVERT(VARCHAR(32),@BeginRowNum)+' AND '+CONVERT(VARCHAR(32),@EndRowNum)
    EXEC(@SQL)
END
复制代码
复制代码
        /// <summary>
        /// 通用分页
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T> GetCommonPaging<T>(string tableName, string fieldName, string where, string groupby, string orderby, int pageIndex, int pageSize)
        {
            var para = new DynamicParameters();
            para.Add("TableName", tableName);
            para.Add("FieldName", fieldName);
            para.Add("Where", where);
            para.Add("GroupBy", groupby);
            para.Add("OrderBy", orderby);
            para.Add("PageIndex", pageIndex);
            para.Add("PageSize", pageSize);
            para.Add("TotalCount", dbType: DbType.Int32, direction: ParameterDirection.Output);

            using (var conn = GetCon())
            {
                conn.Open();
                return conn.Query<T>("PageList", para, commandType: CommandType.StoredProcedure);
            }
        }
复制代码


相关教程