Home  >  Article  >  Backend Development  >  Example explanation of ADO calling paging query stored procedure_Practical tips

Example explanation of ADO calling paging query stored procedure_Practical tips

韦小宝
韦小宝Original
2017-12-15 14:37:332036browse

The editor below will share with you an ADO call paging querystored procedureexample explanation, which has a good reference value. I hope it will be helpful to everyone and let everyone know more about it. Good use ADO for paging. If you are interested in ADO, please follow the editor to take a look

1. Paging stored procedure


----------使用存储过程编写一个分页查询-----------------------
set nocount off --关闭SqlServer消息
--set nocount on --开启SqlServer消息
go
create proc usp_getMyStudentsDataByPage
--输入参数
@pagesize int=7,--每页记录条数
@pageindex int=1,--当前要查看第几页的记录
--输出参数
@recordcount int output,--总的记录的条数
@pagecount int output --总的页数
as
begin
--1.编写查询语句,把用户要的数据查询出来
select
t.fid,
t.fname,
t.fage,
t.fgender,
t.fmath,
t.fclassid,
t.fbirthday
from (select *,rn=row_number() over(order by fid asc) from MyStudent) as t
where t.rn between (@pageindex-1)*@pagesize+1 and @pagesize*@pageindex
--2.计算总的记录条数
set @recordcount=(select count(*) from MyStudent)
--3.计算总页数
set @pagecount=ceiling(@recordcount*1.0/@pagesize)
end
 
--调用前定义输出参数
declare @rc int,@pc int
exec usp_getMyStudentsDataByPage @pagesize=7,@pageindex=4, @recordcount=@rc output,@pagecount=@pc output
print @rc
print @pc



##2. ADO calls the stored procedure


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _02通过Ado.Net调用存储过程
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  private int pageIndex = 1;//当前要查看的页码
  private int pageSize = 7;//每页显示的记录条数

  private int pageCount;//总页数
  private int recordCount;//总条数
  //窗体加载的时候显示第一页的数据
  private void Form1_Load(object sender, EventArgs e)
  {
   LoadData();
  }
  private void LoadData()
  {
   //根据pageIndex来加载数据
   string constr = "Data Source=steve-pc;Initial Catalog=itcast2014;Integrated Security=True";
   #region 1
   //using (SqlConnection conn = new SqlConnection(constr))
   //{
   // //将sql语句变成存储过程名称
   // string sql = "usp_getMyStudentsDataByPage";
   // using (SqlCommand cmd = new SqlCommand(sql, conn))
   // {
   //  //告诉SqlCommand对象,现在执行的存储过程不是SQL语句
   //  cmd.CommandType = CommandType.StoredProcedure;
   //  //增加参数(存储过程中有几个参数,这里就需要增加几个参数)
   //  //@pagesize int=7,--每页记录条数
   //  //@pageindex int=1,--当前要查看第几页的记录
   //  //@recordcount int output,--总的记录的条数
   //  //@pagecount int output --总的页数
   //  SqlParameter[] pms = new SqlParameter[] { 
   //  new SqlParameter("@pagesize",SqlDbType.Int){Value =pageSize},
   //  new SqlParameter("@pageindex",SqlDbType.Int){Value =pageIndex},
   //  new SqlParameter("@recordcount",SqlDbType.Int){ Direction=ParameterDirection.Output},
   //  new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
   //  };
   //  cmd.Parameters.AddRange(pms);
   //  //打开连接
   //  conn.Open();
   //  //执行
   //using(SqlDataReader reader=cmd.ExecuteReader())
   //{
    //reader.Read()
   //}
   //pms[2].Value
   // }
   //}
   #endregion
   //DataAdapter方式
   DataTable dt = new DataTable();
   using (SqlDataAdapter adapter = new SqlDataAdapter("usp_getMyStudentsDataByPage", constr))
   {
    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
    SqlParameter[] pms = new SqlParameter[] { 
     new SqlParameter("@pagesize",SqlDbType.Int){Value =pageSize},
     new SqlParameter("@pageindex",SqlDbType.Int){Value =pageIndex},
     new SqlParameter("@recordcount",SqlDbType.Int){ Direction=ParameterDirection.Output},
     new SqlParameter("@pagecount",SqlDbType.Int){Direction=ParameterDirection.Output}
     };
    adapter.SelectCommand.Parameters.AddRange(pms);
    adapter.Fill(dt);
    //获取输出参数并且赋值给label
    label1.Text = "总条数:" + pms[2].Value.ToString();
    label2.Text = "总页数:" + pms[3].Value.ToString();
    label3.Text = "当前页:" + pageIndex;
    //数据绑定
    this.dataGridView1.DataSource = dt;
   }
  }
  //下一页
  private void button2_Click(object sender, EventArgs e)
  {
   pageIndex++;
   LoadData();
  }
  //上一页
  private void button1_Click(object sender, EventArgs e)
  {
   pageIndex--;
   LoadData();
  }
 }
}


Rendering:

3. Call through ado.net The difference between stored procedures and calling SQL statements with parameters.

1> Turn the SQL statement into a stored procedure name

2> Set the CommandType of the SqlCommand object to CommandType.StoredProcedure

The essence of this step is Add "exec" in front of the stored procedure name

3>Set the parameters of the SqlCommand object according to the parameters of the stored procedure.

4>If there are output parameters, you need to set the Direction property of the output parameters as: Direction=ParameterDirection.Output

4. If you call ExecuteReader() of the Command object method to execute the stored procedure, then if you want to obtain the output parameters, you must wait until the reader object is closed before you can obtain the output parameters.

The above example explanation of ADO calling paging query stored procedure is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website. .

Related recommendations:

Analysis of detailed explanation of ADO.NET execution of add, delete, modify and query operations on SQL Server database

ADO.NET Practical Example Introduction

ADO.NET Implementation Tutorial for Operation of SQL Server Database

The above is the detailed content of Example explanation of ADO calling paging query stored procedure_Practical tips. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn