淺述asp.net海量分頁資料儲存過程
是Web應用程式開發中經常使用的一種技術,也是提高Web資料訪問效能的主要手段。本文結合ASP.NET,詳細給出了兩種ASP.NET海量分頁資料儲存的技術。
儲存過程1:
#div_code img{border:0px;}<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->CREATE PROCEDURE pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的欄位名
@PageSize int , -- 頁尺寸
@PageIndex int, -- 頁碼
@doCount bit , -- 返回記錄總數, 非 0 值則返回
@OrderType bit , -- 設定排序型別, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時變數
declare @strOrder varchar(400) -- 排序型別
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere
else
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
end
--以上程式碼的意思是如果@doCount傳遞過來的不是0,就執行總數統計。以下的所有程式碼都是@doCount為0的情況
else
begin
if @OrderType != 0
begin
set @strTmp = ""
set @strOrder = " order by [" + @fldName +"] desc"
--如果@OrderType不是0,就執行降序,這句很重要!
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @fldName +"] asc"
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder
else
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder
--如果是第一頁就執行以上程式碼,這樣會加快執行速度
end
else
begin
--以下程式碼賦予了@strSQL以真正執行的SQL程式碼
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder
end
end
exec (@strSQL)
GO
呼叫的程式(為了通用性,我寫了一個方法,大家可以提意見,也可以對其進行修改,多多交換意見,共同進步)
private static DataSet GetCustomersData(string tblName, string strGetFields,string fldName, int PageSize, int PageIndex, int doCount, int OrderType, string strWhere)
{
string connString = ConfigurationSettings.AppSettings["connstr"];
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("pagination3", conn);
comm.Parameters.Add(new SqlParameter("@tblName", SqlDbType.VarChar));//表名
comm.Parameters[0].Value =tblName ;
comm.Parameters.Add(new SqlParameter("@strGetFields", SqlDbType.VarChar));//返回的列
comm.Parameters[1].Value = strGetFields;
comm.Parameters.Add(new SqlParameter("@fldName", SqlDbType.VarChar));//排序的欄位名
comm.Parameters[2].Value = fldName;
comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));//頁尺寸
comm.Parameters[3].Value = PageSize;
comm.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));//頁碼
comm.Parameters[4].Value = PageIndex;
comm.Parameters.Add(new SqlParameter("@doCount", SqlDbType.Int));//是否返回記錄總數,0為不返回,1為返回
comm.Parameters[5].Value = doCount;
comm.Parameters.Add(new SqlParameter("@OrderType", SqlDbType.Int));//設定排序型別,0為升序,非0為降序
comm.Parameters[6].Value = OrderType;
comm.Parameters.Add(new SqlParameter("@strWhere", SqlDbType.VarChar));//where語句
comm.Parameters[7].Value = strWhere;
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->CREATE PROCEDURE pagination
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='', -- 排序的欄位名
@PageSize int , -- 頁尺寸
@PageIndex int, -- 頁碼
@doCount bit , -- 返回記錄總數, 非 0 值則返回
@OrderType bit , -- 設定排序型別, 非 0 值則降序
@strWhere varchar(1500) = '' -- 查詢條件 (注意: 不要加 where)
AS
declare @strSQL varchar(5000) -- 主語句
declare @strTmp varchar(110) -- 臨時變數
declare @strOrder varchar(400) -- 排序型別
if @doCount != 0
begin
if @strWhere !=''
set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere
else
set @strSQL = "select count(*) as Total from [" + @tblName + "]"
end
--以上程式碼的意思是如果@doCount傳遞過來的不是0,就執行總數統計。以下的所有程式碼都是@doCount為0的情況
else
begin
if @OrderType != 0
begin
set @strTmp = ""
set @strOrder = " order by [" + @fldName +"] desc"
--如果@OrderType不是0,就執行降序,這句很重要!
end
else
begin
set @strTmp = ">(select max"
set @strOrder = " order by [" + @fldName +"] asc"
end
if @PageIndex = 1
begin
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder
else
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder
--如果是第一頁就執行以上程式碼,這樣會加快執行速度
end
else
begin
--以下程式碼賦予了@strSQL以真正執行的SQL程式碼
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder
if @strWhere != ''
set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["
+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["
+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["
+ @fldName + "] from [" + @tblName + "] where " + @strWhere + " "
+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder
end
end
exec (@strSQL)
GO
呼叫的程式(為了通用性,我寫了一個方法,大家可以提意見,也可以對其進行修改,多多交換意見,共同進步)
private static DataSet GetCustomersData(string tblName, string strGetFields,string fldName, int PageSize, int PageIndex, int doCount, int OrderType, string strWhere)
{
string connString = ConfigurationSettings.AppSettings["connstr"];
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand("pagination3", conn);
comm.Parameters.Add(new SqlParameter("@tblName", SqlDbType.VarChar));//表名
comm.Parameters[0].Value =tblName ;
comm.Parameters.Add(new SqlParameter("@strGetFields", SqlDbType.VarChar));//返回的列
comm.Parameters[1].Value = strGetFields;
comm.Parameters.Add(new SqlParameter("@fldName", SqlDbType.VarChar));//排序的欄位名
comm.Parameters[2].Value = fldName;
comm.Parameters.Add(new SqlParameter("@PageSize",SqlDbType.Int));//頁尺寸
comm.Parameters[3].Value = PageSize;
comm.Parameters.Add(new SqlParameter("@PageIndex", SqlDbType.Int));//頁碼
comm.Parameters[4].Value = PageIndex;
comm.Parameters.Add(new SqlParameter("@doCount", SqlDbType.Int));//是否返回記錄總數,0為不返回,1為返回
comm.Parameters[5].Value = doCount;
comm.Parameters.Add(new SqlParameter("@OrderType", SqlDbType.Int));//設定排序型別,0為升序,非0為降序
comm.Parameters[6].Value = OrderType;
comm.Parameters.Add(new SqlParameter("@strWhere", SqlDbType.VarChar));//where語句
comm.Parameters[7].Value = strWhere;
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
return ds;
}
儲存過程:
#div_code img{border:0px;}<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->CREATE PROCEDURE [dbo].[GetRecordFromPage]
@SelectList VARCHAR(2000), --欲選擇欄位列表
@TableSource VARCHAR(100), --表名或檢視表
@SearchCondition VARCHAR(2000), --查詢條件
@OrderExpression VARCHAR(1000), --排序表示式
@PageIndex INT = 1, --頁號,從0開始
@PageSize INT = 10 --頁尺寸
AS
BEGIN
IF @SelectList IS NULL OR LTRIM(RTRIM(@SelectList)) = ''
BEGIN
SET @SelectList = '*'
END
PRINT @SelectList
SET @SearchCondition = ISNULL(@SearchCondition,'')
SET @SearchCondition = LTRIM(RTRIM(@SearchCondition))
IF @SearchCondition <> ''
BEGIN
IF UPPER(SUBSTRING(@SearchCondition,1,5)) <> 'WHERE'
BEGIN
SET @SearchCondition = 'WHERE ' + @SearchCondition
END
END
PRINT @SearchCondition
SET @OrderExpression = ISNULL(@OrderExpression,'')
SET @OrderExpression = LTRIM(RTRIM(@OrderExpression))
IF @OrderExpression <> ''
BEGIN
IF UPPER(SUBSTRING(@OrderExpression,1,5)) <> 'WHERE'
BEGIN
SET @OrderExpression = 'ORDER BY ' + @OrderExpression
END
END
PRINT @OrderExpression
IF @PageIndex IS NULL OR @PageIndex < 1
BEGIN
SET @PageIndex = 1
END
PRINT @PageIndex
IF @PageSize IS NULL OR @PageSize < 1
BEGIN
SET @PageSize = 10
END
PRINT @PageSize
DECLARE @SqlQuery VARCHAR(4000)
SET @SqlQuery='SELECT '+@SelectList+',RowNumber
FROM
(SELECT ' + @SelectList + ',ROW_NUMBER() OVER( '+ @OrderExpression +') AS RowNumber
FROM '+@TableSource+' '+ @SearchCondition +') AS RowNumberTableSource
WHERE RowNumber BETWEEN ' + CAST(((@PageIndex - 1)* @PageSize+1) AS VARCHAR)
+ ' AND ' +
CAST((@PageIndex * @PageSize) AS VARCHAR)
-- ORDER BY ' + @OrderExpression
PRINT @SqlQuery
SET NOCOUNT ON
EXECUTE(@SqlQuery)
SET NOCOUNT OFF
RETURN @@RowCount
END
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->CREATE PROCEDURE [dbo].[GetRecordFromPage]
@SelectList VARCHAR(2000), --欲選擇欄位列表
@TableSource VARCHAR(100), --表名或檢視表
@SearchCondition VARCHAR(2000), --查詢條件
@OrderExpression VARCHAR(1000), --排序表示式
@PageIndex INT = 1, --頁號,從0開始
@PageSize INT = 10 --頁尺寸
AS
BEGIN
IF @SelectList IS NULL OR LTRIM(RTRIM(@SelectList)) = ''
BEGIN
SET @SelectList = '*'
END
PRINT @SelectList
SET @SearchCondition = ISNULL(@SearchCondition,'')
SET @SearchCondition = LTRIM(RTRIM(@SearchCondition))
IF @SearchCondition <> ''
BEGIN
IF UPPER(SUBSTRING(@SearchCondition,1,5)) <> 'WHERE'
BEGIN
SET @SearchCondition = 'WHERE ' + @SearchCondition
END
END
PRINT @SearchCondition
SET @OrderExpression = ISNULL(@OrderExpression,'')
SET @OrderExpression = LTRIM(RTRIM(@OrderExpression))
IF @OrderExpression <> ''
BEGIN
IF UPPER(SUBSTRING(@OrderExpression,1,5)) <> 'WHERE'
BEGIN
SET @OrderExpression = 'ORDER BY ' + @OrderExpression
END
END
PRINT @OrderExpression
IF @PageIndex IS NULL OR @PageIndex < 1
BEGIN
SET @PageIndex = 1
END
PRINT @PageIndex
IF @PageSize IS NULL OR @PageSize < 1
BEGIN
SET @PageSize = 10
END
PRINT @PageSize
DECLARE @SqlQuery VARCHAR(4000)
SET @SqlQuery='SELECT '+@SelectList+',RowNumber
FROM
(SELECT ' + @SelectList + ',ROW_NUMBER() OVER( '+ @OrderExpression +') AS RowNumber
FROM '+@TableSource+' '+ @SearchCondition +') AS RowNumberTableSource
WHERE RowNumber BETWEEN ' + CAST(((@PageIndex - 1)* @PageSize+1) AS VARCHAR)
+ ' AND ' +
CAST((@PageIndex * @PageSize) AS VARCHAR)
-- ORDER BY ' + @OrderExpression
PRINT @SqlQuery
SET NOCOUNT ON
EXECUTE(@SqlQuery)
SET NOCOUNT OFF
RETURN @@RowCount
END
相關文章
- [MSSQL]mssql海量高效分頁儲存過程SQL儲存過程
- 實現小資料量和海量資料的通用分頁顯示儲存過程儲存過程
- 分頁儲存過程儲存過程
- asp.net利用儲存過程分頁程式碼ASP.NET儲存過程
- SQL 分頁儲存過程SQL儲存過程
- 通用分頁儲存過程儲存過程
- sql儲存過程分頁SQL儲存過程
- SqlServer-儲存過程分頁SQLServer儲存過程
- asp.net分頁的SQL語句及儲存過程ASP.NETSQL儲存過程
- Sql儲存過程分頁--臨時表儲存SQL儲存過程
- asp.net SQL Server 儲存過程分頁及程式碼呼叫ASP.NETSQLServer儲存過程
- oracle儲存過程分頁程式碼Oracle儲存過程
- (SQL Server)分頁的儲存過程SQLServer儲存過程
- 簡單的儲存過程分頁儲存過程
- 簡單的分頁儲存過程儲存過程
- Oracle 儲存過程分頁 + Sqlsugar呼叫Oracle儲存過程SqlSugar
- 兩種SQL分頁方法儲存過程和遊標儲存過程SQL儲存過程
- 淺談資料庫中的儲存過程資料庫儲存過程
- 通用的SQL Server資料庫查詢分頁儲存過程SQLServer資料庫儲存過程
- sqlserver儲存過程實現多表分頁SQLServer儲存過程
- 分頁控制元件及儲存過程控制元件儲存過程
- ORACLE高效分頁儲存過程程式碼Oracle儲存過程
- 基於ROWCOUNT的分頁儲存過程儲存過程
- asp.net分頁控制元件AspNetPager的使用,使用傳統分頁和儲存過程分頁ASP.NET控制元件儲存過程
- 使用儲存過程實現分頁列印 (轉)儲存過程
- SQLServer 2005通用分頁儲存過程SQLServer儲存過程
- 拋磚引玉——通用分頁儲存過程儲存過程
- SQL SERVER 2005分頁儲存過程SQLServer儲存過程
- 資料庫儲存過程資料庫儲存過程
- MyBatis(八) 資料庫BLOB讀寫、批量更新操作、儲存過程呼叫、分表、分頁MyBatis資料庫儲存過程
- 儲存過程分頁 Ado.Net分頁 EF分頁 滿足90%以上儲存過程
- SQL Server 儲存過程的經典分頁(轉)SQLServer儲存過程
- MySql資料庫——儲存過程MySql資料庫儲存過程
- 儲存過程返回資料集儲存過程
- 完整的分頁儲存過程以及c#呼叫方法儲存過程C#
- 一個比較不錯的儲存過程分頁儲存過程
- [Procedure]Oracle之分頁儲存過程Oracle儲存過程
- 【資料庫】資料庫儲存過程(一)資料庫儲存過程