不过在网上找了一些,发现都有一个特点——就是不能传出总记录数,干脆自己研究吧。终于,算是搞出来了,效率可能不是很好,但是我也觉得不错了。贴代码吧直接:也算是对自己学习mysql的一个记录。
复制代码 代码如下:
CREATE PROCEDURE p_pageList
(
m_pageNo int ,
m_perPageCnt int ,
m_column varchar(1000) ,
m_table varchar(1000) ,
m_condition varchar(1000),
m_orderBy varchar(200) ,
out m_totalPageCnt int
)
BEGIN
SET @pageCnt = 1; -- 总记录数
SET @limitStart = (m_pageNo - 1)*m_perPageCnt;
SET @limitEnd = m_perPageCnt;
SET @sqlCnt = CONCAT('select count(1) into @pageCnt from ',m_table); -- 这条语句很关键,用来得到总数值
SET @sql = CONCAT('select ',m_column,' from ',m_table);
IF m_condition IS NOT NULL AND m_condition <> '' THEN
SET @sql = CONCAT(@sql,' where ',m_condition);
SET @sqlCnt = CONCAT(@sqlCnt,' where ',m_condition);
END IF;
IF m_orderBy IS NOT NULL AND m_orderBy <> '' THEN
SET @sql = CONCAT(@sql,' order by ',m_orderBy);
END IF;
SET @sql = CONCAT(@sql, ' limit ', @limitStart, ',', @limitEnd);
PREPARE s_cnt from @sqlCnt;
EXECUTE s_cnt;
DEALLOCATE PREPARE s_cnt;
SET m_totalPageCnt = @pageCnt;
PREPARE record from @sql;
EXECUTE record;
DEALLOCATE PREPARE record;
END
asp.net
复制代码 代码如下:
/// <summary>
/// 分页显示
/// </summary>
/// <param name="conn">连接数据库字符串</param>
/// <param name="perPage">每页显示条数</param>
/// <param name="columnList">查询的字段字符</param>
/// <param name="tableName">查询的表名</param>
/// <param name="condition">where条件(不用写where)</param>
/// <param name="orderStr">排序条件(不用写order by)</param>
/// <param name="pageInfo">返回页码的数组,0,1,2,3分别为 总记录集数,总页数,上一页,下一页,4开始为页码</param>
/// <returns>此页的数据记录集</returns>
public static DataTable PageList(string conn, int perPage, string url, string columnList, string tableName, string condition, string orderStr, out string[] pageInfo)
{
int pageNo = 1; //当前页码
int totalCnt = 1; //记录集总数
int pageCnt = 0; //总页数
DataTable dt = new DataTable(); //用于返回的DataTable
using (MySqlConnection myConn = new MySqlConnection(conn))
{
MySqlDataAdapter adp = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand();
if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request["pageNo"]))
{
try
{
pageNo = int.Parse(System.Web.HttpContext.Current.Request["pageNo"].ToString());
}
finally
{
}
}//得到当前页面值
cmd.Connection = myConn;
myConn.Open();
cmd.CommandText = "p_pageList";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("m_pageNo", MySqlDbType.Int32, 11);
cmd.Parameters.Add("m_perPageCnt", MySqlDbType.Int32, 11);
cmd.Parameters.Add("m_column", MySqlDbType.VarChar, 1000);
cmd.Parameters.Add("m_table", MySqlDbType.VarChar, 1000);
cmd.Parameters.Add("m_condition", MySqlDbType.VarChar, 1000);
cmd.Parameters.Add("m_orderBy", MySqlDbType.VarChar, 200);
cmd.Parameters.Add("m_totalCnt", MySqlDbType.Int32, 11);
cmd.Parameters["m_pageNo"].Value = pageNo;
cmd.Parameters["m_perPageCnt"].Value = perPage;
cmd.Parameters["m_column"].Value = columnList;
cmd.Parameters["m_table"].Value = tableName;
cmd.Parameters["m_condition"].Value = condition;
cmd.Parameters["m_orderBy"].Value = orderStr;
cmd.Parameters["m_totalCnt"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
adp.SelectCommand = cmd;
adp.Fill(dt);
totalCnt = int.Parse(cmd.Parameters["m_totalCnt"].Value.ToString());
}
pageCnt = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalCnt) / perPage));
if (pageNo > pageCnt)
{
pageNo = pageCnt;
}//如果当前页码大于总页数,则当前页为最后一页
//处理页码地址参数
string pageNumLink = string.Empty; //页码的链接地址
if (string.IsNullOrEmpty(url.Trim()))
{
pageNumLink = "<a href=\"?pageNo=";
}
else if (url.IndexOf('?') >= 0)
{
pageNumLink = "<a href=\"" + url + "&pageNo=";
}
else
{
pageNumLink = "<a href=\"" + url + "?pageNo=";
}
//上一页,下一页字符串赋值
string abovePage = pageNumLink + (pageNo-1).ToString()+ "\">上一页</a>";
string nextPage = pageNumLink + (pageNo + 1).ToString() + "\">下一页</a>";
if (pageNo == 1)
{
abovePage = string.Empty;
}//如果当前页为第一页,则不显示“上一页”字符串
if (pageNo == pageCnt)
{
nextPage = string.Empty;
}//如果当前页为最后一页,则不显示“下一页”字符串
pageInfo = new string[14];
pageInfo[0] = totalCnt.ToString(); //显示记录集总数
pageInfo[1] = pageCnt.ToString(); //显示总页数
pageInfo[2] = abovePage; //显示上一页
pageInfo[3] = nextPage; //显示下一页
int startIndex;
int endIndex;
startIndex = (pageNo / 10) * 10 + 1; //起始页
if (pageNo % 10 == 0)
{
startIndex = startIndex - 10;
}
endIndex = startIndex + 9; //结束页
if (endIndex > pageCnt)
{
endIndex = pageCnt;
}//如果结束页大于总页数,则结束页为最后一页值
int pageIndex = 4;
for (int i = startIndex; i <= endIndex; i++)
{
string pageValue = pageNumLink + i.ToString() + "\">" + i.ToString() + "</a>";
if (i == pageNo)
{
pageValue = i.ToString();
}
pageInfo[pageIndex] = pageValue;
pageIndex++;
}
return dt;
}
复制代码 代码如下:
CREATE PROCEDURE p_pageList
(
m_pageNo int ,
m_perPageCnt int ,
m_column varchar(1000) ,
m_table varchar(1000) ,
m_condition varchar(1000),
m_orderBy varchar(200) ,
out m_totalPageCnt int
)
BEGIN
SET @pageCnt = 1; -- 总记录数
SET @limitStart = (m_pageNo - 1)*m_perPageCnt;
SET @limitEnd = m_perPageCnt;
SET @sqlCnt = CONCAT('select count(1) into @pageCnt from ',m_table); -- 这条语句很关键,用来得到总数值
SET @sql = CONCAT('select ',m_column,' from ',m_table);
IF m_condition IS NOT NULL AND m_condition <> '' THEN
SET @sql = CONCAT(@sql,' where ',m_condition);
SET @sqlCnt = CONCAT(@sqlCnt,' where ',m_condition);
END IF;
IF m_orderBy IS NOT NULL AND m_orderBy <> '' THEN
SET @sql = CONCAT(@sql,' order by ',m_orderBy);
END IF;
SET @sql = CONCAT(@sql, ' limit ', @limitStart, ',', @limitEnd);
PREPARE s_cnt from @sqlCnt;
EXECUTE s_cnt;
DEALLOCATE PREPARE s_cnt;
SET m_totalPageCnt = @pageCnt;
PREPARE record from @sql;
EXECUTE record;
DEALLOCATE PREPARE record;
END
asp.net
复制代码 代码如下:
/// <summary>
/// 分页显示
/// </summary>
/// <param name="conn">连接数据库字符串</param>
/// <param name="perPage">每页显示条数</param>
/// <param name="columnList">查询的字段字符</param>
/// <param name="tableName">查询的表名</param>
/// <param name="condition">where条件(不用写where)</param>
/// <param name="orderStr">排序条件(不用写order by)</param>
/// <param name="pageInfo">返回页码的数组,0,1,2,3分别为 总记录集数,总页数,上一页,下一页,4开始为页码</param>
/// <returns>此页的数据记录集</returns>
public static DataTable PageList(string conn, int perPage, string url, string columnList, string tableName, string condition, string orderStr, out string[] pageInfo)
{
int pageNo = 1; //当前页码
int totalCnt = 1; //记录集总数
int pageCnt = 0; //总页数
DataTable dt = new DataTable(); //用于返回的DataTable
using (MySqlConnection myConn = new MySqlConnection(conn))
{
MySqlDataAdapter adp = new MySqlDataAdapter();
MySqlCommand cmd = new MySqlCommand();
if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request["pageNo"]))
{
try
{
pageNo = int.Parse(System.Web.HttpContext.Current.Request["pageNo"].ToString());
}
finally
{
}
}//得到当前页面值
cmd.Connection = myConn;
myConn.Open();
cmd.CommandText = "p_pageList";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("m_pageNo", MySqlDbType.Int32, 11);
cmd.Parameters.Add("m_perPageCnt", MySqlDbType.Int32, 11);
cmd.Parameters.Add("m_column", MySqlDbType.VarChar, 1000);
cmd.Parameters.Add("m_table", MySqlDbType.VarChar, 1000);
cmd.Parameters.Add("m_condition", MySqlDbType.VarChar, 1000);
cmd.Parameters.Add("m_orderBy", MySqlDbType.VarChar, 200);
cmd.Parameters.Add("m_totalCnt", MySqlDbType.Int32, 11);
cmd.Parameters["m_pageNo"].Value = pageNo;
cmd.Parameters["m_perPageCnt"].Value = perPage;
cmd.Parameters["m_column"].Value = columnList;
cmd.Parameters["m_table"].Value = tableName;
cmd.Parameters["m_condition"].Value = condition;
cmd.Parameters["m_orderBy"].Value = orderStr;
cmd.Parameters["m_totalCnt"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
adp.SelectCommand = cmd;
adp.Fill(dt);
totalCnt = int.Parse(cmd.Parameters["m_totalCnt"].Value.ToString());
}
pageCnt = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalCnt) / perPage));
if (pageNo > pageCnt)
{
pageNo = pageCnt;
}//如果当前页码大于总页数,则当前页为最后一页
//处理页码地址参数
string pageNumLink = string.Empty; //页码的链接地址
if (string.IsNullOrEmpty(url.Trim()))
{
pageNumLink = "<a href=\"?pageNo=";
}
else if (url.IndexOf('?') >= 0)
{
pageNumLink = "<a href=\"" + url + "&pageNo=";
}
else
{
pageNumLink = "<a href=\"" + url + "?pageNo=";
}
//上一页,下一页字符串赋值
string abovePage = pageNumLink + (pageNo-1).ToString()+ "\">上一页</a>";
string nextPage = pageNumLink + (pageNo + 1).ToString() + "\">下一页</a>";
if (pageNo == 1)
{
abovePage = string.Empty;
}//如果当前页为第一页,则不显示“上一页”字符串
if (pageNo == pageCnt)
{
nextPage = string.Empty;
}//如果当前页为最后一页,则不显示“下一页”字符串
pageInfo = new string[14];
pageInfo[0] = totalCnt.ToString(); //显示记录集总数
pageInfo[1] = pageCnt.ToString(); //显示总页数
pageInfo[2] = abovePage; //显示上一页
pageInfo[3] = nextPage; //显示下一页
int startIndex;
int endIndex;
startIndex = (pageNo / 10) * 10 + 1; //起始页
if (pageNo % 10 == 0)
{
startIndex = startIndex - 10;
}
endIndex = startIndex + 9; //结束页
if (endIndex > pageCnt)
{
endIndex = pageCnt;
}//如果结束页大于总页数,则结束页为最后一页值
int pageIndex = 4;
for (int i = startIndex; i <= endIndex; i++)
{
string pageValue = pageNumLink + i.ToString() + "\">" + i.ToString() + "</a>";
if (i == pageNo)
{
pageValue = i.ToString();
}
pageInfo[pageIndex] = pageValue;
pageIndex++;
}
return dt;
}
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]