*.apsx页面

   1:  <%@ Page Language="C#"  Debug ="true" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml" >
   6:  <head runat="server">
   7:      <title>[xtyang]</title>
   8:      <script runat ="server">
   9:      </script>
  10:  </head>
  11:  <body>
  12:      <form id="form1" runat="server">
  13:      <div>
  14:          &nbsp;
  15:      </div>
  16:      <div runat ="server" id="div_test">
  17:          <table  border="1">
  18:          <%
  19:   
  20:              for (int i = 0; i < lst_pwd.Count;i++ )
  21:              {
  22:                  %>
  23:                  <tr><td><% =lst_id[i] %></td><td><%=lst_name[i] %></td><td><%=lst_pwd[i] %></td></tr>
  24:                  <%
  25:              }
  26:              %>
  27:              <tr>
  28:              <td><a href="Default.aspx?currentpage=<%=0 %>">FirstPage</a></td>
  29:              <td><a href="Default.aspx?currentpage=<%=currentpage==pagecount?currentpage=pagecount:currentpage+1 %>">
  30:              Nextpage</a></td>
  31:              <td><a href="Default.aspx?currentpage=<%=currentpage==0?currentpage=0:currentpage-1 %>">Prepage</a></td>
  32:              <td><a href="Default.aspx?currentpage=<%=pagecount %>">EndPage</a></td>   
  33:              </tr>
  34:              <%        
  35:          %>          
  36:          </table>       
  37:      </div>
  38:      </form>
  39:  </body>
  40:  </html>

*.aspx.cs页面

   1:  using System;
   2:  using System.Data;
   3:  using System.Collections;
   4:  using System.Collections.Generic;
   5:  using System.Configuration;
   6:  using System.Web;
   7:  using System.Web.Security;
   8:  using System.Web.UI;
   9:  using System.Web.UI.WebControls;
  10:  using System.Web.UI.WebControls.WebParts;
  11:  using System.Web.UI.HtmlControls;
  12:  using MySql.Data.MySqlClient;
  13:   
  14:  public partial class _Default : System.Web.UI.Page 
  15:  {
  16:      //next variable should be modify depend you need.
  17:      public List<string> lst_name = new List<string>();
  18:      public List<string> lst_pwd = new List<string>();
  19:      public List<int> lst_id = new List<int>();
  20:      public string tablename="userinfo";
  21:      public int pagesize=3;
  22:   
  23:   
  24:      //next variable donnot need to modify.
  25:      public int currentpage = 0;//current page num.!!!
  26:      public int pagecount;//the count of page to be displayed
  27:      private int maxcount;//the count of the data.
  28:   
  29:   
  30:   
  31:      protected void Page_Load(object sender, EventArgs e)
  32:      {
  33:   
  34:   
  35:          div_page dp = new div_page();
  36:          currentpage = Convert.ToInt32(Request["currentpage"]);
  37:   
  38:          string str_sql_count = "select * from "+tablename;
  39:          DataSet ds_count = (DataSet)dp.exec_sql(str_sql_count, "read");
  40:          maxcount = (int)ds_count.Tables[0].Rows.Count;
  41:          if (maxcount / pagesize == 0)
  42:          {
  43:              pagecount = (maxcount / pagesize) - 1;
  44:          }
  45:          else
  46:          {
  47:              pagecount = (maxcount / pagesize);
  48:          }
  49:   
  50:   
  51:          string str_sql_read = "select * from "+tablename+" limit " + currentpage * pagesize + "," + pagesize;
  52:          DataSet ds_res = (DataSet)dp.exec_sql(str_sql_read, "read");
  53:          foreach (DataRow dr in ds_res.Tables[0].Rows)
  54:          {
  55:   
  56:              //next sentence should be modified next time.
  57:              lst_id.Add((int)dr["id"]);
  58:              lst_name.Add((string)dr["name"]);
  59:              lst_pwd.Add((string)dr["password"]);
  60:          }
  61:   
  62:      }
  63:  }
  64:   
  65:  public class div_page
  66:  {
  67:   
  68:      private  object res;//the result of return.
  69:   
  70:      /// <summary>
  71:      /// the function to execute sql sentence.
  72:      /// </summary>
  73:      /// <param name="str_sql_exec"></param>
  74:      /// <param name="str_opt"></param>
  75:      /// <returns>object</returns>
  76:      public object exec_sql(string str_sql_exec, string str_opt)
  77:      {
  78:          string str_conn = "Host=localhost;uid=***;pwd=***;database=***";
  79:          using (MySqlConnection mysql_conn = new MySqlConnection(str_conn))
  80:          {
  81:              switch (str_opt)
  82:              {
  83:                  case "read":
  84:                      using (MySqlDataAdapter da = new MySqlDataAdapter(str_sql_exec, mysql_conn))
  85:                      {
  86:                          DataSet ds = new DataSet();
  87:                          da.Fill(ds);
  88:                          res = ds;
  89:                      }
  90:                      break;
  91:                  case "update":
  92:                      mysql_conn.Open();
  93:                      using (MySqlCommand mycmd = new MySqlCommand(str_sql_exec, mysql_conn))
  94:                      {
  95:                          res = mycmd.ExecuteNonQuery();
  96:                      }
  97:                      break;
  98:                  case "delete":
  99:                      mysql_conn.Open();
 100:                      using (MySqlCommand mycmd = new MySqlCommand(str_sql_exec, mysql_conn))
 101:                      {
 102:                          res = mycmd.ExecuteNonQuery();
 103:                      }
 104:                      break;
 105:                  case "insert":
 106:                      mysql_conn.Open();
 107:                      using (MySqlCommand mycmd = new MySqlCommand(str_sql_exec, mysql_conn))
 108:                      {
 109:                          res = mycmd.ExecuteNonQuery();
 110:                      }
 111:                      break;
 112:                  default:
 113:                      break;
 114:              }
 115:   
 116:          }
 117:          return res;
 118:   
 119:      }
 120:  }