小編給大家分享一下ADO如何調(diào)用分頁查詢存儲(chǔ)過程,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站制作、成都做網(wǎng)站與策劃設(shè)計(jì),曲周網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:曲周等地區(qū)。曲周做網(wǎng)站價(jià)格咨詢:18982081108
一、分頁存儲(chǔ)過程
----------使用存儲(chǔ)過程編寫一個(gè)分頁查詢----------------------- set nocount off --關(guān)閉SqlServer消息 --set nocount on --開啟SqlServer消息 go create proc usp_getMyStudentsDataByPage --輸入?yún)?shù) @pagesize int=7,--每頁記錄條數(shù) @pageindex int=1,--當(dāng)前要查看第幾頁的記錄 --輸出參數(shù) @recordcount int output,--總的記錄的條數(shù) @pagecount int output --總的頁數(shù) as begin --1.編寫查詢語句,把用戶要的數(shù)據(jù)查詢出來 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ì)算總頁數(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ǔ)過程
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調(diào)用存儲(chǔ)過程
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int pageIndex = 1;//當(dāng)前要查看的頁碼
private int pageSize = 7;//每頁顯示的記錄條數(shù)
private int pageCount;//總頁數(shù)
private int recordCount;//總條數(shù)
//窗體加載的時(shí)候顯示第一頁的數(shù)據(jù)
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
//根據(jù)pageIndex來加載數(shù)據(jù)
string constr = "Data Source=steve-pc;Initial Catalog=itcast2014;Integrated Security=True";
#region 1
//using (SqlConnection conn = new SqlConnection(constr))
//{
// //將sql語句變成存儲(chǔ)過程名稱
// string sql = "usp_getMyStudentsDataByPage";
// using (SqlCommand cmd = new SqlCommand(sql, conn))
// {
// //告訴SqlCommand對(duì)象,現(xiàn)在執(zhí)行的存儲(chǔ)過程不是SQL語句
// cmd.CommandType = CommandType.StoredProcedure;
// //增加參數(shù)(存儲(chǔ)過程中有幾個(gè)參數(shù),這里就需要增加幾個(gè)參數(shù))
// //@pagesize int=7,--每頁記錄條數(shù)
// //@pageindex int=1,--當(dāng)前要查看第幾頁的記錄
// //@recordcount int output,--總的記錄的條數(shù)
// //@pagecount int output --總的頁數(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);
// //打開連接
// 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 = "總頁數(shù):" + pms[3].Value.ToString();
label3.Text = "當(dāng)前頁:" + pageIndex;
//數(shù)據(jù)綁定
this.dataGridView1.DataSource = dt;
}
}
//下一頁
private void button2_Click(object sender, EventArgs e)
{
pageIndex++;
LoadData();
}
//上一頁
private void button1_Click(object sender, EventArgs e)
{
pageIndex--;
LoadData();
}
}
}效果圖:

三、通過ado.net調(diào)用存儲(chǔ)過程與調(diào)用帶參數(shù)的SQL語句的區(qū)別。
1>把SQL語句變成了存儲(chǔ)過程名稱
2>設(shè)置SqlCommand對(duì)象的CommandType為CommandType.StoredProcedure
這步本質(zhì) 就是在 存儲(chǔ)過程名稱前面加了個(gè)“ exec ”
3>根據(jù)存儲(chǔ)過程的參數(shù)來設(shè)置SqlCommand對(duì)象的參數(shù)。
4>如果有輸出參數(shù)需要設(shè)置輸出參數(shù)的Direction屬性為:Direction=ParameterDirection.Output
四、如果是通過調(diào)用Command對(duì)象的ExecuteReader()方法來執(zhí)行的該存儲(chǔ)過程,那么要想獲取輸出參數(shù),必須得等到關(guān)閉reader對(duì)象后,才能獲取輸出參數(shù)。
看完了這篇文章,相信你對(duì)ADO如何調(diào)用分頁查詢存儲(chǔ)過程有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁名稱:ADO如何調(diào)用分頁查詢存儲(chǔ)過程
本文地址:http://chinadenli.net/article0/ggjpoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、微信小程序、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈、網(wǎng)站排名、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)