asp.net 结合mysql存储过程进行分页代码

时间:2021-05-28

不过在网上找了一些,发现都有一个特点——就是不能传出总记录数,干脆自己研究吧。终于,算是搞出来了,效率可能不是很好,但是我也觉得不错了。贴代码吧直接:也算是对自己学习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;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章