SQLHelper

public class SqlHelper
{
private readonly string _constr = ConfigurationManager.ConnectionStrings["key"].ConnectionString; #region 增删改通用方法
public int ExecuteNonQuery(string sql, CommandType type, params SqlParameter[] pms)
{
//创建链接对象
//创建命令对象
//打开链接
//执行
using (SqlConnection con = new SqlConnection(_constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
cmd.CommandType = type;
con.Open();
return cmd.ExecuteNonQuery();
}
}
}
#endregion #region 返回单行单列 public object ExecuteScalar(string sql, CommandType type, params SqlParameter[] pms)
{
using (SqlConnection con = new SqlConnection(_constr))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
cmd.CommandType = type;
con.Open();
return cmd.ExecuteScalar();
}
}
} #endregion #region datatable查询
public DataTable ExecuteTable(string sql, CommandType type, params SqlParameter[] pms)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(_constr))
{
using (SqlDataAdapter cmd = new SqlDataAdapter(sql, con))
{
if (pms != null)
{
cmd.SelectCommand.Parameters.AddRange(pms);
}
cmd.SelectCommand.CommandType = type;
con.Open();
cmd.Fill(dt);
}
}
return dt;
}
#endregion #region Reader查询
public SqlDataReader ExecuteReader(string sql, CommandType type, params SqlParameter[] pms)
{
SqlConnection con = new SqlConnection(_constr);
SqlCommand cmd = new SqlCommand(sql, con);
if (pms != null)
{
cmd.Parameters.AddRange(pms);
}
cmd.CommandType = type;
try
{
con.Open();
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception)
{
cmd.Dispose();
con.Close();
con.Dispose();
throw;
}
}
#endregion
}

分页

public DataTable GetDataByPage(int start,int end)
{
string sql = "select * from(select *,num=ROW_NUMBER() over (order by id) from UserInfo) as t where t.num>@start and t.num<=@end";
SqlParameter[] pms =
{
new SqlParameter("@start",start),
new SqlParameter("@end",end),
};
return helper.ExecuteTable(sql, CommandType.Text, pms);
}

存储过程

private void LoadData(int index)
{
string sql = "usp_UserInfoPage";
SqlParameter[] pms = {
new SqlParameter("@pageIndex",index),
new SqlParameter("@pageSize",PageSize),
new SqlParameter("@pageCount",SqlDbType.Int){Direction=ParameterDirection.Output},
new SqlParameter("@totalCount",SqlDbType.Int){Direction=ParameterDirection.Output}
}; DataTable dt = db.ExecuteTable(sql, CommandType.StoredProcedure, pms);
List<ClassInfo> list = new List<ClassInfo>();
foreach (DataRow item in dt.Rows)
{
ClassInfo model = new ClassInfo();
model.Id = Convert.ToInt32(item["Id"]);
model.name = item["name"].ToString();
model.pwd = item["pwd"].ToString();
list.Add(model);
}
dataGridView1.DataSource = list;
pageCount = Convert.ToInt32(pms[2].Value);
totalCounnt = Convert.ToInt32(pms[3].Value);
label6.Text = string.Format("{0}/{1}", index, pageCount);
}
create database UserDB
go
use UserDB
go
create table Userinfo(
ID int primary key identity ,
name varchar(20) not null,
pwd varchar(20) not null
)
insert into Userinfo values('sasas','1231231')
insert into Userinfo values('sasas','1231231')
insert into Userinfo values('sasas','1231231')
insert into Userinfo values('admin','123456')
select id,name,pwd from userinfo
go
create proc usp_UserInfoPage
@pageIndex int,
@pageSize int,
@pageCount int output,
@totalCount int output
as
begin
--查询总条数
select @totalCount = COUNT(*) from UserInfo
--查询总页数
set @pageCount = CEILING(@totalCount*1.0/@pageSize)
--查询语句
select * from (select *,num=ROW_NUMBER() over(order by Id) from UserInfo) as t where t.num>(@pageIndex-1)*@pageSize and t.num<=@pageIndex*@pageSize
end
select * from (select *,num=row_number() over(order by id) from userInfo) as t where t.num>0 and t.num<=4

