网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > DotNet > ADO.NET > ADO.NET 数据库访问之数据分页  

ADO.NET 数据库访问之数据分页

2008-04-28  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

    程序尽可能地降低其使用的复杂性,它的优点是使用方便。现在这个程序还有一个缺点,那就是第读取一页的数据,都要打开一次数据库,这个问题是可以解决的,那就是要求在不使用之用执行一个Close,关闭数据库,这是以牺牲程序的安全性为代价的。各个方面需要权衡利弊。 中国网管论坛bbs.bitsCN.com

     * 使用说明:
     * 首先创建一个MyDataPage类实例mypage,然后设置数据库连接串、查询表、查询列、查询条件、
     * 排序条件等。然后执行mypage.DoPaging(),注意检查其返回值,返回真表示分页成功,否则
     * 应查看mypage.ErrMessage属性。成功后,就可以使用mypage.GetData(<页编号>)读取数据了.
     *
     * 示例:
     *
     * using MyLibrary.DataAccess;
     * MyDataPage myPage = new MyDataPage(
     *   \"provider=sqloledb;server=(local);uid=sa;pwd=oohacker;database=Northwind\",
     *   \"Product\",
     *   \"ProductId,ProductName\",
     *   \"SupplierId<>1\",
     *   \"SupplierId ASC, ProductId DESC\", 网管下载dl.bitscn.com
     *   20);
     *
     * if (myPage.DoPaging())
     * {
     *   Console.Write(\"Total Records: {0}  Total Pages: {1} \",
     *     myPage.RecordCount,
     *     myPage.PageCount);
     *
     *   for (int i=1; i<=myPage.PageCount; ++i)
     *   {
     *     Console.Write(\"Page {0} \", i);
     *     DataTable table = myPage.GetData(i);
     *     for (int j=0; j<table.Rows.Count; ++j)
     *     {
     *       Console.Write(\"#{0}:{1} \",
     *         table.Rows[j][\"ProductId\"],

中国网管联盟bitsCN.com

     *         table.Rows[j][\"ProductName\"]);
     *     }
     *   }
     * }
     * else
     * {
     *   Console.Write(\"分页失败!原因:{0} \",  myPage.ErrMessage);
     * }
     * 网管u家u.bitsCN.com


    using System;
    using System.Text;
    using System.Data;
    using System.Data.OleDb;
    using System.Collections;

网管网www_bitscn_com

    namespace MyLibrary.DataAccess
    ...{
      public class MyDataPage
      ...{
    成员变量#region 成员变量
    const int defaltPageSize = 10;
    private int recordCount;
    private int pageCount;
    private int pageSize;
    private string table;
    private string columns;
    private string conditions;
    private string orders;
    private string connectionString;
    private string errorMessage;
    private bool isDirty;
    #endregion 中国网管论坛bbs.bitsCN.com

    构造函数#region 构造函数
    public MyDataPage(string _connectionString, string _table)
   
    ...{
      Init(_connectionString, _table, \"*\", \"\", \"\", defaltPageSize);
    } 网管网www.bitscn.com

    public MyDataPage(string _connectionString, string _table, int _pageSize)
    ...{
      Init(_connectionString, _table, \"*\", \"\", \"\", _pageSize);
    }

网管bitscn_com

    public MyDataPage(string _connectionString, string _table, string _columns, int _pageSize)
    ...{
      Init(_connectionString, _table, _columns, \"\", \"\", _pageSize);
    }

网管下载dl.bitscn.com

    public MyDataPage(string _connectionString, string _table, string _columns, string _conditions, int _pageSize)
    ...{
      Init(_connectionString, _table, _columns, _conditions, \"\", _pageSize);
    } 网管u家u.bitscn@com

    public MyDataPage(string _connectionString, 网管u家u.bitsCN.com

    string _table, string _columns, string _conditions, string _orders, int _pageSize)
    ...{
      Init(_connectionString, _table, _columns, _conditions, _orders, _pageSize);
    }

网管bitscn_com

    private void Init(string _connectionString,

网管u家u.bitscn@com

    string _table, string _columns, string _coditions, string _orders, int _pageSize)
    ...{
      this.recordCount = -1;
      this.pageCount = -1;
      this.PageSize = _pageSize;
      this.Table = _table;
      this.Columns = _columns;
      this.Conditions = _coditions;
      this.Orders = _orders;
      this.connectionString = _connectionString;
      this.isDirty = false;
    }
    #endregion

网管网www_bitscn_com

    // 获取和设置页面大小
    public int PageSize
    ...{
      set
      ...{
        pageSize = (value >=10 && value <= 1000) ? value : defaltPageSize;
        isDirty = true;
      }
      get
      ...{
        return pageSize;
      }
    }

网管网www.bitscn.com

    // 获取记录数
    public int RecordCount
    ...{
      get ...{ return recordCount; }
    }

网管网www_bitscn_com

    // 获取页面数
    public int PageCount
    ...{
      get ...{ return pageCount; }
    }

中国网管论坛bbs.bitsCN.com

    // 获取和设置表名
    public string Table
    ...{
      set ...{ this.table = value.Trim(); isDirty = true; }
      get ...{ return this.table; }
    } 中国网管论坛bbs.bitsCN.com


TAGs   访问   数据库       ...   string   //   return   public   int      
 上一篇:ADO.NET参数详解   下一篇:没有了
ADO.NET 数据库访问之数据分页 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: