Two paging stored procedure codes currently used

Source: Internet
Author: User
Tags rowcount

First, get the total number of data rows CopyCode The Code is as follows: Set ansi_nulls on
Set quoted_identifier on
Go
Alter procedure [sq8reyoung]. [fenye_num]
(
@ Tablenames nvarchar (200 ),
@ Filter nvarchar (200 ))
As
If @ filter =''
Set @ filter = 'where 1 = 1'
Else
Set @ filter = 'where' + @ Filter
Execute ('select count (*) from '+ @ tablenames + ''+ @ filter)

Second, retrieve paging dataCopy code The Code is as follows: Set ansi_nulls on
Set quoted_identifier on
Go
Alter procedure [sq8reyoung]. [fenye]
@ Tablenames varchar (200), -- table name, which can be multiple tables but cannot be aliases
@ Primarykey varchar (100), -- primary key, which can be null, but this value cannot be blank when @ order is null
@ Fields varchar (200), -- the field to be retrieved. It can be a field of multiple tables. It can be null. If it is null, it indicates select *
@ Pagesize int, -- number of records per page
@ Currentpage int, -- current page, 0 indicates page 1st
@ Filter varchar (200) = '', -- condition, which can be null. Do not enter where
@ Group varchar (200) = '', -- group basis. It can be empty. You do not need to enter group
@ Order varchar (200) = ''-- sort. It can be null. If it is null, It is sorted by the primary key in ascending order by default. Order by is not required.
As
Begin
Declare @ sortcolumn varchar (200)
Declare @ operator char (2)
Declare @ sorttable varchar (200)
Declare @ sortname varchar (200)
If @ fields =''
Set @ fields = '*'
If @ filter =''
Set @ filter = 'where 1 = 1'
Else
Set @ filter = 'where' + @ Filter
If @ group <>''
Set @ group = 'group by' + @ Group
If @ order <>''
Begin
Declare @ pos1 int, @ pos2 int
Set @ order = Replace (replace (@ order, 'asc ', 'asc'), 'desc', 'desc ')
If charindex ('desc', @ order)> 0
If charindex ('asc ', @ order)> 0
Begin
If charindex ('desc', @ order) <charindex ('asc ', @ order)
Set @ operator = '<='
Else
Set @ operator = '> ='
End
Else
Set @ operator = '<='
Else
Set @ operator = '> ='
Set @ sortcolumn = Replace (replace (@ order, 'asc ', ''), 'desc ',''),'','')
Set @ pos1 = charindex (',', @ sortcolumn)
If @ pos1> 0
Set @ sortcolumn = substring (@sortcolumn, 1, @ pos1-1)
Set @ pos2 = charindex ('.', @ sortcolumn)
If @ pos2> 0
Begin
Set @ sorttable = substring (@sortcolumn, 1, @ pos2-1)
If @ pos1> 0
Set @ sortname = substring (@ sortcolumn, @ pos2 + 1, @ pos1-@ pos2-1)
Else
Set @ sortname = substring (@ sortcolumn, @ pos2 + 1, Len (@ sortcolumn)-@ pos2)
End
Else
Begin
Set @ sorttable = @ tablenames
Set @ sortname = @ sortcolumn
End
End
Else
Begin
Set @ sortcolumn = @ primarykey
Set @ sorttable = @ tablenames
Set @ sortname = @ sortcolumn
Set @ order = @ sortcolumn
Set @ operator = '> ='
End
Declare @ Type varchar (50)
Declare @ prec int
Select @ type = T. Name, @ prec = C. prec
From sysobjects o
Join syscolumns C on O. ID = C. ID
Join policypes t on C. xusertype = T. xusertype
Where o. Name = @ sorttable and C. Name = @ sortname
If charindex ('Char ', @ type)> 0
Set @ type = @ Type + '(' + Cast (@ prec as varchar) + ')'
Declare @ toprows int
Set @ toprows = @ pagesize * @ currentpage + 1
Print @ Type
Declare @ SQL nvarchar (4000)
Set @ SQL = 'Clare @ sortcolumnbegin '+ @ Type +'
Set rowcount '+ Cast (@ toprows as varchar (10) + 'select @ sortcolumnbegin =' +
@ Sortcolumn + 'from' + @ tablenames + ''+ @ filter +'' + @ group + 'ORDER BY' + @ order +'
Set rowcount '+ Cast (@ pagesize as varchar (10) +'
Select '+ @ fields + 'from' + @ tablenames + ''+ @ filter + 'and' + @ sortcolumn +'' + @ operator +' @ sortcolumnbegin '+ isnull (@ group, '') + 'ORDER BY' + @ order +''
-- Print (@ SQL)
Exec (@ SQL)
End

And the data operation class that implements this method Copy code The Code is as follows: using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data. sqlclient;
Using system. Data;
Using system. configuration;
Using Wuqi. webdiyer;
Using models;
Namespace dal
{
Public class dbhelper
{
Public static readonly string conn_string = configurationmanager. connectionstrings ["rymedicalconnectionstring"]. connectionstring;
Public static sqldatareader getreader (string safesql)
{
Sqlconnection conn = new sqlconnection (conn_string );
Sqlcommand cmd = new sqlcommand (safesql, Conn );
Sqldatareader reader = cmd. executereader (commandbehavior. closeconnection );
Reader. Close ();
Return reader;
}
Public static sqldatareader getreader (string SQL, Params sqlparameter [] values)
{
Sqlconnection conn = new sqlconnection (conn_string );
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Conn. open ();
Cmd. Parameters. addrange (values );
Sqldatareader reader = cmd. executereader (commandbehavior. closeconnection );
Reader. Close ();
Conn. Close ();
Return reader;
}
Public static datatable getdataset (string safesql)
{
Sqlconnection conn = new sqlconnection (conn_string );
Dataset DS = new dataset ();
Sqlcommand cmd = new sqlcommand (safesql, Conn );
Conn. open ();
Sqldataadapter da = new sqldataadapter (CMD );
Da. Fill (DS );
Conn. Close ();
Return Ds. Tables [0];
}
Public static datatable getdataset (commandtype parameter type, string parameter text, Params sqlparameter [] parameter parms)
{
Sqlconnection conn = new sqlconnection (conn_string );
Dataset DS = new dataset ();
Sqlcommand cmd = new sqlcommand (plain text, Conn );
Conn. open ();
Preparecommand (CMD, Conn, null, partition type, plain text, plain parms );
Sqldataadapter da = new sqldataadapter (CMD );
Da. Fill (DS );
Conn. Close ();
Return Ds. Tables [0];
}
Public static sqldatareader executereader (commandtype primitive type, string plain text, Params sqlparameter [] partition parms)
{
Sqlcommand cmd = new sqlcommand ();
Sqlconnection conn = new sqlconnection (conn_string );
Conn. open ();
Preparecommand (CMD, Conn, null, partition type, plain text, plain parms );
Sqldatareader RDR = cmd. executereader (commandbehavior. closeconnection );
Cmd. Parameters. Clear ();
RDR. Close ();
Conn. Close ();
Return RDR;
}
Public static object executescalar (commandtype parameter type, string parameter text, Params sqlparameter [] parameter parms)
{
Sqlcommand cmd = new sqlcommand ();
Using (sqlconnection conn = new sqlconnection (conn_string ))
{
Conn. open ();
Preparecommand (CMD, Conn, null, partition type, plain text, plain parms );
Object val = cmd. executescalar ();
Cmd. Parameters. Clear ();
Conn. Close ();
Return val;
}
}
Public static object executescalar (sqlconnection Conn, commandtype limit type, string limit text, Params sqlparameter [] limit parms)
{
Sqlcommand cmd = new sqlcommand ();
Preparecommand (CMD, Conn, null, partition type, plain text, plain parms );
Object val = cmd. executescalar ();
Cmd. Parameters. Clear ();
Return val;
}
Private Static void preparecommand (sqlcommand cmd, sqlconnection Conn, sqltransaction trans, commandtype primitive type, string plain text, sqlparameter [] partial parms)
{
If (conn. State! = Connectionstate. open)
Conn. open ();
Cmd. Connection = conn;
Cmd. commandtext = plain text;
If (trans! = NULL)
Cmd. Transaction = trans;
Cmd. commandtype = primitive type;
If (partition parms! = NULL)
{
Foreach (sqlparameter parm in milliseconds parms)
Cmd. Parameters. Add (parm );
}
}
Public static void executenonquery (string SQL)
{
Sqlconnection conn = new sqlconnection (conn_string );
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Conn. open ();
Cmd. executenonquery ();
Conn. Close ();
}
/// <Summary>
/// Input an SQL statement and return an int
/// </Summary>
/// <Param name = "SQL"> </param>
/// <Returns> </returns>
Public static int excutecommand (string SQL)
{
Sqlconnection conn = new sqlconnection (conn_string );
Sqlcommand cmd = new sqlcommand (SQL, Conn );
Conn. open ();
Int result = cmd. executenonquery ();
Conn. Close ();
Return result;
}
/// <Summary>
///
/// </Summary>
/// <Param name = "name"> display by page </param>
/// <Param name = "Pk"> primary key name </param>
/// <Param name = "fields"> the field to be retrieved. If it is left blank, it is * </param>
/// <Param name = "pagesize"> Number of records per page </param>
/// <Param name = "currentpage"> current page </param>
/// <Param name = "filter"> condition, which can be null. Do not enter where </param>
/// <Param name = "group"> group basis. It can be empty. You do not need to enter group by. </param>
/// <Param name = "order"> sorting. It can be empty. If it is empty, it is sorted by the primary key in ascending order by default. Order by is not required. </param>
/// <Returns> </returns>
Public static datatable pagedlist (string name, string PK, string fields, int pagesize, int currentpage, string filter, string group, string order)
{
Currentpage = currentpage-1;
Datatable dt = getdataset (commandtype. storedprocedure, "fenye ",
New sqlparameter ("@ tablenames", name ),
New sqlparameter ("@ primarykey", PK ),
New sqlparameter ("@ fields", fields ),
New sqlparameter ("@ pagesize", pagesize ),
New sqlparameter ("@ currentpage", currentpage ),
New sqlparameter ("@ filter", filter ),
New sqlparameter ("@ group", group ),
New sqlparameter ("@ order", order)
);
Return DT;
}
Public static int fenye_num (string name, string filter)
{
Return (INT) executescalar (commandtype. storedprocedure, "fenye_num ",
New sqlparameter ("@ tablenames", name ),
New sqlparameter ("@ filter", filter ));
}
/// <Summary>
///
/// </Summary>
/// <Param name = "name"> display by page </param>
/// <Param name = "Pk"> primary key name </param>
/// <Param name = "fields"> the field to be retrieved. If it is left blank, it is * </param>
/// <Param name = "pagesize"> Number of records per page </param>
/// <Param name = "currentpage"> current page </param>
/// <Param name = "filter"> condition, which can be null. Do not enter where </param>
/// <Param name = "group"> group basis. It can be empty. You do not need to enter group by. </param>
/// <Param name = "order"> sorting. It can be empty. If it is empty, it is sorted by the primary key in ascending order by default. Order by is not required. </param>
/// <Param name = "objanp"> pass the aspnetpager Control </param>
/// <Returns> </returns>
Public static datatable paged (string name, string PK, string fields, int pagesize, int currentpage, string filter, string group, string order, aspnetpager objanp)
{
Currentpage = currentpage-1;
Datatable dt = getdataset (commandtype. storedprocedure, "fenye ",
New sqlparameter ("@ tablenames", name ),
New sqlparameter ("@ primarykey", PK ),
New sqlparameter ("@ fields", fields ),
New sqlparameter ("@ pagesize", pagesize ),
New sqlparameter ("@ currentpage", currentpage ),
New sqlparameter ("@ filter", filter ),
New sqlparameter ("@ group", group ),
New sqlparameter ("@ order", order)
);
Objanp. recordcount = fenye_num (name, filter );
Return DT;
}
}
}

