首頁 資料庫 mysql教程 access下的分页方案(仿sql存储过程)

access下的分页方案(仿sql存储过程)

Jun 07, 2016 pm 03:43 PM
access s sql using 分頁 儲存 方案 過程

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using System.Web; /**//// summary /// 名称:access下的分页方案(仿sql存储过程) /// 作者:cncxz(虫虫) /// blog: http://cncxz.cn

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

/**////


/// 名称:access下的分页方案(仿sql存储过程)
/// 作者:cncxz(虫虫)
/// blog:
http://cncxz.cnblogs.com
///
public class AdoPager
{
    protected string m_ConnString;
    protected OleDbConnection m_Conn;

    public AdoPager()
    {
        CreateConn(string.Empty);
    }
    public AdoPager(string dbPath)
    {
        CreateConn(dbPath);
    }

    private void CreateConn(string dbPath)
    {
        if (string.IsNullOrEmpty(dbPath))
        {
            string str = System.Configuration.ConfigurationManager.AppSettings["dbPath"] as string;
            if (string.IsNullOrEmpty(str))
                str = "~/App_Data/db.mdb";
            m_ConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}", HttpContext.Current.Server.MapPath(str));
        }
        else
            m_ConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}", dbPath);

        m_Conn = new OleDbConnection(m_ConnString);
    }
    /**////


    /// 打开连接
    ///

    public void ConnOpen()
    {
        if (m_Conn.State != ConnectionState.Open)
            m_Conn.Open();
    }
    /**////
    /// 关闭连接
    ///

    public void ConnClose()
    {
        if (m_Conn.State != ConnectionState.Closed)
            m_Conn.Close();
    }

    private string recordID(string query, int passCount)
    {
        OleDbCommand cmd = new OleDbCommand(query, m_Conn);
        string result = string.Empty;
        using (IDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                if (passCount                 {
                    result += "," + dr.GetInt32(0);
                }
                passCount--;
            }
        }
        return result.Substring(1);
    }


    /**////


    /// 获取当前页应该显示的记录,注意:查询中必须包含名为ID的自动编号列,若不符合你的要求,就修改一下源码吧 :)
    ///

    /// 当前页码
    /// 分页容量
    /// 显示的字段
    /// 查询字符串,支持联合查询
    /// 查询条件,若有条件限制则必须以where 开头
    /// 排序规则
    /// 传出参数:总页数统计
    /// 传出参数:总记录统计
    /// 装载记录的DataTable
    public DataTable ExecutePager(int pageIndex, int pageSize, string showString, string queryString, string whereString, string orderString, out int pageCount, out int recordCount)
    {
        if (pageIndex         if (pageSize         if (string.IsNullOrEmpty(showString)) showString = "*";
        if (string.IsNullOrEmpty(orderString)) orderString = "ID desc";
        ConnOpen();
        string myVw = string.Format(" ( {0} ) tempVw ", queryString);
        OleDbCommand cmdCount = new OleDbCommand(string.Format(" select count(0) as recordCount from {0} {1}", myVw, whereString), m_Conn);

        recordCount = Convert.ToInt32(cmdCount.ExecuteScalar());

        if ((recordCount % pageSize) > 0)
            pageCount = recordCount / pageSize + 1;
        else
            pageCount = recordCount / pageSize;
        OleDbCommand cmdRecord;
        if (pageIndex == 1)//第一页
        {
            cmdRecord = new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, whereString, orderString), m_Conn);
        }
        else if (pageIndex > pageCount)//超出总页数
        {
            cmdRecord = new OleDbCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, "where 1=2", orderString), m_Conn);
        }
        else
        {
            int pageLowerBound = pageSize * pageIndex;
            int pageUpperBound = pageLowerBound - pageSize;
            string recordIDs = recordID(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageLowerBound, "ID", myVw, whereString, orderString), pageUpperBound);
            cmdRecord = new OleDbCommand(string.Format("select {0} from {1} where id in ({2}) order by {3} ", showString, myVw, recordIDs, orderString), m_Conn);

        }
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(cmdRecord);
        DataTable dt=new DataTable();
        dataAdapter.Fill(dt);
        ConnClose();
        return dt;
    }
}

还有调用示例:html代码



    分页演示


   


   

       

          转到第1

       
           
           
           
           
           
           
           
       

   
   

       
   

