博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL存储过程分页以及.NET调用
阅读量:5769 次
发布时间:2019-06-18

本文共 11389 字,大约阅读时间需要 37 分钟。

存储过程:

create proc Test

 @PageIndex INT,--@PageIndex从计数,0为第一页
 @PageSize INT, --页面大小
 @RecordCount INT OUT, --总记录数
 @PageCount INT OUT--页数
as
SELECT @RecordCount = COUNT(*) FROM A --获取记录数
SET @PageCount = CEILING(@RecordCount * 1.0 / @PageSize) --计算页面数据
SELECT SerialNumber,Id,Names,Age FROM
(SELECT Id,Names,Age,ROW_NUMBER() OVER (ORDER BY Id )  AS SerialNumber FROM A ) AS T
WHERE T.SerialNumber > (@PageIndex * @PageSize) and T.SerialNumber <= ((@PageIndex+1) * @PageSize)
go

存储过程测试:

DECLARE @RecordCount int ,@PageCount int
exec Test 0,10, @RecordCount  OUTPUT, @PageCount  OUTPUT
select @RecordCount,@PageCount
go
存储过程的调用:
-----已更新---
DBHelper类中的方法:
        /// <summary>
        /// sql带返回参数的存储过程(分页)
        /// </summary>
        /// <param name="procName">存储过程名字</param>
        /// <param name="outParameterName">输出参数的名字</param>
        /// <param name="dic">输出参数得到的值</param>
        /// <param name="pars">参数列表</param>
        /// <returns>DataTable</returns>
        public static DataTable GetFillData(string procName,string [] outParameterName, out Dictionary<string,object> dic,params SqlParameter[] pars)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            DataSet ds = new DataSet();
            using (cmd = new SqlCommand(procName, Connectionstrings))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(pars);
                foreach (var item in outParameterName)
                {
                    cmd.Parameters[item].Direction = ParameterDirection.Output;
                }
                using (adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(ds);
                    dic = dictionary;
                    foreach (var item in outParameterName)
                    {
                        dictionary.Add(item, cmd.Parameters[item].Value.ToString());
                    }
                    return ds.Tables[0];
                }
            }
        }
DBHelper类可能大家的都不同,可以根据自己的DBHelper类修改此方法。
在这里我也把自己用的DBHelper写出来(注重版权,本人只是交流需要,无其他商业目的)

//  ***********************************************************************

//        Copyright (C) 2008-2009  じJF[CMS]ve

//        All rights reserved

//        guid1:  5315b4dc-168b-44a3-98c0-83524a8bfcce

//        CLR Version:        2.0.50727.1433

//        CreateUser:        じJF[CMS]ve

//        Email:              jfcms_lzq@qq.com
//        SiteWeb:           http://www.jfstudio.net

//        CreateDate:         11/08/2008 13:22:59

//           
//  ***********************************************************************

using System;

using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace WebSite1.DAL

