不足之處,歡迎指正!
創建有輸出參數的存儲過程
if exists(select * from sysobjects where name='usp_getPage1')drop PRocedure usp_getPage1gocreate procedure usp_getPage1--存儲過程名稱@count int output,--輸出參數@countIndex int=1,--參數帶默認值@countPage int=5--參數帶默認值as --一個sql語句。ROW_NUMBER ( ) OVER ()返回結果集分區內行的序列號,每個分區的第一行從 1 開始,這里給別名id。temp也是一個別名,是前面括號的數據select * from(select row_number()over(order by studentno)id,* from student)temp where id >(@countIndex-1)*@countPage and id<=@countIndex*@countPage--給輸出參數賦值set @count=ceiling((select count(*) from student)*1.0/@countPage) --ceiling四舍五入的節奏,乘以1.0呢,是因為int除以int還是得到int。浮點型除以整型得到浮點型go--執行存儲過程,有默認值的可以不給值,但是輸出參數需要聲明一個變量接收declare @num intexecute usp_getPage1 @count=@num outputprint @num
創建一個類,里面有一個靜態方法,等下會調用,引用命名空間
using System.Data;using System.Data.SqlClient;
class SqlHelper { public static readonly string connStr = "Data Source=.;Initial Catalog=MySchoolMoreData;Integrated Security=True"; /// <summary> /// 三個參數得到數據結果集 /// </summary> /// <param name="commandText">sql命令,存儲過程名稱</param> /// <param name="ctype">命令字符串類型</param> /// <param name="ps">參數數據 params關鍵詞表示可為空</param> /// <returns>返回DataTable的結果集</returns> public static DataTable LoadData(string commandText,CommandType ctype,params SqlParameter[]ps) { SqlDataAdapter da = new SqlDataAdapter(commandText, connStr); da.SelectCommand.Parameters.AddRange(ps); da.SelectCommand.CommandType = ctype; DataTable dt = new DataTable(); da.Fill(dt); return dt; } }
界面--一個DataGridView空間,兩個Button
聲明三個全局變量
int pageIndex = 1;//頁碼 int pageCount = 5; //每一頁顯示多少個數據 int count = 0;//接收輸出參數的
private void Form1_Load(object sender, EventArgs e)界面初始之后的事件
SqlParameter p=new SqlParameter("@count",SqlDbType.Int);//@count 輸出參數,和存儲過程必須同名。聲明類型
p.Direction = ParameterDirection.Output; //告訴服務器的參數輸出方向//調用方法靜態 DataTable dt = SqlHelper.LoadData("usp_getPage1", CommandType.StoredProcedure, p); dgvData.DataSource = dt;//綁定數據 this.count = (int)p.Value;//記錄第幾頁
在上一頁按鈕寫的事件
private void btnPer_Click(object sender, EventArgs e) { //當頁碼為1的時候將return if (pageIndex==1) { MessageBox.Show("已經是第一頁了"); return; } pageIndex--;//點擊上一次頁碼減去1 //聲明參數數組,參數必須和存儲過程聲明的變量名稱一致 SqlParameter []ps={new SqlParameter("@countIndex",pageIndex), new SqlParameter("@countPage",pageCount), new SqlParameter("@count",SqlDbType.Int) }; //設置輸出參數的方向 ps[2].Direction = ParameterDirection.Output; DataTable dt = SqlHelper.LoadData("usp_getPage1", CommandType.StoredProcedure, ps);//CommandType.StoredProcedure 告訴服務器這是執行一個存儲過程不是sql語句
dgvData.DataSource = dt;//綁定數據 this.count = (int)ps[2].Value;//記錄第幾頁 }
然后在下一頁按鈕的事件代碼和上一頁是差不多的只是變量pageIndex是pageIndex++代碼如下:
private void btnNext_Click(object sender, EventArgs e) { if (pageIndex==this.count)//不同處 { MessageBox.Show("已經是最后一頁了"); return; } pageIndex++;//點擊下一次頁碼加1--不同處 //聲明參數數組 SqlParameter[] ps ={new SqlParameter("@countIndex",pageIndex), new SqlParameter("@countPage",pageCount), new SqlParameter("@count",SqlDbType.Int) }; //設置輸出參數的方向 ps[2].Direction = ParameterDirection.Output; DataTable dt = SqlHelper.LoadData("usp_getPage1", CommandType.StoredProcedure, ps); dgvData.DataSource = dt;//綁定數據 this.count = (int)ps[2].Value;//每一次都要記錄輸出的值 }
總結:1.存儲過程的正確創建很重要
2.參數的名稱一定要和存儲過程的變量名稱要一致
3.輸出參數一定要聲明,同時設置他的方向。
4.CommandType.StoredProcedure 的設置
新聞熱點
疑難解答