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

系统 2573 0

第一、首先在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);

        }
      
    

 

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


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论