SQL Server 百萬級數據分頁存儲過程 分頁存儲過程

SQL Server  百萬級數據分頁存儲過程  分頁

CREATE PROC [dbo].[Common_PageList]
(
@tab nvarchar(max),---數據表名
@strFld nvarchar(max), --要顯示的字段
@strWhere varchar(max), --where條件 
@PageIndex int, --當頁頁碼
@PageSize int, --頁碼尺寸
@Sort VARCHAR(255), --排序字段及規則,不用加order by
@IsGetCount bit  --是否得到記錄總數,1爲得到記錄總數,0爲不得到記錄總數,返回記錄集
)
AS
declare @strSql nvarchar(max)
set nocount on;
if(@IsGetCount = 1)
begin
 set @strSql='SELECT COUNT(0) FROM ' + @tab + ' WHERE ' + @strWhere
end
else
begin
  set @strSql=' SELECT * FROM (SELECT ROW_NUMBER() 
  OVER(ORDER BY ' + @Sort + ') AS rownum, ' + @strFld + ' FROM ' + @tab + ' where ' + @strWhere + ') AS Dwhere
  WHERE rownum BETWEEN ' + CAST(((@PageIndex-1)*@PageSize + 1) as nvarchar(20)) + ' and ' + cast((@PageIndex*@PageSize) as nvarchar(20))
end

exec (@strSql)

set nocount off;

select * from  [dbo].[Teacher]

存儲過程

調用方法; 執行方法;

exec  Common_PageList  'Teacher','*','',1,10,null,0

C#調用方法;調用方式; 

  /// <summary>
        /// 讀取存貯過程  
        /// </summary>
        /// <param name="ProcName"></param>
        /// <param name="Pars"></param>
        /// <returns></returns>
        public static DataTable ExexProcQuery(string ProcName, SqlParameter[] Pars)
        {
            SqlConnection con = new SqlConnection(ConnectionString);
            SqlDataAdapter sda = new SqlDataAdapter(ProcName, con);
            sda.SelectCommand.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < Pars.Length; i++)
            {
                sda.SelectCommand.Parameters.Add(Pars[i]);
            }
            //這裏寫成
            DataTable dt = new DataTable();
            try
            {
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                //LogTool.WriteLog(ex.Message.ToString());
                return dt;
            }
            finally
            {
                con.Close();
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章