C#调用SQL Server分页存储过程
以SQL Server2012提供的offset ..rows fetch next ..rows only为例
e.g.
表名:Tab1
----------------------------------
ID Name
1 tblAttributeGroupDetail
2 tblAttributeGroup
3 tblAttribute
.......
50 tblBRItemTypeAppliesTo
51 tblBRItemProperties
52 tblBRItem
53 tblBRBusinessRule
54 Test
--创建分页存储过程 rTabByCondition
USE [ExampleDB]
GO
if OBJECT_ID('rTabByCondition','P') is not null
drop procedure rTabByCondition
GO
create procedure [dbo].[rTabByCondition](
@PageCount int=1 --页数
,@PageSize int=10 --页显示记录数
,@Rowcount int=0 output --总记录数
)
as
set nocount on;
declare @Rows int;
select * from dbo.Tab1 order by ID offset (@PageCount-1)*@PageSize rows fetch next @PageSize rows only
set @Rows=@@ROWCOUNT
select @Rowcount=count(*) from dbo.Tab1;
return @Rows
go
declare @i int,@j int
exec @i=[rTabByCondition] @PageCount=6,@PageSize=10,@Rowcount=@j output
select @i as "@Rowcount",@j as "Return_Value"
go
显示结果:
--打开Visual Studio—创建项目—选择【控制台应用程序】
#region Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
#endregion namespace SQLStoredProcedure2
{
class Program
{
static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Server=(Local)\SQL16;Integrated Security=True;Database=ExampleDB");
thisConnection.Open();
SqlCommand thisCommend = thisConnection.CreateCommand();
thisCommend.CommandType = CommandType.StoredProcedure;
thisCommend.CommandText = "rTabByCondition";
thisCommend.Parameters.AddWithValue("@PageCount", "6");//页数
thisCommend.Parameters.AddWithValue("@PageSize", "10");//页显示记录数
SqlParameter paraOut = thisCommend.Parameters.Add("@Rowcount", SqlDbType.Int);//输出参数定义
paraOut.Direction = ParameterDirection.Output;
SqlParameter paraRet = thisCommend.Parameters.Add("return_value", SqlDbType.Int);//返回值
paraRet.Direction = ParameterDirection.ReturnValue;
SqlDataReader thisReader = thisCommend.ExecuteReader();
while (thisReader.Read())
{
Console.WriteLine("ID:{0}\tName:{1}", thisReader[0], thisReader[1]);
}
thisReader.Close();
thisConnection.Close();
Console.WriteLine("Rows:{0};\tReturn_Value:{1};", paraOut.Value, paraRet.Value);
Console.WriteLine("Program finished,press Enter/Return to continue:");
Console.ReadLine();
}
}
}
显示效果:
C#调用SQL Server分页存储过程的更多相关文章
- (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法
本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...
- Delphi 调用SQL Server 2008存储过程
1.表结构如下(预算数据明细表): CREATE TABLE [dbo].[BA_FeeDetail]( [ID] [int] IDENTITY(1,1) NOT NULL, [FeeDeptID] ...
- Java调用SQL Server的存储过程详解
转载自Microsoft的官方文档 http://msdn2.microsoft.com/zh-cn/library/ms378995.aspx收录于 www.enjoyjava.net/f25 本文 ...
- (转)Java调用SQL Server的存储过程详解
本文转载自:http://dev.yesky.com/128/8088128.shtml 1使用不带参数的存储过程 使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序 ...
- Java调用SQL Server的存储过程详解(转)
1使用不带参数的存储过程 使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序列.不带参数的 call 转义序列的语法如下所示: 以下是引用片段:{call proce ...
- 分页系列之一:SQL Server 分页存储过程
以下为最基本的代码结构,SQL Server 2012 开始支持 CREATE PROCEDURE procXXX @Page int, --当前页码,从1开始 @PageSize int --每页记 ...
- sql server 分页存储过程
----------------------分页存储过程------------------------------------------------------------------------ ...
- [转]Sql Server 分页存储过程
本文转自: 版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且 ...
- c++ builder调用sql server的存储过程进行数据的下载和上传
小小的几行代码,在这里搞了一天.好好的一个周六过的无比的难受.代码很简单,但是主要原因是因为在用合作商的软件上传数据的时候有些框框没有勾选. come on....... 1.用两个控件ADOConn ...
随机推荐
- 最新微信公众平台js sdk整合PHP版
由于没有持续关注微信公众平台相关的开发,所以看到这个东西时,都没有耐心看完开发文档,或者不知道重点. 重点在哪呢?重点在示例代码:http://mp.weixin.qq.com/wiki/7/aaa1 ...
- JavaScript的DOM操作。Window.document对象
间隔执行一段代码:window.setlnteval("需要执行的代码",间隔毫秒数) 例 : window.setlnteval("alert("你 ...
- Eclipse保存文件时自动格式化代码
实现效果:Ctrl+S会自动格式化并保存代码. 应用上图所示效果之后,在每次对Eclipse保存的时候都会实现自动格式化代码. 1. Fomated All lines,格式化该文件的所有代码:还是 ...
- Verilog学习笔记简单功能实现(六)...............计数分频电路
在分频器电路中最重要的概念有两个:1)奇分频/偶分频:2)占空比. A)其中最简单的就是二分频电路,占空比为50%,其Verilog程序为 module half_clk(clr,clk_in,clk ...
- 利用Canvas编辑图片
使用<canvas>对象在浏览器中把一幅彩色图片变成灰度图片. grayscale.html <!DOCTYPE html> <html lang="en&qu ...
- linux下安装redis的详细过程
先安装一些组件: yum -y install gcc gcc-c++ libstdc++-devel 下载并安装: # wget http://download.redis.io/releases/ ...
- Node.JS文件系统解析
1.Node.js 文件系统 var fs = require("fs") 2.异步和同步 读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFile ...
- According to TLD or attribute directive in tag file, attribute end does not accept any expressions
问题描述: 在 JSP 页面中使用 JSTL 标签库,访问 JSP 页面时抛出了如下异常信息: org.apache.jasper.JasperException: /WEB-INF/manageUs ...
- 【转】PHP调试开发工具你认识多少?
来源:PHP100中文网(http://www.php100.com/html/itnews/PHPxinwen/2009/0902/3257.html) PHP现在已经是使用最为广泛的开源服务器端脚 ...
- 属性观察器willSet与didSet
willSet与didSet是swift特有的,就是监听属性值的变化,但有一个小注意点. willSet与didSet调用时机:对象创建后的第一次赋值时开始调用.也就是说,初始化时(包含重载的init ...