|
复制代码 代码如下:
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;
}
AspNet技术:asp.net 结合mysql存储过程进行分页代码,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。