博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
创建有输出参数的存储过程并在c#中实现DataGridView分页功能
阅读量:4620 次
发布时间:2019-06-09

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

不足之处,欢迎指正!

 

 

创建有输出参数的存储过程

 

if exists(select * from sysobjects where name='usp_getPage1')drop procedure usp_getPage1gocreate 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 intexecute usp_getPage1 @count=@num outputprint @num

 

 

  创建一个类,里面有一个静态方法,等下会调用,引用命名空间

using System.Data;

using System.Data.SqlClient;

class SqlHelper    {        public static readonly string connStr = "Data Source=.;Initial Catalog=MySchoolMoreData;Integrated Security=True";        ///         /// 三个参数得到数据结果集        ///         /// sql命令,存储过程名称        /// 命令字符串类型        /// 参数数据 params关键词表示可为空        /// 
返回DataTable的结果集
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 的设置

 

转载于:https://www.cnblogs.com/aguan/p/3917190.html

你可能感兴趣的文章
Demon_背包系统(实现装备栏,背包栏,可以切换装备)
查看>>
记录:一次数据库被恶意修改配置文件的问题
查看>>
redis 持久化
查看>>
http协议详解
查看>>
解决Jupyter notebook[import tensorflow as tf]报错
查看>>
Windows平台下使用ffmpeg和segmenter实现m3u8直播点播
查看>>
python网络画图——networkX
查看>>
ubuntu16.04文件形式安装mongodb
查看>>
SpringBoot------ActiveMQ安装
查看>>
详细了解 int? 类型
查看>>
纯js事件注册方法(解决兼容性)
查看>>
字符串匹配 ?kmp : hash
查看>>
mongod.service: control process exited, code=exited status=1
查看>>
vue npm 安装
查看>>
大照片背景在网页设计中应用的精美作品范例(下篇)
查看>>
c# 发送邮件、附件 分类: C# 2014-12-...
查看>>
对360来说,江湖上再无“搜狗”这个传说
查看>>
composer
查看>>
OpenCV特征点检测——ORB特征
查看>>
mysql的csv数据导入与导出
查看>>