Page call Method Copy code The Code is as follows: using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using insus. net;
Using Dal;
Using system. Data. sqlclient;
Public partial class news_newlist: system. Web. UI. Page
{
Int NID;
Int totalorders;
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
If (request. querystring ["typeid"]! = NULL)
{
Nid = convert. toint16 (request. querystring ["typeid"]. tostring ());
Binddata (1 );
}
Else
{
Response. Redirect ("~ /Default. aspx ");
}
}
}
Private void binddata (INT page)
{
Datatable dt = dbhelper. paged ("m_newinfoall", "new_id", "", aspnetpager1.pagesize, page, "new_typeid =" + NID. tostring () + "", "", "new_pubdate DESC", aspnetpager1 );
This. repeater1.datasource = DT;
This. repeater1.databind ();
Datarow DR = DT. Rows [0];
This. label1.text = Dr ["new_typename"]. tostring ();
// This. literal1.text = Dr ["new_typename"]. tostring ();
Page. Title = label1.text. Trim () + "-New Rural Cooperative Medical Network ";
}
Protected void aspnetpager1_pagechanged (Object sender, eventargs E)
{
If (request. querystring ["page"]! = NULL)
{
Binddata (convert. toint32 (request. querystring ["page"]. tostring ()));
}
}
}

In this way, you can implement () by page. In any project, you only need to copy two stored procedures and one data operation class. Alternatively, you can make the data class DLL, only one line of code is required to input parameters during page calling.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.