创建有输出参数的存储过程并在c#中实现DataGridView分页功能
不足之处,欢迎指正!
创建有输出参数的存储过程
if exists(select * from sysobjects where name='usp_getPage1')
drop procedure usp_getPage1
go
create procedure usp_getPage1--存储过程名称
@count int output,--输出参数
@countIndex int=1,--参数带默认值
@countPage int=5--参数带默认值
as
--一个sql语句。ROW_NUMBER ( ) OVER ()返回结果集分区内行的序列号,每个分区的第一行从 1 开始,这里给别名id。temp也是一个别名,是前面括号的数据
select * from(select row_number()over(order by studentno)id,* from student)temp where id >(@countIndex-1)*@countPage and id<=@countIndex*@countPage
--给输出参数赋值
set @count=ceiling((select count(*) from student)*1.0/@countPage)
--ceiling四舍五入的节奏,乘以1.0呢,是因为int除以int还是得到int。浮点型除以整型得到浮点型
go
--执行存储过程,有默认值的可以不给值,但是输出参数需要声明一个变量接收
declare @num int
execute usp_getPage1 @count=@num output
print @num
创建一个类,里面有一个静态方法,等下会调用,引用命名空间
using System.Data;
using System.Data.SqlClient;
class SqlHelper
{
public static readonly string connStr = "Data Source=.;Initial Catalog=MySchoolMoreData;Integrated Security=True";
/// <summary>
/// 三个参数得到数据结果集
/// </summary>
/// <param name="commandText">sql命令,存储过程名称</param>
/// <param name="ctype">命令字符串类型</param>
/// <param name="ps">参数数据 params关键词表示可为空</param>
/// <returns>返回DataTable的结果集</returns>
public static DataTable LoadData(string commandText,CommandType ctype,params SqlParameter[]ps)
{
SqlDataAdapter da = new SqlDataAdapter(commandText, connStr);
da.SelectCommand.Parameters.AddRange(ps);
da.SelectCommand.CommandType = ctype;
DataTable dt = new DataTable();
da.Fill(dt);
return dt; }
}
界面--一个DataGridView空间,两个Button
声明三个全局变量
int pageIndex = 1;//页码
int pageCount = 5; //每一页显示多少个数据
int count = 0;//接收输出参数的
private void Form1_Load(object sender, EventArgs e)界面初始之后的事件
SqlParameter p=new SqlParameter("@count",SqlDbType.Int);//@count 输出参数,和存储过程必须同名。声明类型
p.Direction = ParameterDirection.Output; //告诉服务器的参数输出方向
//调用方法静态
DataTable dt = SqlHelper.LoadData("usp_getPage1", CommandType.StoredProcedure, p);
dgvData.DataSource = dt;//绑定数据
this.count = (int)p.Value;//记录第几页
在上一页按钮写的事件
private void btnPer_Click(object sender, EventArgs e)
{ //当页码为1的时候将return
if (pageIndex==1)
{
MessageBox.Show("已经是第一页了");
return;
}
pageIndex--;//点击上一次页码减去1
//声明参数数组,参数必须和存储过程声明的变量名称一致
SqlParameter []ps={new SqlParameter("@countIndex",pageIndex),
new SqlParameter("@countPage",pageCount),
new SqlParameter("@count",SqlDbType.Int)
};
//设置输出参数的方向
ps[2].Direction = ParameterDirection.Output;
DataTable dt = SqlHelper.LoadData("usp_getPage1", CommandType.StoredProcedure, ps);//CommandType.StoredProcedure 告诉服务器这是执行一个存储过程不是sql语句
dgvData.DataSource = dt;//绑定数据
this.count = (int)ps[2].Value;//记录第几页 }
然后在下一页按钮的事件代码和上一页是差不多的只是变量pageIndex是pageIndex++代码如下:
private void btnNext_Click(object sender, EventArgs e)
{
if (pageIndex==this.count)//不同处
{
MessageBox.Show("已经是最后一页了");
return;
}
pageIndex++;//点击下一次页码加1--不同处
//声明参数数组
SqlParameter[] ps ={new SqlParameter("@countIndex",pageIndex),
new SqlParameter("@countPage",pageCount),
new SqlParameter("@count",SqlDbType.Int)
};
//设置输出参数的方向
ps[2].Direction = ParameterDirection.Output;
DataTable dt = SqlHelper.LoadData("usp_getPage1", CommandType.StoredProcedure, ps);
dgvData.DataSource = dt;//绑定数据
this.count = (int)ps[2].Value;//每一次都要记录输出的值
}
总结:1.存储过程的正确创建很重要
2.参数的名称一定要和存储过程的变量名称要一致
3.输出参数一定要声明,同时设置他的方向。
4.CommandType.StoredProcedure 的设置
创建有输出参数的存储过程并在c#中实现DataGridView分页功能的更多相关文章
- C# 调用带输入输出参数的存储过程
//调用存储过程执行类似于2//select count(*) from userinfo where username=username and pwd=pwd and grade=grade3// ...
- 用exec调用带有output输出参数的存储过程
用exec调用带有output输出参数的存储过程,想要得到输出参数的值,必须在调用参数后面加output关键字,如: declare @value int exec up_test 2,3,@v ...
- sqlserver 带输出参数的存储过程的创建与执行
创建 use StudentManager go if exists(select * from sysobjects where name='usp_ScoreQuery4') drop proce ...
- Oracle带输入输出参数的存储过程
(一)使用输入参数 需求:在emp_copy中添加一条记录,empno为已有empno的最大值+1,ename不能为空且长度必须大于0,deptno为60. 创建存储过程: create or rep ...
- c#调用带输出参数的存储过程
sql server中编写一个存储过程: CREATE PROCEDURE ProGetPWD @username varchar(20), @password varchar(20) OUTPUT ...
- ExecuteReader在执行有输出参数的存储过程时拿不到输出参数
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 后期会在博客首发更新:http://dnt.dkill.net/Article/D ...
- sqlserver 带输出参数的存储过程
--创建存储过程create procedure proc_stu@sname varchar(20),@pwd varchar(50),@flag bit outputasif exists(sel ...
- C#调用存储过程带输出参数或返回值
CREATE PROCEDURE [dbo].[GetNameById] @studentid varchar(8), @studentname nvarchar(50) OUTPUT AS BEGI ...
- [转] ADO.NET调用存储过程带输出参数或返回值
CREATE PROCEDURE [dbo].[GetNameById] @studentid varchar(), @studentname nvarchar() OUTPUT AS BEGIN S ...
随机推荐
- 进击的菜鸟问题1(设置checkbox全选)
问题:设置页面标签属性时,常常使用jquery.attr("","");在设置checkbox属性时,会出现操作设置checkbox属性后,无法清除,导致第二次 ...
- R语言系列:数据的基本运算
基本运算符号 1.基本数学计算 +.-.*./.^.%%(求模).%/%(整除) 注意:求模运算两边若为小数,则整数和小数部分分别求模.例:5.6%%2.2 2.比较运算 >.< ...
- android studio在windows上设置git/ssh
windows果然是与众不同的,凡事都要那么麻烦一点点(当然..是对程序员来说..) 一开始,我想用cygwin里的git,就省得我再多装一套软件,配置也可以统一,但事实证明不行 在android s ...
- 【Qt官方例程学习笔记】Getting Started Programming with Qt Widgets
创建一个QApplication对象,用于管理应用程序资源,它对于任何使用了Qt Widgets的程序都必要的.对于没有使用Qt Widgets 的GUI应用,可以使用QGuiApplication代 ...
- C# 关于跨线程访问控件问题
跨线程访问控件问题的原因是:控件都是在主线程中创建的,而系统默认控件的修改权归其创建线程所有.在子线程中如果需要直接修改控件的内容,需要使用委托机制将控件的修改操作交给主线程处理.因此,当没有使用委托 ...
- ASP.NET MVC 小牛之旅3:Routing——网址路由
网址路由(Routing)在ASP.NET MVC中有两个主要用途,一个用途是匹配通过浏览器传来的HTTP请求,另一个用途则是响应适当的网址给浏览器. 3.1匹配通过浏览器传来的HTTP请求 首先我们 ...
- Ajax原生请求及Json基础
1.基本结构 <script type="text/javascript"> // 创建XMLHttpRequest对象 var request = new XMLHt ...
- 解读人:李琼,Metabolic profiling of tumors, sera and skeletal muscles from an orthotopic murine model of gastric cancer associated-cachexia(胃癌相关恶病质的原位小鼠模型中肿瘤,血清和骨骼肌的代谢谱分析)
发表时间:(2019年4月) IF:3.950 单位: 厦门大学 厦门理工大学 物种:小鼠的肿瘤,血清和骨骼肌 技术:核磁共振代谢组学分析 一. 概述:(用精炼的语言描述文章的整体思路及结果) 本研究 ...
- Activity有四种加载模式:standard(默认), singleTop, singleTask和 singleInstance
standard:Activity的默认加载方法,即使某个Activity在Task栈中已经存在,另一个activity通过Intent跳转到该activity,同样会新创建一个实例压入栈中.例如:现 ...
- Click: 命令行工具神器
Click是一个Python用来快速实现命令行应用程序的包,主要优势表现在以下三点: 任意嵌套命令 自动生成帮助页 自动运行时lazy加载子命令 示例程序: import click @click.c ...