• 也发布一个asp.net中分页的代码


    代码和存储过程是针对AspNetPager。其中,使用环境为:VS2005 + sqlserver2005。
    CS代码如下:


    /*
        this class is for visual studio 2005 only
    */

    using System;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    public class Pager
    {
        
    private static readonly string selectSqlTp = "select * from( select [columns], row_number() over([orderby]) as __row__ from [table] [where])t where t.__row__ >= [low] and t.__row__ <= [high]";
        
    private static readonly string selectTotalTp = "select count(*) from [table] [where]";
        
        
    // 排序后,每个页面的内容项会变
        public static DataTable getTable(string table, string columns, string where,string orderby, string ascDesc, int pageIndex, int pageCount, ref int total)
        
    {
            
    string selectSql = getSelectSql(table, columns, where, orderby, ascDesc, pageIndex, pageCount);
            
    string totalSql = getTotalSql(table, where);
            
            System.Data.DataTable dt 
    = new System.Data.DataTable();
            SqlConnection conn 
    = new SqlConnection("[replaceConnectionString]");
            SqlCommand cmd 
    = new SqlCommand();
            cmd.Connection 
    = conn;
            cmd.CommandText 
    = "up_pager";
            
            SqlParameter []paras 
    = {new SqlParameter("@select_total_sql", totalSql),
                                                            
    new SqlParameter("@select_sql", selectSql),
                                                            
    new SqlParameter("@total", SqlDataType.Int)}
    ;
            paras[
    2].Direction = ParameterDirection.Output;
            cmd.Parameters.AddRange(paras);
            
            
    try{
                conn.Open();
                SqlDataReader dr 
    = cmd.ExcuteReader();
                
    if(dt.HasRows)
                
    {
                    dt.Load(dr);
                    total 
    = (int)paras[2].Value;
                }

            }

            
    catch(Exception ex)
            
    {
                
    throw ex;
            }

            
    finally 
            
    {
                
    if(conn.Status == ConnectionStatus.Open)
                conn.Close();
                cmd.Parameters.Clear();
            }

            
    return dt;
        }

        
        
    private static string getSelectSql(string table, string columns, string where,string orderby, string ascDesc, int pageIndex, int pageCount)
        
    {
            System.Text.StringBuilder sbSelect 
    = new System.Text.StringBuilder(selectSqlTp);
            sbSelect.Replace(
    "[table]", table);
            sbSelect.Replace(
    "[columns]", columns);
            
    if(where == null || where == "")
                sbSelect.Replace(
    "[where]""where 1=1");
            
    else sbSelect.Replace("[where]", where);
            
    string order  = "order by " + orderby;
            
    if(ascDesc != null && ascDesc !="")
                order 
    += " "+ascDesc;
            sbSelect.Replace(
    "[orderby]", order);
            
    int low = (int)(pageIndex * pageCount);
            
    int high = (int)((pageIndex + 1* pageCount);
            sbSelect.Replace(
    "[low]""" + low);
            sbSelect.Replace(
    "[high]""" + high);
            
    return sbSelect.ToString();
        }

        
        
    private static string getSelectTotal(string table, string where)
        
    {
            System.Text.StringBuilder sbTotal 
    = new System.Text.StringBuilder(selectTotalTp);
            sbTotal.Replace(
    "[table]", table);
            
    if(where == null)
                where 
    = "where 1=1";
            sbTotal.Replace(
    "[where]", where);
            
    return sbTotal.ToString();
        }

    }


    下面是存储过程,很简单:

    -- the pager procedure
    -- zyl
    -- version: 2.0
    CREATE PROCEDURE up_pager
        @select_total_sql nvarchar(
    1000),
        @select_sql nvarchar(
    2000),
        @total 
    int output
    as
        exec sp_executesql @select_sql
        exec @total 
    = sp_executesql @select_total_sql
        
    return @@rowcount

    GO

    上面的代码是参照一位师兄的代码写的,在此表示感谢。

  • 相关阅读:
    限制并方便用户输入
    更新QQ客户端缩略图//不加好友聊QQ
    XPManifest控件的作用与用法
    传奇SF一机多区路由器的设置
    SQL查询语句使用详解
    汇编学习笔记3
    汇编学习笔记2
    汇编学习笔记6
    汇编学习笔记4
    汇编学习笔记11
  • 原文地址:https://www.cnblogs.com/qkhh/p/869624.html
Copyright © 2020-2023  润新知