access下的分页方案
来源:网络整理 责任编辑:栏目编辑 发表时间:2013-07-01 23:53 点击:次
具体不多说了,只贴出相关源码~
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 (pageInd
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 (pageInd
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>