ADO調用分頁查詢存儲過程


 

一、分頁存儲過程

----------使用存儲過程編寫一個分頁查詢-----------------------
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)  --乘以1.0轉成flot型, 然后celling “進一法”取值
end

 

--調用前定義輸出參數
declare @rc int,@pc int
exec usp_getMyStudentsDataByPage @pagesize=7,@pageindex=4, @recordcount=@rc output,@pagecount=@pc output
print @rc
print @pc

 

 

二、ADO調用存儲過程

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();
        }
    }
}

 

 

 

 

效果圖:

 

三.通過ado.net調用存儲過程與調用帶參數的SQL語句的區別。
1>把SQL語句變成了存儲過程名稱
2>設置SqlCommand對象的CommandType為CommandType.StoredProcedure

這步本質 就是在 存儲過程名稱前面加了個“ exec  ”
3>根據存儲過程的參數來設置SqlCommand對象的參數。
4>如果有輸出參數需要設置輸出參數的Direction屬性為:Direction=ParameterDirection.Output


四.如果是通過調用Command對象的ExecuteReader()方法來執行的該存儲過程,那么要想獲取輸出參數,必須得等到關閉reader對象后,才能獲取輸出參數。

 

來源:傳智播客 視頻教程。

ROW_NUMBER() OVER函數的基本用法

https://jingyan.baidu.com/article/9989c74604a644f648ecfef3.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM