• sql server高效分页控件及c#调用实例


    第一、首先在sqlserver中创建一个存储过程

    USE [BZY]
    GO
    /****** 对象:  StoredProcedure [dbo].[up_ProcCustomPage2005_New]    脚本日期: 12/24/2013 11:17:03 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    
    -- =============================================
    -- Author:        <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:    <Description,,>
    -- =============================================
    CREATE PROCEDURE [dbo].[up_ProcCustomPage2005_New]
    @SelectSql varchar(4000)
    ,@OrderBy varchar(4000)
    ,@PageSize int
    ,@PageIndex int
    ,@PageCount int output
    ,@RecordCount int output
    AS
    
    --SET @SelectSql = 'select * from table_3'
    --SET @OrderBy = 'order by id desc'
    
    Declare @sql nvarchar(4000)
    SET @sql = 'select @RecourdCount = count(*) from (' + @SelectSql + ') as tt@#$'
    EXEC sp_executesql @sql,N'@RecourdCount int OUTPUT',@RecordCount OUTPUT
    
    SET @PageCount = CEILING( (@RecordCount + 0.0) / @PageSize)
    
    IF @PageIndex > @PageCount
        SET @PageIndex = @PageCount
        
    IF @PageIndex < 1
        SET @PageIndex = 1
    
    DECLARE @StartIndex int
    DECLARE @EndIndex int
    
    SET @StartIndex = (@PageIndex-1) * @PageSize + 1
    SET @EndIndex = @PageIndex * @PageSize
        
    SET @sql = 'select * from (select row_number()over(' + @OrderBy + ') as rownumber,*  from (' + @SelectSql + ') AS tt@#$) as tt@#$result where rownumber between ' + cast(@StartIndex AS varchar) + ' and ' + cast(@EndIndex AS varchar) 
    PRINT @sql    
    EXEC sp_executesql @sql

    二、c#代码:

            private static DataTable GetExecuteCustomPage_New(string selectSql, string orderBy, int pageSize, int pageIndex, out int pageCount, out int recordCount) 
            {
                MatchCollection mc = Regex.Matches(orderBy, @"([^ ]*).[^ ]*");
    
                if (mc.Count > 0)
                {
                    foreach (Match item in mc)
                    {                    
                        orderBy = orderBy.Replace(item.Groups[1].Value + ".", "");
                    }
                }           
    
                SqlParameter SelectSql = new SqlParameter() { ParameterName = "@SelectSql", Size = 4000, Value = selectSql };
                SqlParameter OrderBy = new SqlParameter() { ParameterName = "@OrderBy", Size = 4000, Value = orderBy };
                SqlParameter PageSize = new SqlParameter() { ParameterName = "@PageSize", Size = 4, Value = pageSize , DbType = DbType.Int32 };
                SqlParameter PageIndex = new SqlParameter() { ParameterName = "@PageIndex", Size = 4, Value = pageIndex , DbType = DbType.Int32 };
                SqlParameter PageCount = new SqlParameter() { ParameterName = "@PageCount", Size = 4, Direction = ParameterDirection.Output , DbType = DbType.Int32 };
                SqlParameter RecordCount = new SqlParameter() { ParameterName = "@RecordCount", Size = 4, Direction = ParameterDirection.Output , DbType = DbType.Int32 };
    
                DataSet ds = new DataSet();
            
                using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConfig"].ToString())) 
                {
                    SqlCommand cmd = cnn.CreateCommand();
                    cmd.CommandText = "up_ProcCustomPage2005_New";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = cnn;
                    cmd.Parameters.Add(SelectSql);
                    cmd.Parameters.Add(OrderBy);
                    cmd.Parameters.Add(PageSize);
                    cmd.Parameters.Add(PageIndex);
                    cmd.Parameters.Add(PageCount);
                    cmd.Parameters.Add(RecordCount);
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    cnn.Open();
                    sda.Fill(ds);
    
                    pageCount = Convert.ToInt32(PageCount.Value);
                    recordCount = Convert.ToInt32(RecordCount.Value);
                }
    
                return ds.Tables[0];
    
            }

    三、引用例子

    public DataSet GetPurchaserSalesVolumeAnalysis(string where,
               string sFilter_Condition, int iPage_Size, int iPage_Index, string sTaxisField,
               int iTaxis_Sign, out int iPageCount, out int iiRecord_Count)
            {
    
                string sql = @"select NewT.ptPaymentDate,NewT.fsStatus from Student NewT ";string orderwhere = " ORDER BY NewT.ptPaymentDate ";
                return BaitourDAO.Common.DBHelper.GetExecuteCustomPage_New(
                                      sql
                                      , orderwhere
                                      , iPage_Size
                                      , iPage_Index
                                      , out iPageCount
                                      , out iiRecord_Count);
            }
  • 相关阅读:
    音频电路设计中的基本知识(-)
    Usart的单线半双工模式(stm32F10x系列)
    RTS与CTS的含义
    NetBIOS与Winsock编程接口
    debian下使用gitosis+gitweb搭建SSH认证的git服务器
    解决:无法将“Add-Migration”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次
    Windows Azure Storage Client Library 2.0 入门
    Windows Azure Table Storage 解决 Guid 查询问题
    EF 报【序列包含一个以上的元素】解决办法
    javascript技巧大全套
  • 原文地址:https://www.cnblogs.com/honghong75042/p/3488725.html
Copyright © 2020-2023  润新知