search
HomeBackend DevelopmentC#.Net TutorialExample explanation of ADO calling paging query stored procedure_Practical tips

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
C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

Demystifying C# .NET: An Overview for BeginnersDemystifying C# .NET: An Overview for BeginnersApr 20, 2025 am 12:11 AM

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools