通用SQL存储过程分页以及asp.net后台调用的方法
作者:jingxian 时间:2024-01-29 05:03:58
创建表格并添加300万数据
use Stored
CREATE TABLE UserInfo( --创建表
id int IDENTITY(1,1) PRIMARY KEY not null,--添加主键和标识列
UserName varchar(50)
)
declare @i int --添加3百万数据,大概4分钟时间
set @i=1
while @i<3000000
begin
insert into UserInfo (UserName) values(@i)
set @i=@i+1
end
存储过程T-SQL
create PROCEDURE [dbo].[GetDataList]
(
@TableName varchar(5000), --表名
@Fields varchar(5000) = '*', --字段名(全部字段为*)
@OrderField varchar(5000), --排序字段(必须!支持多字段)
@OrderType varchar(5000), --排序类型
@sqlWhere varchar(5000) = Null, --条件语句(不用加where)
@pageSize int, --每页多少条记录
@pageIndex int = 1 , --指定当前为第几页
@TotalPage int output, --返回总页数
@totalRecord int output --计算总记录数 --返回总记录数
)
as
begin Begin Tran --开始事务
Declare @sql nvarchar(500);
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'select @totalRecord = count(*) from ' + @TableName
else
set @sql = 'select @totalRecord = count(*) from ' + @TableName + ' where ' + @sqlWhere
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT--计算总记录数
--计算总页数
select @TotalPage=CEILING((@totalRecord+0.0)/@PageSize) if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField +' ' + @Ordertype+' ) as rowId,' + @Fields + ' from ' + @TableName
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField +' ' + @Ordertype+' ) as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
--处理页数超出范围情况
if @PageIndex<=0
Set @pageIndex = 1
if @pageIndex>@TotalPage
Set @pageIndex = @TotalPage --处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@pageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @pageSize - 1 --继续合成sql语句
set @Sql = @Sql + ') as ' + @TableName + ' where rowid between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
--print @Sql
Exec(@Sql)
---------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @totalRecord ---返回记录总数
End
end
--exec GetDataList 'Userinfo','*','id','desc','',10,1,3,3000000
前台页面Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Label ID="lbl_page" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
后台CS代码Default2.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
public partial class Default2 : System.Web.UI.Page
{
private int PageIndex = 0;//当前页码
private int PageSize = 50;//每页几条记录
private int TotalPage = 1;//总分页数
private int TotalRecord = 0;//总记录
private string OrderType = " desc";//排序方式 默认正序
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetParams();
DataSet ds = PageData("UserInfo", "*", "id", OrderType, "", PageSize, PageIndex, out TotalPage, out TotalRecord);
GridView1.DataSource = ds;
GridView1.DataBind();
lbl_page.Text = GetDivPager("", ds);
}
}
//数据库连接字符
public static string StrConn()
{
//return string.Format("{0}","server=.;database=Stored;user=sa;password=123456");
return ConfigurationSettings.AppSettings["ConnString"].ToString();
}
//Get方式获得下一页
private void GetParams()
{
if (!String.IsNullOrEmpty(Request["page"]))
{
PageIndex = Convert.ToInt32(Request["Page"]);
}
else
{
PageIndex = 1;
}
}
#region 获得分页字符
public string GetDivPager(string queryString, DataSet ds)
{
StringBuilder sp = new StringBuilder();
int TotalCount = TotalRecord;
int rowCount = TotalPage;
if (ds != null)
{
sp.AppendFormat(" <p>总记录:<span id=\"sum\">{0}</span>", TotalCount);
sp.AppendFormat(" 页码:<em><b id=\"current\">{0}</b>/<span id=\"count\">{1}</span></em> ", PageIndex, rowCount);
sp.AppendFormat(" 每页:<span id=\"eachPage\">{0}</span></p> ", PageSize);
sp.AppendFormat(" <a href='{0}'>首页</a> ", "?page=1" + queryString);
if (PageIndex > 1)
{
sp.AppendFormat(" <a href='{0}'>< 上一页 </a>", "?page=" + (PageIndex - 1) + queryString);
}
int temp = 0;
int loopc = rowCount > 10 ? 10 : rowCount;
for (int i = 0; i < loopc; i++)
{
temp = i + 1;
if (PageIndex > 10) { temp = (PageIndex - 10) + i + 1; }
sp.AppendFormat(" <a class=\"{0}\" href='{1}'>{2}</a>", PageIndex == temp ? "active" : "", "?page=" + temp + queryString, temp);
}
if (PageIndex != rowCount)
{
sp.AppendFormat(" <a href='{0}'>下一页 ></a>", "?page=" + (PageIndex + 1) + queryString);
}
sp.AppendFormat(" <a href='{0}'>尾页</a>", "?page=" + rowCount + queryString);
}
else
{
ds = null;
}
return sp.ToString();
}
#endregion
#region 获取分页的数据
/// <summary>
/// 获取分页的数据
/// </summary>
/// <param name="TblName">数据表名</param>
/// <param name="Fields">要读取的字段</param>
/// <param name="OrderField">排序字段</param>
/// <param name="OrderType">排序方式</param>
/// <param name="SqlWhere">查询条件</param>
/// <param name="PageSize">每页显示多少条数据</param>
/// <param name="pageIndex">当前页码</param>
/// <param name="TotalPage">返回值,共有多少页</param>
/// <param name="TotalRecord">返回值,总有多少条记录</param>
/// <returns></returns>
public static DataSet PageData(string TblName, string Fields, string OrderField, string OrderType, string SqlWhere, int PageSize, int pageIndex, out int TotalPage, out int TotalRecord)
{
SqlConnection conn = new SqlConnection(StrConn());
SqlCommand comm = new SqlCommand("GetDataList", conn);
comm.Parameters.Add(new SqlParameter("@TableName", SqlDbType.NVarChar, 100)).Value = TblName;
comm.Parameters.Add(new SqlParameter("@Fields", SqlDbType.NVarChar, 1000)).Value = Fields;
comm.Parameters.Add(new SqlParameter("@OrderField", SqlDbType.NVarChar, 1000)).Value = OrderField;
comm.Parameters.Add(new SqlParameter("@OrderType", SqlDbType.NVarChar, 1000)).Value = OrderType;
comm.Parameters.Add(new SqlParameter("@sqlWhere", SqlDbType.NVarChar, 1000)).Value = SqlWhere;
comm.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int)).Value = PageSize;
comm.Parameters.Add(new SqlParameter("@pageIndex", SqlDbType.Int)).Value = pageIndex;
comm.Parameters.Add(new SqlParameter("@TotalPage", SqlDbType.Int));
comm.Parameters["@TotalPage"].Direction = ParameterDirection.Output;//获得out出来的参数值
comm.Parameters.Add(new SqlParameter("@totalRecord", SqlDbType.Int));
comm.Parameters["@totalRecord"].Direction = ParameterDirection.Output;
comm.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dataAdapter = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
TotalPage = (int)comm.Parameters["@TotalPage"].Value;
TotalRecord = (int)comm.Parameters["@totalRecord"].Value;
conn.Close();
conn.Dispose();
comm.Dispose();
return ds;
}
#endregion
}
标签:sql,分页,存储,过程
0
投稿
猜你喜欢
对Python函数设计规范详解
2023-08-02 15:59:17
python神经网络MobileNetV2模型的复现详解
2023-08-19 16:20:44
Python远程控制Windows服务器的方法详解
2023-07-13 13:14:03
python如何为创建大量实例节省内存
2023-03-19 06:35:37
python爬取豆瓣电影TOP250数据
2021-12-27 12:50:17
django主动抛出403异常的方法详解
2023-03-28 17:40:10
Python的shutil模块中文件的复制操作函数详解
2023-04-13 16:59:22
Python Pyinstaller库安装步骤以及使用方法
2021-05-12 07:07:26
MySQL的表级锁,行级锁,排它锁和共享锁
2024-01-28 15:44:22
CSS的书写顺序规范
2008-06-12 13:51:00
合理设置内存让数据库与其他程序共存
2009-05-21 16:24:00
js 数据存储和DOM编程
2024-02-24 07:52:31
python2.7 json 转换日期的处理的示例
2021-02-10 12:39:50
pdo中使用参数化查询sql
2023-07-20 21:11:21
基于python的selenium两种文件上传操作实现详解
2022-01-31 23:02:17
canvas 2d 环形统计图手写实现示例
2023-07-13 16:35:23
matplotlib bar()实现多组数据并列柱状图通用简便创建方法
2022-12-11 11:59:15
让程序员都费解的10大编程语言特性
2023-09-12 03:56:49
Python 12306抢火车票脚本
2023-09-12 13:36:11
jquery 将disabled的元素置为enabled的三种方法
2024-04-19 10:19:14