Using system; using system.collections; using system.web.ui; using system.data; using system.data.sqlclient; using workstatic.database;
namespace ShareSoft.PageClass {///
Private statebag viewState = new statebag (); #Region Get or set up a database connection string private string _connection = string.empty; ///
#REGON Get or set the table name ///
#Region Get or sets the number of ///
#Region whether the total record number ////
#Region Get or set the primary key field ///
#Region Get or set Query Condition ///
#Region desired page DataView private DataView _PageDataView = null; desired ///
#Region Set the total page number ///
#REGON Piente Social Method ///
#Region Determine if you want to count the total record number count; if (iScount == counttype.no) {if (curpage == 1) {mustcount = counttype.yes;} else {mustcount = countType.no;}} else {Mustcount = Counttype.yes;} # endregion # region parameter sqlparameter [] ps = new sqlparameter [9]; ps [0] = new SQLParameter ("@ TableName", SqldbType.varchar, 50); PS [0] .value = TableName ;
PS [1] = New SqlParameter ("@ FieldList", SqldbType.varchar, 255); PS [1] .value = fieldList;
PS [2] = New Sqlparameter ("@ Keyfield", SqldbType.varchar, 50, keyfield); PS [2] .value = keyfield;
PS [3] = New SQLParameter ("@ Pagesize", SqldbType.Int); PS [3] .value = Pagesize;
PS [4] = New SQLParameter ("@ pageindex", sqldbtype.int); PS [4] .value = CURPAGE
PS [5] = New SQLParameter ("@ iScount", SqldbType.bit); PS [5] .value = (int).
PS [6] = New SQLParameter ("@ OrdeeExpression", SqldbType.varchar, 100); PS [6] .value = OrdereExpression;
PS [7] = New SqlParameter ("@ conditioneexpression", sqldbtype.varchar, 500); ps [7] .value = conditioneexpression;
PS [8] = New SqlParameter ("@ recordcount", sqldbtype.int); PS [8] .value = RecordCount;
#endregion DataSet ds = SqlHelper.ExecuteDataset (Link, CommandType.StoredProcedure, "up_Page_Result", ps); if (! ds == null || ds.Tables.Count = 2) {PageDataView = null; return;} else {RecordCount = Convert.Toint32 (DS.Tables [0] .Rows [0] [0]); if (MustCount == countType.yes) {setPageCount () {setPageCount ();} if (curpage> 1 && keyfield! = ") {Ix (Fieldlist) .Indexof ("*") == - 1) {string [] arrsortfield = OrdeeExpression.Replace ("DESC", ""). Replace ("ASC", "). Split (','); foreach (String SortField in ArrSortField) {if (FieldList.IndexOf (SortField.Trim ()) == - 1) {PageDataView = null; return;}}} ds.Tables [1] .DefaultView.Sort = OrderEexpression;} PageDataView = ds. Tables [1] .defaultview;} # endregion # region release object ///
Stored Procedure - Gets Data Create Procedure DBo.up_page_result @tablename VARCHAR (50), - Table Name @fieldlist Varchar (255), - To display the name @keyfield varchar (50) = ', - - Home key field name @Pagesize int = 10, - Page size @PageIndex int = 1, - Total number of records @iscount bit = 0, - Return to record total, non-0 value returns @ORDEREEXPRESSION VARCHAR (100), - - Sort field and method (Note: Do not add order by) @conditioneexpression varchar (500) = ', - Query Conditions (Note: Don't add where) @recordcount int = 0 - total record number AS
Declare @execsql nvarchar (2000) - primary sentence
Declare @ReverseReradeExpression varchar (100) - Sort Type
SET @ OrderEexpression = UPPER (@OrderEexpression) SET @ OrderEexpression = 'ORDER BY' @ OrderEexpressionSET @ ReverseOrderEexpression = REPLACE (@ OrderEexpression, 'DESC', '###') SET @ ReverseOrderEexpression = REPLACE (@ ReverseOrderEexpression, 'ASC' , 'DESC') SET @ ReverseOrderEexpression = REPLACE (@ReverseOrderEexpression, '###', 'ASC') IF @ConditionEexpression! = '' SET @ConditionEexpression = 'WHERE' @ConditionEexpressionELSE SET @ConditionEexpression = 'WHERE 1 = 1 '
--- Total record number if @iscount! = 0 or @ recordcount = 0 Begin if (@ Keyfield = '') set @Execsql = 'select @ recordcount = count (' '1' ') from [' @TableName ']' @ConditionEexpression ELSE SET @EXECSQL = 'SELECT @ RecordCount = COUNT ([' @ KeyField ']) FROM [' @TableName ']' @ConditionEexpression --PRINT @EXECSQL EXECUTE sp_executesql @ eXECSQL, N '@RecordCount int OUTPUT', @RecordCount OUTPUT SET @EXECSQL = 'SELECT' STR (@RecordCount) 'AS RecordCount' EXEC (@EXECSQL) ENDELSE BEGIN SET @EXECSQL = 'SELECT' STR (@RecordCount ) 'As recordcount' exec (@execsql) End
IF @PageIndex = 1 Begin Set @Execsql = 'SELECT TOP' STR (@PageSize) '' @ FieldList 'from' Char (13) '[' @tablename '] @ConditioneExpression ' ' @OrderEexpression ENDELSE BEGIN IF (@ KeyField =' ') BEGIN DECLARE @TopNum int IF (@ PageIndex = CEILING (@RecordCount * 1.0 / @PageSize) AND @RecordCount% @PageSize <> 0) SET @ TopNum = @RecordCount % @PageSize Else Set @ TopNum = @PageSize Set @Execsql = 'SELECT TOP' STR (@topnum) '' @ FieldList 'from' Char (13) '[' @tablename '] a Where [' @ Keyfield '] in (SELECT TOP ' STR (@ PageIndex * @ Pagesize) Char (13) ' [' @keyfield '] from [' @tablename '] ' @ConditioneExpression '' CHAR (13) @OrderEexpression ')' @ ReverseOrderEexpression CHAR (13) END ELSE BEGIN DECLARE @BeginIndex int, @EndIndex int DECLARE @NewField varchar (100) SET @NewField = '[NEW_AutoId]' SET @BEG ININDEX = @ Pagesize * (@PageIndex-1) set @ endindex = @ Pagesize * @ PageIndex-1
SET @ EXECSQL = 'SELECT' @ NewField '= IDENTITY (int, 0,1),' @ FieldList 'INTO #tb FROM' @ TableName '' '' @ ConditionEexpression '' @OrderEexpression 'SELECT ' @ FieldList ' from #tb where ' @ newfield ' between ' str (@beginIndex) ' and ' str (@endindex) ' Drop table #tb 'end endprint @Execsqlexec (@execsql) Go
aspx page <% @ Page language = "c #" Codebehind = "pagetest.aspx.cs" AutoEventWireup = "false" Inherits = "WorkStatic.pagetest"%> <% @ Register TagPrefix = "MyCtl" Namespace = "ShareSoft.PageClass" Assembly = "Workstatic"%>