{
    /// <summary>
    /// DBHelper 的摘要说明

    /// </summary>
    public  class DBHelper : IDisposable
    {
        //定义这个类要使用的全局变量
        private static string constr;
        private static SqlConnection conn;
        private static SqlCommand cmd;
        private static SqlDataReader dr;
        private static SqlDataAdapter adapter;

        /// <summary>
        /// 数据库连接属性

        /// </summary>
        public static SqlConnection Connectionstrings
        {
            get
            {
                constr = ConfigurationManager.ConnectionStrings["SqlConnectionStrings"].ToString();
                //上面这个必须添加引用System.configuartion
                conn = new SqlConnection(constr);
                //DotNet默认打开数据库连接池
                conn.Open();
                return conn;

            }

        }
        // 先做几个处理 ,该类实现了IDisposable接口,自动调用非托管堆中释放资源,在由GC自动清理。

        public void Dispose()
        {
            Close();
            cmd.Dispose();
            dr.Dispose();
            conn.Dispose();
        }

        /// <summary>

        /// 取消 Command 执行,并关闭 DataReader 对象和数据连接

        /// </summary>
        public void Close()
        {
            cmd.Cancel();
            if (!dr.IsClosed)
                dr.Close();
            if (conn.State != ConnectionState.Closed)
                conn.Close();
        }
        /// <summary>
        /// 创建一个 SQL 参数,主要实现SqlParameter[] 参数列表
        /// </summary>
        /// <param name="parameterName">参数名</param>
        /// <param name="dbType">类型</param>
        /// <param name="value">值</param>
        /// <returns>返回创建完成的参数</returns>
        public static SqlParameter CreateParameter(string parameterName, SqlDbType dbType, object value)
        {
            SqlParameter result = new SqlParameter(parameterName, dbType);
            //if(value!=null)
                result.Value = value;
            return result;
        }

 

        /// <summary>

        /// 单向操作,主要用于(增加,删除,修改),返回受影响的行数
        /// </summary>
        /// <param name="cmdTxt">安全的sql语句(string.format)</param>
        /// <returns></returns>
        public static int GetExcuteNonQuery(string cmdTxt)
        {
            return GetExcuteNonQuery(cmdTxt, null);
        }
        /// <summary>
        /// 带参数化的 主要用于(增加,删除,修改),返回受影响的行数
        /// </summary>
        /// <param name="cmdTxt">带参数列表的sql语句</param>
        /// <param name="pars">要传入的参数列表</param>
        /// <returns></returns>
        public static int GetExcuteNonQuery(string cmdTxt, params SqlParameter[] pars)
        {
            using (cmd = new SqlCommand(cmdTxt, Connectionstrings))
            {
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                return cmd.ExecuteNonQuery();
            }
        }

        /// <summary>

        /// 对连接执行 Transact-SQL 语句或者存储过程并返回受影响的行数
        /// </summary>
        /// <param name="cmdText">SQL 语句或者存储过程名称</param>
        /// <param name="cmdType">枚举存储过程或者sql查询文本</param>
        /// <param name="pars">参数</param>
        /// <returns>受影响的行数</returns>
        public static int GetExcuteNonQuery(string cmdTxt, CommandType cmdtype, params SqlParameter[] pars)
        {
            using (cmd = new SqlCommand(cmdTxt, Connectionstrings))
            {
                cmd.CommandType = cmdtype;
                cmd.Parameters.AddRange(pars);
                return cmd.ExecuteNonQuery();
            }
        }

        /// <summary>

        /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。

        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="cmdText">SQL 语句</param>
        /// <param name="pars">参数列表</param>
        /// <returns>结果集中第一行的第一列或空引用</returns>
        public static T ExecuteScalar<T>(string cmdText, params SqlParameter[] pars)
        {
            using (SqlCommand cmd = new SqlCommand(cmdText, Connectionstrings))
            {
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                T result = (T)cmd.ExecuteScalar();
                conn.Close();
                return result;
            }

        }

        /// <summary>
        /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。

        /// </summary>
        /// <typeparam name="T">返回的类型</typeparam>
        /// <param name="cmdType">枚举存储过程或者sql查询文本</param>
        /// <param name="cmdText">SQL 语句或者存储过程名称</param>
        /// <returns></returns>
        public static T ExecuteScalar<T>(string cmdText, CommandType type, params SqlParameter[] pars)
        {
            using (SqlCommand cmd = new SqlCommand(cmdText, Connectionstrings))
            {
                cmd.CommandType = type;
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                T result = (T)cmd.ExecuteScalar();
                conn.Close();
                return result;
            }

        }

        /// <summary>
        /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。

        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="cmdText">sql语句</param>
        /// <returns></returns>
        public static T ExecuteScalar<T>(string cmdText)
        {
            return ExecuteScalar<T>(cmdText, null);
        }
        /// <summary>
        /// 将 cmdText 发送到 System.Data.SqlClient.SqlCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
        /// </summary>
        /// <param name="cmdTxt">安全的sql语句(string.format)</param>
        /// <returns>一个 DataReader 对象</returns>
        public static SqlDataReader GetDataReader(string cmdTxt)
        {
            return GetDataReader(cmdTxt, null);
        }
        /// <summary>
        /// 将 cmdText 发送到 System.Data.SqlClient.SqlCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
        /// </summary>
        /// <param name="cmdTxt">安全的sql语句(string.format)</param>
        /// <param name="pars">参数</param>
        /// <returns>一个 DataReader 对象</returns>
        public static SqlDataReader GetDataReader(string cmdTxt, params SqlParameter[] pars)
        {
            using (cmd = new SqlCommand(cmdTxt, Connectionstrings))
            {
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }
        }
        /// <summary>
        /// 将 cmdText 发送到 System.Data.SqlClient.SqlCommand.Connection,并使用 System.Data.CommandBehavior 值之一生成一个 DataReader
        /// </summary>
        /// <param name="cmdTxt">安全的sql语句(string.format)或者存储过程名称</param>
        /// <param name="pars">参数</param>
        /// <param name="cmdType">枚举存储过程或者sql查询文本</param>
        /// <returns>一个 DataReader 对象</returns>
        public static SqlDataReader GetDataReader(string cmdTxt, CommandType type, params SqlParameter[] pars)
        {
            using (cmd = new SqlCommand(cmdTxt, Connectionstrings))
            {
                cmd.CommandType = type;
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                return dr;
            }
        }

        /// <summary>

        /// 做数据绑定显示作用,一般绑定的是数据查看控件

        /// </summary>
        /// <param name="cmdTxt">sql语句</param>
        /// <param name="tableName">要绑定显示的具体表名</param>
        /// <returns>返回一个数据表</returns>
        public static DataTable GetFillData(string cmdTxt)
        {
            return GetFillData(cmdTxt, null);
        }
        /// <summary>
        /// 做数据绑定显示作用,一般绑定的是数据查看控件

        /// </summary>
        /// <param name="cmdTxt">带参数的sql语句</param>
        /// <param name="pars">参数列表</param>
        /// <returns>返回是一个数据表</returns>

        public static DataTable GetFillData(string cmdTxt, params SqlParameter[] pars)

        {
            DataSet ds = new DataSet();
            using (cmd = new SqlCommand(cmdTxt, Connectionstrings))
            {
                if (pars != null)
                    cmd.Parameters.AddRange(pars);
                using (adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }
        /// <summary>
        /// 做数据绑定显示作用,一般绑定的是数据查看控件

        /// </summary>
        /// <param name="cmdTxt">带参数的sql语句</param>
        /// <param name="cmdType">枚举存储过程或者sql查询文本</param>
        /// <param name="pars">参数列表</param>
        /// <returns>返回是一个数据表</returns>

        public static DataTable GetFillData(string cmdTxt, CommandType type, params SqlParameter[] pars)

        {
            DataSet ds = new DataSet();
            using (cmd = new SqlCommand(cmdTxt, Connectionstrings))
            {
                cmd.CommandType = type;
                cmd.Parameters.AddRange(pars);
                using (adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(ds);
                    return ds.Tables[0];
                }
            }
        }
        /// <summary>
        /// 分页数据绑定显示
        /// </summary>
        /// <param name="cmdTxt">string.format格式化sql语句,格式如:"select top {0} * from books where typeid not in (select top {1} id from books order by typeid) order by typeid"总记录数 TotalRecordCount总记录数通过executescalar获取</param>
        /// <param name="pageSize">设置的分页数大小,默认为10</param>
        /// <param name="currentIndex">当前页的索引,通常是通过querystring获取.如:string currentIndex = Request.QueryString["id"] ?? "1"</param>
        /// <returns>返回当前页的数据显示</returns>
        public static DataTable GetFillData(string cmdTxt, int pageSize, int currentIndex)
        {
            DataTable dt = new DataTable();
            using (adapter = new SqlDataAdapter(string.Format(cmdTxt, pageSize, pageSize * (currentIndex - 1)), Connectionstrings))
            {
                adapter.Fill(dt);
            }
            return dt;
        }

        /// <summary>

        /// sql带返回参数的存储过程(分页)
        /// </summary>
        /// <param name="procName">存储过程名字</param>
        /// <param name="outParameterName">输出参数的名字</param>
        /// <param name="dic">输出参数得到的值</param>
        /// <param name="pars">参数列表</param>
        /// <returns>DataTable</returns>
        public static DataTable GetFillData(string procName,string [] outParameterName, out Dictionary<string,object> dic,params SqlParameter[] pars)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            DataSet ds = new DataSet();
            using (cmd = new SqlCommand(procName, Connectionstrings))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(pars);
                foreach (var item in outParameterName)
                {
                    cmd.Parameters[item].Direction = ParameterDirection.Output;
                }
                using (adapter = new SqlDataAdapter(cmd))
                {
                    adapter.Fill(ds);
                    dic = dictionary;
                    foreach (var item in outParameterName)
                    {
                        dictionary.Add(item, cmd.Parameters[item].Value.ToString());
                    }
                    return ds.Tables[0];
                }
            }
        }

    }

}

 现在我们看看表示层的调用:

        //数据绑定
        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        DataTable dt = new DataTable();
        string[] outParameterName = { "@RecordCount", "@PageCount" };//要输出的参数放入数组中
        dt = DBHelper.GetFillData("Test", outParameterName, out dictionary,
        DBHelper.CreateParameter("@PageIndex", SqlDbType.Int, PageIndex),
        DBHelper.CreateParameter("@PageSize", SqlDbType.Int, 10),
        DBHelper.CreateParameter("@RecordCount", SqlDbType.Int, 100),
        DBHelper.CreateParameter("@PageCount", SqlDbType.Int, 100));
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();
        //绑定当前页、总页数、总条数
        this.lb_RecordCount.Text = dictionary["@RecordCount"].ToString();//从dictionary取到输出参数的值
        this.lb_PageCount.Text = dictionary["@PageCount"].ToString();
        this.lb_Page.Text = (PageIndex +1).ToString();
         

