问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

asp.net里利用aspnetpager连接access来实现分页的sql语句怎么写?

发布网友 发布时间:2022-05-19 18:49

我来回答

2个回答

懂视网 时间:2022-05-19 23:10

具体不多说了,只贴出相关源码~

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.cnblogs.com
/// </summary>
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);
    }
    /**//// <summary>
    /// 打开连接
    /// </summary>
    public void ConnOpen()
    {
        if (m_Conn.State != ConnectionState.Open)
            m_Conn.Open();
    }
    /**//// <summary>
    /// 关闭连接
    /// </summary>
    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 < 1)
                {
                    result += "," + dr.GetInt32(0);
                }
                passCount--;
            }
        }
        return result.Substring(1);
    }


    /**//// <summary>
    /// 获取当前页应该显示的记录,注意:查询中必须包含名为ID的自动编号列,若不符合你的要求,就修改一下源码吧 :)
    /// </summary>
    /// <param name="pageIndex">当前页码</param>
    /// <param name="pageSize">分页容量</param>
    /// <param name="showString">显示的字段</param>
    /// <param name="queryString">查询字符串,支持联合查询</param>
    /// <param name="whereString">查询条件,若有条件限制则必须以where 开头</param>
    /// <param name="orderString">排序规则</param>
    /// <param name="pageCount">传出参数:总页数统计</param>
    /// <param name="recordCount">传出参数:总记录统计</param>
    /// <returns>装载记录的DataTable</returns>
    public DataTable ExecutePager(int pageIndex, int pageSize, string showString, string queryString, string whereString, string orderString, out int pageCount, out int recordCount)
    {
        if (pageIndex < 1) pageIndex = 1;
        if (pageSize < 1) pageSize = 10;
        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代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>分页演示</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
          转到第<asp:TextBox ID="txtPageSize" runat="server" Width="29px">1</asp:TextBox>页<asp:Button ID="btnJump" runat="server" Text="Go" OnClick="btnJump_Click" /><br />
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="90%">
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <EditRowStyle BackColor="#2461BF" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

    </div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
</body>
</html>

示例的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();
    }
}

 

热心网友 时间:2022-05-19 20:18

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HouseConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select count(guid) from hire",conn);
conn.Open(); ///获取总记录数
int num = Convert.ToInt32(cmd.ExecuteScalar()); ///将总记录数赋值给AspNetPager1对象
AspNetPager1.RecordCount = num; message.Text = "总记录条数:" + num.ToString();
DepeaterDataBind();
conn.Close();
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
抖音直播怎么让画质清晰 php 有了 &amp; || 为什么还需要 and 和 or ...的明星,登台五分钟让其下台,爆红后再没来过 ...不给,爆红后再没去过快本,除了薛之谦还有谁? 喝熟普洱茶的好处和坏处:专家解答 设身处地的意思是什么意思设身处地的意思 QQ怎么举报群聊_QQ举报群聊方法 合同订立的主要形式有什么方面 合同订立的形式是如何规定的 合同订立有几种形式 大专和本科入伍有什么区别?大专能和本科享受同等待遇吗? 大学本科去当兵和高中毕业后去当兵有什么区别? 大专在校生当兵和毕业生有什么区别 高中毕业当兵和在大专里面当兵有什么区别 专科生入伍与本科生入伍区别大吗 当兵跟本科和专科有区别吗? 专科和本科当兵的区别 大专去当兵和本科去当兵有什么区别 大学期间,专科入伍和本科入伍有何不同? 大专当兵跟本科当兵有区别吗 本科当兵和专科当兵区别? 本科当兵和专科当兵区别是什么? 专科和本科当兵的区别是什么 专科当兵和本科当兵有什么区别 专科入伍和本科入伍有什么区别? 专科生当兵与本科生当兵有什么区别? 本科当兵和专科当兵区别 西安哪的老碗鱼好吃又实惠? 西安 哪里的老碗鱼做的好吃啊 老碗鱼怎么在家里做? 上海首套房公积金贷款首付比例是什么 导出文件时如何过滤掉decimal型字段中前面的0 去掉decimal类型后面的小数点 datastage中如何去掉decimal类型小数点前面的0 informix如何去掉decimal类型后的零 C#中decimal类型如何去掉后面的位数 如何在Java程序的sql中去掉小数点后多余的0 java.math.BigDecimal 去掉多余的(0) C#中函数怎样去掉末尾的0和小数点 C语言 实数的小数部分(指针专题) 按题目要求来 C语言 实数的小数部分 招标代理从业人员培训什么? 有朋友参加过上海市*培训中心举办的全国招标师职业考试培训么?每门450元 教学、老师怎么样? 2009年广西招标师考试培训如何可以查到? 2012年四川省招标师培训班怎么找到准确信息? 2013广西招标师培训课程哪里可以查到? 2011年辽宁省招标师培训课程谁有?给我说一下 2010年贵州省招标师培训课程谁知道啊?给我说一下,谢谢了! 辛特兰怎么跑 四川省工程建设项目招标从业资格报名