asp.net分页类,sqlserver

using System;
using System.Data;
namespace cosvote
{
 /// 
 /// page_size_ct 的摘要说明。
 /// 
 public class page_size_ct
 {
  public page_size_ct()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  int page_size;//页尺寸
  int orderby_tag;//排序方式(0为asc,1为desc)
  string tb_name; //表名
  string field_list; //字段列表
  int page_no=0;//页码
  string condition_str;//条件字段串(不加where)
  string primary_key;// 主键(必须而且要自动编号无重复)

  public int pagesize  //pagesize 属性
  {
   set
   {
    page_size=value;
   }
   get
   {
    return(page_size);
   }
  }
  public int orderbytag  //排序标志 属性(0为正序1为倒序)
  {
   set
   {
    orderby_tag=value;
   }
   get
   {
    return(orderby_tag);
   }
  }

  public string tbname  //表名 属性
  {
   set
   {
    tb_name=value;
   }
   get
   {
    return(tb_name);
   }
  }
  public string fieldlist  //字段列表 属性
  {
   set
   {
    field_list=value;
   }
   get
   {
    return(field_list);
   }
  }

  public int pageno  //页码 属性
  {
   set
   {
    if (value!=0)
    {
     page_no=value;
    }
    else
     page_no=1;
    
   }
   get
   {
    return(page_no);
   }
  }

  public string condition  //条件 属性
  {
   set
   {
    condition_str=value;
   }
   get
   {
    return(condition_str);
   }
  }

  public string primarykey  //关键字 属性
  {
   set
   {
    primary_key=value;
   }
   get
   {
    return(primary_key);
   }
  }
  
  //组合查询语句
  public System.Data.DataTable page_tb()
  {
   string condition_tag;  //存放按最大ID或最小ID查询
   string orderbystr;     //存放orderby字符串
   string strsql;         //组合后的查询字符串
   if (this.orderbytag==0)
   {
    
    condition_tag=" > (select max("+ this.primarykey +") from ( select top ";
    orderbystr=" order by "+ this.primarykey + " asc ";
   }
   else
   {
    condition_tag=" < ( select min("+ this.primarykey +") from (select top ";
    orderbystr=" order by "+ this.primarykey +" desc ";
   }
   if (this.pageno==1) 
   {
    if (this.condition=="")
    {
     strsql="Select top "+ this.pagesize +" " + this.fieldlist + " from "+ this.tbname + orderbystr;
    }
    else
    {
     strsql="Select top "+ this.pagesize +" " + this.fieldlist + " from "+ this.tbname ;
     strsql+=" where "+ this.condition + orderbystr;
    }
    
   }
   else
   {
    if (this.condition=="")
    {
   
     strsql="Select top "+ this.pagesize +" " + this.fieldlist + " from "+ this.tbname ;
     strsql+=" where "+ this.primarykey + condition_tag +((this.pageno -1)*this.pagesize) + " "+this.primarykey +" from " + this.tbname;
     strsql+= orderbystr+ ") as maxid )" + orderbystr;
    }
    else
    {
     strsql="Select top "+ this.pagesize +" " + this.fieldlist + " from "+ this.tbname ;
     strsql+=" where ( "+ this.primarykey + condition_tag +((this.pageno -1)*this.pagesize) + " "+ this.primarykey +" from " + this.tbname;
     strsql+=" where "+ this.condition +" " + orderbystr+ ") as maxid )" + " ) and ( " +this.condition + " )"+orderbystr;
    }
   }
   getconnect conn=new getconnect();
   conn.openconn();
   DataSet ds=new DataSet();
   try
   {
    
    System.Data.SqlClient.SqlDataAdapter adp=new System.Data.SqlClient.SqlDataAdapter(strsql,conn.conn);
    adp.Fill(ds);
    return(ds.Tables[0]);
   }
   catch (Exception e)
   {
    System.Web.HttpContext.Current.Response.Write (e.Message);
    conn.closeconn();
    return(new DataTable());
   }
   finally
   {
    conn.closeconn();
   }
  }

  //返回总行数
  public long recordcount()
  {
   string strsql;
   if (this.condition.ToString()!="")
   {
    strsql="Select Count(" + this.primarykey +")  as counts from "+ this.tbname + " where " + this.condition.ToString() ;
   }
   else
   {
    strsql="Select Count(" + this.primarykey +")  as counts from "+ this.tbname ;
   }
   getconnect conn=new getconnect();
   conn.openconn();
   System.Data.SqlClient.SqlCommand cmd=new  System.Data.SqlClient.SqlCommand();
   cmd.CommandText=strsql;
   cmd.Connection=conn.conn;
   System.Data.SqlClient.SqlDataReader rd;
   try
   {
    rd= cmd.ExecuteReader();
    if (rd.HasRows==true)
    {
     rd.Read();
     return(long.Parse(rd["counts"].ToString()));
    }
    else
    {
       return(0);
    }
   }
   catch (Exception e)
   {
    System.Web.HttpContext.Current.Response.Write (e.Message);
    conn.closeconn();
    cmd.Dispose();
    return(0);
   }
   finally
   {
    conn.closeconn();
   }
  }
 }
}