First view results
Involves 3 files, a database connection class dbutil, a paging class pageutil, and an instance CS file, which is quite easy to use.
Dbutil. CS
Using system;
Using system. Data;
Using
System. configuration;
Using system. Web;
Using
System. Web. Security;
Using system. Web. UI;
Using
System. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using
System. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using
Tour. exception;
Namespace tour. DB
{
//// Throw a tourexception exception. The previous layer does not capture and handle the exception.
Public class dbutil
{
Public sqlconnection strcon;
Public dbutil ()
{
Strcon = new
Sqlconnection (system. configuration. configurationmanager. etettings ["conn"]);
}
/*
* Query a record
* Returns sqldatareader.
* Parameter SQL statement
* Discard this method and do not close the connection. Please note that the connection is closed externally.
**/
Public sqldatareader getonedata (string SQL)
{
Try
{
Openconn ();
}
Catch (tourexception E)
{
Throw E;
}
Sqlcommand comm = new sqlcommand (SQL, strcon );
Sqldatareader READ = NULL;
Try
{
Read = comm. executereader ();
}
Catch
(Sqlexception ){
// Throw new
Tourexception ("an exception occurred while executing the command on the locked line ");
Throw new
Tourexception (SQL );
}
Catch
(Exception)
{
// Throw new
Tourexception ("failed to execute this command ");
Throw new
Tourexception (SQL );
}
Return read;
}
/*
* Add, delete, and modify operations
* Parameter SQL statement
*
**/
Public void updatedata (string SQL)
{
Try
{
Openconn ();
}
Catch (exception E)
{
Throw new
Tourexception ("a connection-level error occurred when you opened the connection! ");
}
Sqlcommand
Cmd = new sqlcommand (SQL, strcon );
Try
{
Cmd. executenonquery ();
}
Catch (exception E)
{
// Throw new
Tourexception ("an exception occurred while executing the command on the locked line ");
Throw new
Tourexception (SQL );
}
Try
{
Closeconn ();
}
Catch
(Exception E)
{
Throw new tourexception ("Closing connection failure ");
}
}
/*
* Close the connection.
*
*/
Public void closeconn ()
{
Try
{
If (strcon. State! =
Connectionstate. Closed ){
Strcon. Close ();
}
}
Catch
(Exception)
{
Throw new
Tourexception ("a connection-level error occurs when a connection is opened ");
}
}
Private void openconn ()
{
Try
{
If (strcon. State! =
Connectionstate. open)
{
Strcon. open ();
}
}
Catch (invalidoperationexception
Ee)
{
Throw new
Tourexception ("the data source or server is not specified. The connection cannot be opened or the connection has been opened! ");
}
Catch
(Exception)
{
Throw new
Tourexception ("a connection-level error occurred when you opened the connection! ");
}
}
Public dataset getdataset (string SQL ){
Try
{
Openconn ();
}
Catch (exception E)
{
// Throw
New tourexception ("no data source or server is specified. The connection cannot be opened or the connection has been opened! ");
Throw new
Tourexception (SQL );
}
Sqldataadapter SDA = new
Sqldataadapter (SQL, strcon );
Dataset DS = new
Dataset ();
Try
{
SDA. Fill (DS );
}
Catch (exception
Te)
{
Throw new tourexception (SQL );
}
Try
{
Closeconn ();
}
Catch (exception E)
{
Throw new tourexception ("Closing connection failure ");
}
Return Ds;
}
}
}
Pageutil. CS
Using system;
Using system. Data;
Using
System. configuration;
Using system. Web;
Using
System. Web. Security;
Using system. Web. UI;
Using
System. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using
System. Web. UI. htmlcontrols;
Using system. text;
Using tour. dB;
Namespace tour. Tools. Page
{
Public class pageutil
{
Private string pageurl; // relative path of the current page
Private
Int totalpage; // the total number of pages.
Private int prepageindex; // Previous Page
Private int nextpageindex; // next page
Private int currpageindex; // current page
// Private int firstpageindex; // The first page
// Private int lastpageindex; // last page
Private int totalrec; // The total number of records.
Private int pagesize; // number of records per page
Private string SQL; // SQL
Private string PK; // primary key
Private string DESC; // sort
Private dbutil du = new dbutil ();
Public pageutil (INT totalrec, int currpageindex, int pagesize, string
Pageurl)
{
This. totalrec = totalrec;
This. currpageindex = currpageindex;
This. pagesize =
Pagesize;
This. pageurl = pageurl;
Init ();
}
Public pageutil (INT currpageindex, int
Pagesize, string pageurl, string SQL, string PK, string DESC ){
This. currpageindex = currpageindex;
This. pagesize =
Pagesize;
This. pageurl = pageurl;
This. SQL =
SQL;
This. PK = PK;
This. DESC =
DESC;
Init ();
}
Private void
Init ()
{
// Total number of pages
This. totalrec =
Computetotalrec ();
Totalpage = totalrec/
Pagesize;
If (totalrec % pagesize> 0)
Totalpage = totalpage + 1;
// This. pageurl + = "? P = P ";
}
Private int computetotalrec (){
Stringbuilder sb =
New stringbuilder ();
SB. append ("select count (*) c
From ");
SB. append ("(");
SB. append (this. SQL );
SB. append (") count_table ");
Dataset DS = DU. getdataset (sb. tostring ());
Return
Convert. toint32 (Ds. Tables [0]. Rows [0] ["C"]. tostring ());
}
Public dataset pagedataset (){
Int n1 =
(Convert. toint32 (currpageindex)-1) * convert. toint32 (pagesize) +
1;
Int n2 = N1 + convert. toint32 (pagesize)-1;
Stringbuilder sb = new stringbuilder ();
SB. append ("select tb1 .*
From ");
SB. append ("(");
SB. append ("select
TB. *, row_number () over (order by "+ this. PK +" "+ this. DESC +") Rn
From ");
SB. append ("(");
SB. append (this. SQL );
SB. append (") TB ");
SB. append (") tb1 ");
SB. append ("where tb1.rn <=" +
N2 );
SB. append ("and tb1.rn> =" + N1 );
Return
Du. getdataset (sb. tostring ());
}
Public String
Createpagefooter ()
{
Stringbuilder STR = new
Stringbuilder ();
This. prepageindex = currpageindex-
1;
This. nextpageindex = currpageindex + 1;
If (currpageindex> 1)
{
Str. append (
"<A href = '" +
This. pageurl +
"& Page = 1 & pagesize =" + this. pagesize + "'> homepage </a> & nbsp ;");
}
Else
{
Str. append ("homepage & nbsp ;");
}
If (currpageindex
> 1)
{
Str. append (
"<A href = '" + this. pageurl + "& page =" + this. prepageindex +
"& Pagesize =" + this. pagesize +
"'> Previous page </a> & nbsp ;");
}
Else
{
Str. append ("Previous Page & nbsp ;");
}
Str. append ("& nbsp; current" + this. currpageindex + "Page & nbsp ;");
If (currpageindex <totalpage)
{
Str. append (
"<A href = '" + this. pageurl + "& page ="
+ This. nextpageindex + "& pagesize =" + this. pagesize +
"'> Next page </a> & nbsp ;");
}
Else
{
Str. append ("next page ");
}
If (totalpage> 1 & currpageindex! =
Totalpage)
{
Str. append (
"<A href = '" + this. pageurl + "& page ="
+ This. totalpage + "& pagesize =" + this. pagesize +
"'> Last page </a> & nbsp ;");
}
Else
{
Str. append ("last page ");
}
Str. append ("Total" + totalrec + "record ");
Str. append ("per page <select size = 1 name = pagesize
Onchange =/"window. Location. href = This. Value/"> ");
If (pagesize = 3)
{
Str. append ("<option value =" + this. pageurl + "& pagesize = 3
Selected> 3 </option> ");
}
Else
{
Str. append ("<option value =" +
This. pageurl + "& pagesize = 3> 3 </option> ");
}
If (pagesize = 10)
{
Str. append ("<option value =" + this. pageurl +
"& Pagesize = 10 selected> 10 </option> ");
}
Else
{
Str. append ("<Option
Value = "+ this. pageurl +
"& Pagesize = 10> 10 </option> ");
}
If
(Pagesize = 20)
{
Str. append ("<Option
Value = "+ this. pageurl +" & pagesize = 20
Selected> 20 </option> ");
}
Else
{
Str. append ("<option value =" +
This. pageurl + "& pagesize = 20> 20 </option> ");
}
If (pagesize = 50)
{
Str. append ("<option value =" + this. pageurl + "& pagesize = 50
Selected> 50 </option> ");
}
Else
{
Str. append ("<option value =" +
This. pageurl + "& pagesize = 50> 50 </option> ");
}
If (pagesize = 100)
{
Str. append ("<option value =" + this. pageurl + "& pagesize = 100
Selected> 100 </option> ");
}
Else
{
Str. append ("<option value =" +
This. pageurl + "& pagesize = 100> 100 </option> ");
}
Str. append ("</SELECT> ");
Str. append ("points"
+ Totalpage + "page display ");
Str. append ("<select size = 1
Name = pagelist onchange =/"window. Location. href = This. Value/"> ");
For (INT I = 1; I <totalpage + 1; I ++)
{
If (I = currpageindex)
{
Str. append ("<option value =" + this. pageurl + "& page =" + I +
"& Pagesize =" + this. pagesize + "selected>" + I
+
"</Option> ");
}
Else
{
Str. append ("<option value =" + this. pageurl +
"& Page =" + I + "& pagesize =" + this. pagesize + ">" + I +
"</Option> ");
}
}
Str. append ("</SELECT> page ");
Return
Str. tostring ();
}
}
}
This document uses an example to describe how to apply this function. All articles in this topic are queried Based on the topic ID and displayed on pages.
String lmid = request ["lmid"];
// Current page
String currpageindex = request ["page"];
// Several entries per page
String pagesize = request ["pagesize"];
If
("". Equals (currpageindex) | currpageindex = NULL)
{
Currpageindex = "1 ";
}
If
("". Equals (pagesize) | pagesize = NULL)
{
Pagesize = "20 ";
}
Stringbuilder SQL = new
Stringbuilder ();
SQL. append ("select T. *, TT. lmname from
T_news T, t_lanmu TT where T. lid = TT. lid ");
SQL. append ("and T. Lid
= "+ Lmid );
Pu = new pageutil (convert. toint32 (currpageindex ),
Convert. toint32 (pagesize), "newsbylm. aspx? Lmid = "+
Lmid, SQL. tostring (), "NID", "DESC ");
Dataset ds1 =
Pu. pagedataset ();
Repeater1.datasource = ds1;
// Bind the repeater to the frontend.
Repeater1.databind ();
L_lmname.text = ds1.tables [0]. Rows [0] ["lmname"]. tostring ();
// This is the topic name.
Rochelle page.text = pu. createpagefooter ();