


Example 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 name2> Set the CommandType of the SqlCommand object to CommandType.StoredProcedureThe essence of this step is Add "exec" in front of the stored procedure name3>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.Output4. 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

MySQL and PL/SQL are two different database management systems, representing the characteristics of relational databases and procedural languages respectively. This article will compare the similarities and differences between MySQL and PL/SQL, with specific code examples to illustrate. MySQL is a popular relational database management system that uses Structured Query Language (SQL) to manage and operate databases. PL/SQL is a procedural language unique to Oracle database and is used to write database objects such as stored procedures, triggers and functions. same

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

If you want to check the activation date using an Apple mobile phone, the best way is to check it through the serial number in the mobile phone. You can also check it by visiting Apple's official website, connecting it to a computer, and downloading third-party software to check it. How to check the activation date of Apple mobile phone Answer: Serial number query, Apple official website query, computer query, third-party software query 1. The best way for users is to know the serial number of their mobile phone. You can see the serial number by opening Settings, General, About This Machine. . 2. Using the serial number, you can not only know the activation date of your mobile phone, but also check the mobile phone version, mobile phone origin, mobile phone factory date, etc. 3. Users visit Apple's official website to find technical support, find the service and repair column at the bottom of the page, and check the iPhone activation information there. 4. User

Title: How to use Oracle to query whether a table is locked? In Oracle database, table lock means that when a transaction is performing a write operation on the table, other transactions will be blocked when they want to perform write operations on the table or make structural changes to the table (such as adding columns, deleting rows, etc.). In the actual development process, we often need to query whether the table is locked in order to better troubleshoot and deal with related problems. This article will introduce how to use Oracle statements to query whether a table is locked, and give specific code examples. To check whether the table is locked, we

Forum is one of the most common website forms on the Internet. It provides users with a platform to share information, exchange and discuss. Discuz is a commonly used forum program, and I believe many webmasters are already very familiar with it. During the development and management of the Discuz forum, it is often necessary to query the data in the database for analysis or processing. In this article, we will share some tips for querying the location of the Discuz database and provide specific code examples. First, we need to understand the database structure of Discuz