至于页面的下一页、上一页跳到哪一页、这里就不多说了,只是加一减一罢了。有什么更好的建议,请联系本人。大家共同进步。

转载于:https://www.cnblogs.com/richzhang/archive/2013/03/27/2984818.html

你可能感兴趣的文章
P1078 文化之旅
查看>>
pip 更换国内镜像与记录
查看>>
oracle之 11g RAC R2 体系结构---Grid
查看>>
定义类和对象
查看>>
layer获取iframe内容
查看>>
USB_4大描述符
查看>>
Activity启动模式
查看>>
Rufus 制作 USB 启动盘简单教程
查看>>
站在宇宙中心呼唤爱:程序员节(10月24日)今年不加班,明年不上班!
查看>>
【转】允许远程用户登录访问mysql的方法
查看>>
JAVA内部线程2
查看>>
【C#】详解C#委托
查看>>
敏捷开发与Scrum
查看>>
网络驱动移植之解析Linux网络驱动的基本框架
查看>>
GROUP BY 和 GROUP_CONCAT的使用
查看>>
PHP团队 编码规范 & 代码样式风格规范
查看>>
Docker学习笔记 - Docker Compose
查看>>
数据、事实、实体、值对象、事务、不变性
查看>>
php里面的编码问题
查看>>
RESTful API浅谈
查看>>