All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
最简单快速的Apache二级域名实现方法介绍_[PHP教程]
JS特效:用鼠标事件实现图片的渐显效果_JavaScript技术_编程技术
SQL2000系统表的应用_[SQL,Server教程]
ASP.NET中设置默认提交按钮的代码_[Asp.Net教程]
Visual C++ 6.0中的向导类型
详细介绍:Apache+PHP+MySQL配置攻略_[PHP教程]
《Effective,C#》:区别四个判等函数_[Asp.Net教程]
如何用正确的方法写出高质量软件的75条体会_[Asp.Net教程]
GridView,实现服务器端和客户端全选的两种方法_[Asp.Net教程]
HOW,TO—操作定长字符串_[Asp.Net教程]
Reading number is top 10 pictures
[猫扑大杂烩]华东师范墙上看到的捐精告示 15毫升3600元
The world first motorcycle will be auctioned for 21000 pounds
The woman fight much more than men
Sanya, hainan Haitian party feast promiscuity
奇趣的世界记录2
鸡也看毛片
各种囧况!玩游戏最不喜欢出现的十件事(点评)
有种屌丝级别的好妹子
教你做读书笔记
The money of more than 100 countries and regions10
Download software ranking
Call Of Duty5
Proficient in Eclipse
WebService在.NET中的实战应用教学视频 → 第5集
linux初级教程
Tram sex maniac 2 (H) rar bag11
Eclipse 4.2.2 For Win64
尖东毒玫瑰B
Unix video tutorial3
XML+Web+Service开发教程
Jinling thirteen stock
delv published in(发表于) 2014/1/6 9:03:52 Edit(编辑)
ASP.NET技巧:access下的分页方案_[Asp.Net教程]

ASP.NET技巧:access下的分页方案_[Asp.Net教程]

ASP.NET技巧:access下的分页方案_[Asp.Net教程]

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


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 < 1)
{
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 < 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" %>





分页演示







&nbsp; 转到第1onClick="btnJump_Click" />

















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


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.