sql server 帶輸入輸出參數的分頁預存程序(效率最高)

來源:互聯網
上載者:User

標籤:style   blog   io   ar   color   sp   on   資料   div   

create procedure proc_page_withtopmax( @pageIndex int,--頁索引 @pageSize int,--每頁顯示數 @pageCount int output,--總頁數,輸出參數  @totalCount int output--總條數)asbeginset nocount on;declare @sql nvarchar(1000)set @sql=‘select top 10 *  from tb_testtable where (id> (select max(id) from (select top ‘+str((@pageIndex-1)*@pageSize)+‘ id from tb_testtable order by id) as temp)) order by id‘execute(@sql)declare @sqlRecordCount nvarchar(1000) --得到總記錄條數的語句set @sqlRecordCount=N‘select @recordCount=count(*) from tb_testtable‘ declare @recordCount int --儲存總記錄條數的變數exec sp_executesql @sqlRecordCount,N‘@recordCount int output‘,@recordCount output if( @recordCount % @pageSize = 0) --如果總記錄條數可以被頁大小整除 set @pageCount = @recordCount / @pageSize --總頁數就等於總記錄條數除以頁大小 else --如果總記錄條數不能被頁大小整除 set @pageCount = @recordCount / @pageSize + 1 --總頁數就等於總記錄條數除以頁大小加1 set @totalCount = @recordCountset nocount off;end--資料庫中執行該預存程序declare @pageCount int, @totalCount intexec proc_page_withtopmax 2,95955,@pageCount output,@totalCount outputselect ‘總頁數:‘+str(@pageCount)select ‘總條數:‘+str(@totalCount)

C# 代碼調用該帶輸入輸出參數的分頁預存程序

  public static DataSet GetRecordByPage( int pageSize, int pageIndex, out int pageCount, out int totalCount)        {            DataSet ds = new DataSet();            try            {                using (SqlConnection conn = new SqlConnection(@"server=;database=data_test;uid=; pwd=;"))                {                    SqlCommand cmd = new SqlCommand();                    cmd.Connection = conn;                    cmd.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int));                    cmd.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int));                    SqlParameter param = new SqlParameter("@totalCount", SqlDbType.Int);                    param.Direction = ParameterDirection.Output;                    cmd.Parameters.Add(param);                    SqlParameter param1 = new SqlParameter("@pageCount", SqlDbType.Int);                    param1.Direction = ParameterDirection.Output;                    cmd.Parameters.Add(param1);                    cmd.Parameters[0].Value = pageSize;                    cmd.Parameters[1].Value = pageIndex;                    cmd.Parameters[2].Value = 0;                    cmd.Parameters[3].Value = 0;                    cmd.CommandType = CommandType.StoredProcedure;                    cmd.CommandText = "proc_page_withtopmax";                    cmd.CommandTimeout = 180;                    SqlDataAdapter adapter = new SqlDataAdapter();                    adapter.SelectCommand = cmd;                    DataSet source = new DataSet();                    adapter.Fill(ds);                                     object o = cmd.Parameters["@totalCount"].Value;                    totalCount = (o == null || o == DBNull.Value) ? 0 : System.Convert.ToInt32(o);                    object b = cmd.Parameters["@pageCount"].Value;                    pageCount = (b == null || o == DBNull.Value) ? 0 : System.Convert.ToInt32(b);                }            }            catch (SqlException e)            {                throw e;            }            return ds;        }    }

 

sql server 帶輸入輸出參數的分頁預存程序(效率最高)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.