ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解
一、分頁(yè)存儲(chǔ)過(guò)程
----------使用存儲(chǔ)過(guò)程編寫一個(gè)分頁(yè)查詢----------------------- set nocount off --關(guān)閉SqlServer消息 --set nocount on --開(kāi)啟SqlServer消息 go create proc usp_getMyStudentsDataByPage --輸入?yún)?shù) @pagesize int=7,--每頁(yè)記錄條數(shù) @pageindex int=1,--當(dāng)前要查看第幾頁(yè)的記錄 --輸出參數(shù) @recordcount int output,--總的記錄的條數(shù) @pagecount int output --總的頁(yè)數(shù) as begin --1.編寫查詢語(yǔ)句,把用戶要的數(shù)據(jù)查詢出來(lái) 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.計(jì)算總的記錄條數(shù) set @recordcount=(select count(*) from MyStudent) --3.計(jì)算總頁(yè)數(shù) set @pagecount=ceiling(@recordcount*1.0/@pagesize) end --調(diào)用前定義輸出參數(shù) declare @rc int,@pc int exec usp_getMyStudentsDataByPage @pagesize=7,@pageindex=4, @recordcount=@rc output,@pagecount=@pc output print @rc print @pc
二、ADO調(diào)用存儲(chǔ)過(guò)程
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通過(guò)Ado.Net調(diào)用存儲(chǔ)過(guò)程 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int pageIndex = 1;//當(dāng)前要查看的頁(yè)碼 private int pageSize = 7;//每頁(yè)顯示的記錄條數(shù) private int pageCount;//總頁(yè)數(shù) private int recordCount;//總條數(shù) //窗體加載的時(shí)候顯示第一頁(yè)的數(shù)據(jù) private void Form1_Load(object sender, EventArgs e) { LoadData(); } private void LoadData() { //根據(jù)pageIndex來(lái)加載數(shù)據(jù) string constr = "Data Source=steve-pc;Initial Catalog=itcast2014;Integrated Security=True"; #region 1 //using (SqlConnection conn = new SqlConnection(constr)) //{ // //將sql語(yǔ)句變成存儲(chǔ)過(guò)程名稱 // string sql = "usp_getMyStudentsDataByPage"; // using (SqlCommand cmd = new SqlCommand(sql, conn)) // { // //告訴SqlCommand對(duì)象,現(xiàn)在執(zhí)行的存儲(chǔ)過(guò)程不是SQL語(yǔ)句 // cmd.CommandType = CommandType.StoredProcedure; // //增加參數(shù)(存儲(chǔ)過(guò)程中有幾個(gè)參數(shù),這里就需要增加幾個(gè)參數(shù)) // //@pagesize int=7,--每頁(yè)記錄條數(shù) // //@pageindex int=1,--當(dāng)前要查看第幾頁(yè)的記錄 // //@recordcount int output,--總的記錄的條數(shù) // //@pagecount int output --總的頁(yè)數(shù) // 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); // //打開(kāi)連接 // conn.Open(); // //執(zhí)行 //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); //獲取輸出參數(shù)并且賦值給label label1.Text = "總條數(shù):" + pms[2].Value.ToString(); label2.Text = "總頁(yè)數(shù):" + pms[3].Value.ToString(); label3.Text = "當(dāng)前頁(yè):" + pageIndex; //數(shù)據(jù)綁定 this.dataGridView1.DataSource = dt; } } //下一頁(yè) private void button2_Click(object sender, EventArgs e) { pageIndex++; LoadData(); } //上一頁(yè) private void button1_Click(object sender, EventArgs e) { pageIndex--; LoadData(); } } }
效果圖:
三、通過(guò)ado.net調(diào)用存儲(chǔ)過(guò)程與調(diào)用帶參數(shù)的SQL語(yǔ)句的區(qū)別。
1>把SQL語(yǔ)句變成了存儲(chǔ)過(guò)程名稱
2>設(shè)置SqlCommand對(duì)象的CommandType為CommandType.StoredProcedure
這步本質(zhì) 就是在 存儲(chǔ)過(guò)程名稱前面加了個(gè)“ exec ”
3>根據(jù)存儲(chǔ)過(guò)程的參數(shù)來(lái)設(shè)置SqlCommand對(duì)象的參數(shù)。
4>如果有輸出參數(shù)需要設(shè)置輸出參數(shù)的Direction屬性為:Direction=ParameterDirection.Output
四、如果是通過(guò)調(diào)用Command對(duì)象的ExecuteReader()方法來(lái)執(zhí)行的該存儲(chǔ)過(guò)程,那么要想獲取輸出參數(shù),必須得等到關(guān)閉reader對(duì)象后,才能獲取輸出參數(shù)。
以上這篇ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Repeater控件動(dòng)態(tài)變更列(Header,Item和Foot)信息實(shí)現(xiàn)思路
需求開(kāi)發(fā)一個(gè)小報(bào)表,顯示最近五個(gè)月的summary的數(shù)量統(tǒng)計(jì),報(bào)表會(huì)隨月份的變化而變化,接下來(lái)為大家詳細(xì)介紹下實(shí)現(xiàn)方法,感興趣的各位不要錯(cuò)過(guò)了哈2013-03-03把jQuery的each(callback)方法移植到c#中
jQuery中使用each(callback)方法可以很方便的遍歷集合,如2008-03-03Asp.Net Core Identity 隱私數(shù)據(jù)保護(hù)的實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net Core Identity 隱私數(shù)據(jù)保護(hù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01ASP.NET實(shí)現(xiàn)基于Forms認(rèn)證的WebService應(yīng)用實(shí)例
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)基于Forms認(rèn)證的WebService應(yīng)用,實(shí)例分析了使用Forms進(jìn)行WebService身份認(rèn)證的相關(guān)技巧與實(shí)現(xiàn)方法,需要的朋友可以參考下2015-05-05Coolite Cool Study 1 在Grid中用ComboBox 來(lái)編輯數(shù)據(jù)
作為Coolite的第一個(gè)教程,我想展現(xiàn)給大家能夠體現(xiàn)Coolite強(qiáng)大的例子(當(dāng)然也比官方例子稍微復(fù)雜一點(diǎn))。2009-05-05如何在ASP.NET Core應(yīng)用程序運(yùn)行Vue并且部署在IIS上詳解
這篇文章主要給大家介紹了關(guān)于如何運(yùn)行Vue在ASP.NET Core應(yīng)用程序并且部署在IIS上的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10ASP.NET動(dòng)態(tài)加載用戶控件的實(shí)現(xiàn)方法
動(dòng)態(tài)加載用戶控件的方法,用asp.net的朋友推薦2008-10-10