C#调用SQL Server分页存储过程
生活随笔
收集整理的這篇文章主要介紹了
C#调用SQL Server分页存储过程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以SQL Server2012提供的offset ..rows fetch next ..rows only為例
e.g.
表名:Tab1 ---------------------------------- ID Name 1 tblAttributeGroupDetail 2 tblAttributeGroup 3 tblAttribute ....... 50 tblBRItemTypeAppliesTo 51 tblBRItemProperties 52 tblBRItem 53 tblBRBusinessRule 54 Test--創建分頁存儲過程?rTabByCondition
USE [ExampleDB] GO if OBJECT_ID('rTabByCondition','P') is not null drop procedure rTabByCondition GO create procedure [dbo].[rTabByCondition]( @PageCount int=1 --頁數 ,@PageSize int=10 --頁顯示記錄數 ,@Rowcount int=0 output --總記錄數 ) as set nocount on; declare @Rows int; select * from dbo.Tab1 order by ID offset (@PageCount-1)*@PageSize rows fetch next @PageSize rows only set @Rows=@@ROWCOUNT select @Rowcount=count(*) from dbo.Tab1; return @Rows go declare @i int,@j int exec @i=[rTabByCondition] @PageCount=6,@PageSize=10,@Rowcount=@j output select @i as "@Rowcount",@j as "Return_Value" go顯示結果:
--打開Visual Studio—創建項目—選擇【控制臺應用程序】
#region Directives using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient; #endregionnamespace SQLStoredProcedure2 {class Program{static void Main(string[] args){SqlConnection thisConnection = new SqlConnection(@"Server=(Local)\SQL16;Integrated Security=True;Database=ExampleDB");thisConnection.Open();SqlCommand thisCommend = thisConnection.CreateCommand();thisCommend.CommandType = CommandType.StoredProcedure;thisCommend.CommandText = "rTabByCondition";thisCommend.Parameters.AddWithValue("@PageCount", "6");//頁數thisCommend.Parameters.AddWithValue("@PageSize", "10");//頁顯示記錄數SqlParameter paraOut = thisCommend.Parameters.Add("@Rowcount", SqlDbType.Int);//輸出參數定義paraOut.Direction = ParameterDirection.Output;SqlParameter paraRet = thisCommend.Parameters.Add("return_value", SqlDbType.Int);//返回值paraRet.Direction = ParameterDirection.ReturnValue;SqlDataReader thisReader = thisCommend.ExecuteReader();while (thisReader.Read()){Console.WriteLine("ID:{0}\tName:{1}", thisReader[0], thisReader[1]);}thisReader.Close();thisConnection.Close();Console.WriteLine("Rows:{0};\tReturn_Value:{1};", paraOut.Value, paraRet.Value);Console.WriteLine("Program finished,press Enter/Return to continue:");Console.ReadLine();}} }顯示效果:
?
轉載于:https://www.cnblogs.com/Roy_88/p/5463032.html
總結
以上是生活随笔為你收集整理的C#调用SQL Server分页存储过程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Beta Roun
- 下一篇: C# 之 6.0 新特性