示例的codebehind代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    private AdoPager mm_Pager;
    protected AdoPager m_Pager
    {
        get{
            if (mm_Pager == null)
                mm_Pager = new AdoPager();
            return mm_Pager;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
            LoadData();
    }
    private int pageIndex = 1;
    private int pageSize = 20;
    private int pageCount = -1;
    private int recordCount = -1;

    private void LoadData()
    {
        string strQuery = "select a.*,b.KindText from tableTest a left join tableKind b on a.KindCode=b.KindCode ";
        string strShow = "ID,Subject,KindCode,KindText";    
       
        DataTable dt = m_Pager.ExecutePager(pageIndex, pageSize, strShow, strQuery, "", "ID desc", out pageCount, out recordCount);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        Label1.Text = string.Format("共{0}条记录,每页{1}条,页次{2}/{3}",recordCount,pageSize,pageIndex,pageCount);
    }
   
  
    protected void btnJump_Click(object sender, EventArgs e)
    {
        int.TryParse(txtPageSize.Text, out pageIndex);
        LoadData();
    }
}

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
apache怎麼配置zend apache怎麼配置zend Apr 13, 2025 pm 12:57 PM

如何在 Apache 中配置 Zend?在 Apache Web 服務器中配置 Zend Framework 的步驟如下:安裝 Zend Framework 並解壓到 Web 服務器目錄中。創建 .htaccess 文件。創建 Zend 應用程序目錄並添加 index.php 文件。配置 Zend 應用程序(application.ini)。重新啟動 Apache Web 服務器。

使用DICR/YII2-Google將Google API集成在YII2中 使用DICR/YII2-Google將Google API集成在YII2中 Apr 18, 2025 am 11:54 AM

vProcesserazrabotkiveb被固定,мнелостольностьстьс粹餾標д都LeavallySumballanceFriablanceFaumDoptoMatification,Čtookazalovnetakprosto,kakaožidal.posenesko

apache服務器是什麼 apache服務器是乾嘛的 apache服務器是什麼 apache服務器是乾嘛的 Apr 13, 2025 am 11:57 AM

Apache服務器是強大的Web服務器軟件,充當瀏覽器與網站服務器間的橋樑。 1. 它處理HTTP請求,根據請求返回網頁內容;2. 模塊化設計允許擴展功能,例如支持SSL加密和動態網頁;3. 配置文件(如虛擬主機配置)需謹慎設置,避免安全漏洞,並需優化性能參數,例如線程數和超時時間,才能構建高性能、安全的Web應用。

nginx限流怎麼解決 nginx限流怎麼解決 Apr 14, 2025 pm 12:06 PM

Nginx 限流問題可通過以下方法解決:使用 ngx_http_limit_req_module 限制請求次數;使用 ngx_http_limit_conn_module 限制連接數;使用第三方模塊(ngx_http_limit_connections_module、ngx_http_limit_rate_module、ngx_http_access_module)實現更多限流策略;使用雲服務(Cloudflare、Google Cloud Rate Limiting、AWS WAF)進行 DD

解決Magento項目中的內存管理問題:zend-memory庫的應用 解決Magento項目中的內存管理問題:zend-memory庫的應用 Apr 17, 2025 pm 11:03 PM

在處理一個Magento項目時,我遇到了一個棘手的內存管理問題。由於項目中涉及大量數據處理,內存消耗迅速增加,導致系統性能下降甚至崩潰。經過一番研究,我發現了zend-memory庫,它有效地解決了我的內存管理問題。

Nginx性能監控與故障排查工具使用 Nginx性能監控與故障排查工具使用 Apr 13, 2025 pm 10:00 PM

Nginx性能監控與故障排查主要通過以下步驟進行:1.使用nginx-V查看版本信息,並啟用stub_status模塊監控活躍連接數、請求數和緩存命中率;2.利用top命令監控系統資源佔用,iostat和vmstat分別監控磁盤I/O和內存使用情況;3.使用tcpdump抓包分析網絡流量,排查網絡連接問題;4.合理配置worker進程數,避免並發處理能力不足或進程上下文切換開銷過大;5.正確配置Nginx緩存,避免緩存大小設置不當;6.通過分析Nginx日誌,例如使用awk和grep命令或ELK

Nginx日誌分析與統計,了解網站訪問情況 Nginx日誌分析與統計,了解網站訪問情況 Apr 13, 2025 pm 10:06 PM

本文介紹瞭如何分析Nginx日誌以提升網站性能和用戶體驗。 1.理解Nginx日誌格式,例如時間戳、IP地址、狀態碼等;2.使用awk等工具解析日誌,統計訪問量、錯誤率等指標;3.根據需求編寫更複雜的腳本或使用更高級工具,例如goaccess,分析不同維度的數據;4.對於海量日誌,考慮使用Hadoop或Spark等分佈式框架。通過分析日誌,可以識別網站訪問模式、改進內容策略,並最終優化網站性能和用戶體驗。

SQL的目的:與MySQL數據庫進行交互 SQL的目的:與MySQL數據庫進行交互 Apr 18, 2025 am 12:12 AM

SQL用於與MySQL數據庫交互,實現數據的增、刪、改、查及數據庫設計。 1)SQL通過SELECT、INSERT、UPDATE、DELETE語句進行數據操作;2)使用CREATE、ALTER、DROP語句進行數據庫設計和管理;3)複雜查詢和數據分析通過SQL實現,提升業務決策效率。

See all articles