程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 結合mysql存儲過程進行分頁代碼

asp.net 結合mysql存儲過程進行分頁代碼

編輯:ASP.NET基礎
不過在網上找了一些,發現都有一個特點——就是不能傳出總記錄數,干脆自己研究吧。終於,算是搞出來了,效率可能不是很好,但是我也覺得不錯了。貼代碼吧直接:也算是對自己學習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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved