淺述asp.net海量分頁資料儲存過程

monicamh發表於2011-05-20
是Web應用程式開發中經常使用的一種技術,也是提高Web資料訪問效能的主要手段。本文結合ASP.NET,詳細給出了兩種ASP.NET海量分頁資料儲存的技術。

  儲存過程1:

#div_code img{border:0px;}<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtCREATE 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/

--&gtCREATE 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

相關文章