关闭

Asp.net 中通用分页解决办法

651人阅读 评论(0) 收藏 举报

www.studyez.com网站的开发中,经常涉及到数据的分页.搞得人是不胜其烦.因此就想尽量想把分页相关统一起来.

因此,就把分页的统一了一下.暂时记录一下.

------------------------------------CSer.cs源码-----------------------------

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
/// <summary>
/// Summary description for BaseSplitPage
/// </summary>
public abstract class BaseSplitPage<T, U> : IDisposable where U : List<T>, new()
{
    private SqlConnection _cnn;

    private SqlCommand _cproc;
    private SqlCommand _lproc;

    protected BaseSplitPage(string countProc, string listProc)
    {
        //
        // TODO: Add constructor logic here
        //
        _cnn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[0].ConnectionString);
        _cproc = new SqlCommand(countProc, _cnn);
        _cproc.CommandType = CommandType.StoredProcedure;
        _cproc.Parameters.Add("@strFilter", SqlDbType.NVarChar, 200);
        _lproc = new SqlCommand(listProc, _cnn);
        _lproc.CommandType = CommandType.StoredProcedure;
        _lproc.Parameters.Add("@startRecord", SqlDbType.Int);
        _lproc.Parameters.Add("@maxRecord", SqlDbType.Int);
        _lproc.Parameters.Add("@strFilter", SqlDbType.NVarChar, 200);
        _lproc.Parameters.Add("@strOrder", SqlDbType.NVarChar, 200);

    }
    public int RecordCount(string strFilter)
    {
        int res = 0;
        try
        {
            _cnn.Open();
            _cproc.Parameters[0].Value = strFilter;
            res = (int)_cproc.ExecuteScalar();

        }
        finally
        {
            _cnn.Close();
        }
        return res;

    }
    public U List(int startRecord, int maxRecord, string strFilter, string strOrderExpress)
    {
        U u = new U();
        try
        {
            _cnn.Open();
            _lproc.Parameters[0].Value = startRecord;
            _lproc.Parameters[1].Value = maxRecord;
            _lproc.Parameters[2].Value = strFilter;
            _lproc.Parameters[3].Value = strOrderExpress;
            SqlDataReader dr = _lproc.ExecuteReader(CommandBehavior.CloseConnection);
            if (dr != null)
            {
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        u.Add(CreateFormDB(dr));
                    }
                }
                dr.Close();
            }

        }
        finally
        {
            _cnn.Close();
        }
        return u;
    }

    protected abstract T CreateFormDB(IDataRecord dr);

    #region IDisposable Members
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (_cnn != null)
                _cnn.Dispose();
            if (_cproc != null)
                _cproc.Dispose();
            if (_lproc != null)
                _lproc.Dispose();
        }
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    #endregion
}
public enum Sex
{
    /// <summary>
    /// 公的
    /// </summary>
    Gong,
    /// <summary>
    /// 母的
    /// </summary>
    Mu,
    /// <summary>
    /// 太监
    /// </summary>
    TaiJian

}
[Serializable]
public class Cser
{
    private int _age;
    private Sex _sex;
    public Cser()
    {
    }
    public Cser(int age, Sex sex)
    {
        _age = age;
        _sex = sex;

    }
    public int Age
    {
        get { return _age; }
        set { _age = value; }
    }
    public Sex GorM
    {
        get { return _sex; }
        set { _sex = value; }
    }
}
public class CserCollection:List<Cser>
{
}
public class CserSplitPage:BaseSplitPage<Cser ,CserCollection>
{
   public CserSplitPage():base("cs_userCount","cs_userList")
   {
   }


   protected override Cser CreateFormDB(IDataRecord dr)
   {
         int age=(int)dr["Age"];
         Sex sex=(Sex)Enum.Parse(typeof(Sex),(string)dr["Sex"]);
         return new Cser(age,sex );
   }
}

 

public class   UserPage
{
         public void Run()
         {
             using(CserSplitPage sp=new CserSplitPage())
             {
                sp.RecordCount(" Age=10");
             }
         }
}

 

这个东西支持单表还行,多表联合就不怎么好用.看过也用过一些ORM产品,可是老是找不到一个省心省事.

----------------------------------End--------------------------------------
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:224948次
    • 积分:3555
    • 等级:
    • 排名:第9230名
    • 原创:120篇
    • 转载:75篇
    • 译文:0篇
    • 评论:26条
    最新评论