ADO.NET 帮助类 参数传递 存储过程 分页的更多相关文章

  1. 存储过程分页 Ado.Net分页 EF分页 满足90%以上

    存储过程分页: create proc PR_PagerDataByTop @pageIndex int, @pageSize int, @count int out as select top(@p ...

  2. asp.net利用存储过程分页代码

    -最通用的分页存储过程 -- 获取指定页的数据 CREATE PROCEDURE Pagination ), -- 表名 ) = '*', -- 需要返回的列 )='', -- 排序的字段名 , -- ...

  3. SQL存储过程分页(通用的拼接SQL语句思路实现)

    多表通用的SQL存储过程分页 案例一: USE [Community] GO /****** Object: StoredProcedure [dbo].[Common_PageList] Scrip ...

  4. Oracle数据库中调用Java类开发存储过程、函数的方法

    Oracle数据库中调用Java类开发存储过程.函数的方法 时间:2014年12月24日  浏览:5538次 oracle数据库的开发非常灵活,不仅支持最基本的SQL,而且还提供了独有的PL/SQL, ...

  5. php分页类代码带分页样式效果(转)

    php分页类代码,有漂亮的分页样式风格 时间:2016-03-16 09:16:03来源:网络 导读:不错的php分页类代码,将类文件与分页样式嵌入,实现php查询结果的精美分页,对研究php分页原理 ...

  6. 存储过程 分页【NOT IN】和【>】效率大PK 千万级别数据测试结果

    use TTgoif exists (select * from sysobjects where name='Tonge')drop table Tongecreate table Tonge( I ...

  7. Sql Service存储过程分页

    一起是用oracle数据库..感觉oracle数据库强大.查询速度是杠杠的.换了家公司用的是SQL SERVICE.以前用了1年现在捡回以前的记忆.动手写了动态SQL过存储过程分页.感觉和oracle ...

  8. MS SQLSERVER通用存储过程分页

    最近在面试的时候,遇到个奇葩的秃顶老头面试官. 问:写过存储过程分页吗? 答:没写过,但是我知道分页存储的原理,我自己也写过,只是在工作中没写过. 问:那你这么多年工作中就没写过吗? 答:的确没写过, ...

  9. 【原创】10万条数据采用存储过程分页实现(Mvc+Dapper+存储过程)

    有时候大数据量进行查询操作的时候,查询速度很大强度上可以影响用户体验,因此自己简单写了一个demo,简单总结记录一下: 技术:Mvc4+Dapper+Dapper扩展+Sqlserver 目前主要实现 ...

随机推荐

  1. Ajax原理与图解

    Ajax原理 Ajax 的全称是Asynchronous JavaScript and XML. Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后 ...

  2. python中字符串的编码和解码

    1. 常用的编码 ASCII:只能表示一些字母,数字和特殊的字符,占一个字节 GBK:国家简体中文字符集和繁体字符集,兼容ASCII,占两个字节 Unicode:能够表示全世界上所有的字符,Unico ...

  3. SpringBoot集成FastDFS依赖实现文件上传

    前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...

  4. MiniProfiler性能监控分析工具在.NET项目中的使用

    MiniProfiler是一款针对.NET, Ruby, Go and Node.js的性能分析的轻量级程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控 ...

  5. 第四章 、PyQt中的信号(signal)和槽(slot)机制以及Designer中的使用

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 前面章节其实已经在使用信号和槽了,但是作为Qt中最重要的机制也是Qt区别与其他开发平台的重 ...

  6. kettle如何从cube抽数据

    接触kettle已经还是有一段时间了,但是一直都使用简单的输入.输出(二维数据库to二维数据库).今天,突然接到一个需求,需要从多维数据库(CUBE)里面将数据抽取到二维数据库,我难住了,不知道该如何 ...

  7. python中的Restful

    哇,昨天组里进行总结的时候,小哥哥和小姐姐真是把我给秀到了,跟他们一比,我总结的太垃圾了,嘤嘤嘤.因为我平常不怎么总结,总结的话,有word还有纸质的,现在偏向于纸质,因为可以练练字.个人观点是,掌握 ...

  8. mvvm和mvc区别?

    mvc和mvvm其实区别并不大.都是一种设计思想.主要就是mvc中Controller演变成mvvm中的viewModel. mvvm主要解决了mvc中大量的DOM 操作使页面渲染性能降低,加载速度变 ...

  9. BJOI2017 机动训练

    落谷.Loj. Description 定义机动路径为: 没有自环 路径至少包含两个格子 从起点开始每一步都向不远离终点的方向移动 相同地形序列指路径上顺序经过的地形序列. 定义机动路径的权值为相同地 ...

  10. Java集合源码分析(三)——LinkedList

    简介 LinkedList是一个链表结构的列表,也可以被作为堆栈.队列或双端队列使用.它继承于AbstractSequentialList双向链表,实现了List.Deque.Cloneable.ja ...