zoukankan      html  css  js  c++  java
  • 千万级数据分页存储过程示例!

    ----填写正确的数据库连接配置后使用连接按钮连接数据库,选择相应的数据库,表名以及索引字段,点读取数据按钮即可获取分页数据,如果你的数据库中不包含 GetRecordFromPage 分页存储过程,请先建立,内容请见 Depends 目录下的 GetRecordFromPage.sql
    /*
      函数名称: GetRecordFromPage
      函数功能: 获取指定页的数据
      参数说明: @tblName      包含数据的表名
               @fldName      关键字段名
               @PageSize     每页记录数
               @PageIndex    要获取的页码
               @OrderType    排序类型, 0 - 升序, 1 - 降序
               @strWhere     查询条件 (注意: 不要加 where)
      作  者: 铁拳
      邮  箱: sunjianhua_kki@sina.com
      创建时间: 2004-07-04
      修改时间: 2004-07-04
    */
    CREATE PROCEDURE GetRecordFromPage
        @tblName      varchar(255),       -- 表名
        @fldName      varchar(255),       -- 字段名
        @PageSize     int = 10,           -- 页尺寸
        @PageIndex    int = 1,            -- 页码
        @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
        @strWhere     varchar(2000) = ''  -- 查询条件 (注意: 不要加 where)
    AS

    declare @strSQL   varchar(6000)       -- 主语句
    declare @strTmp   varchar(1000)       -- 临时变量
    declare @strOrder varchar(500)        -- 排序类型

    if @OrderType != 0
    begin
        set @strTmp = '<(select min'
        set @strOrder = ' order by [' + @fldName + '] desc'
    end
    else
    begin
        set @strTmp = '>(select max'
        set @strOrder = ' order by [' + @fldName +'] asc'
    end

    set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
        + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
        + @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
        + @strOrder

    if @strWhere != ''
        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
            + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
            + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
            + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

    if @PageIndex = 1
    begin
        set @strTmp = ''
        if @strWhere != ''
            set @strTmp = ' where (' + @strWhere + ')'

        set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
            + @tblName + ']' + @strTmp + ' ' + @strOrder
    end

    exec (@strSQL)

    GO

    --------代码
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;

    namespace PageSample
    {
     /// <summary>
     /// Form1 的摘要说明。
     /// </summary>
     public class formMain : System.Windows.Forms.Form
     {
      private System.Windows.Forms.GroupBox grpConSet;
      private System.Windows.Forms.TextBox txtPwd;
      private System.Windows.Forms.TextBox txtUid;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.GroupBox grpTblSet;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.Label label5;
      private System.Windows.Forms.ComboBox cboDB;

      private SqlConnection g_con = new SqlConnection();
      private int iCurrentPage = 1;
      private int iRecordCount = 0;

      private System.Windows.Forms.GroupBox grpResult;
      private System.Windows.Forms.ComboBox cboTbl;
      private System.Windows.Forms.TextBox txtServer;
      private System.Windows.Forms.Button btnConnect;
      private System.Windows.Forms.Button btnExit;
      private System.Windows.Forms.Button btnNext;
      private System.Windows.Forms.Button btnPrev;
      private System.Windows.Forms.Button btnGo;
      private System.Windows.Forms.DataGrid dgResult;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Button btnRefresh;
      private System.Windows.Forms.ComboBox cboField;
      private System.Windows.Forms.Label label8;
      private System.Windows.Forms.Label label9;
      private System.Windows.Forms.TextBox txtWhere;
      private System.Windows.Forms.Label label10;
      private System.Windows.Forms.ComboBox cboSort;
      private System.Windows.Forms.Label label6;
      private System.Windows.Forms.NumericUpDown nudSize;
      private System.Windows.Forms.Button btnRead;
      private System.Windows.Forms.NumericUpDown nudIndex;
      private System.Windows.Forms.Label lblState;
      
      /// <summary>
      /// 必需的设计器变量。
      /// </summary>
      private System.ComponentModel.Container components = null;

      public formMain()
      {
       //
       // Windows 窗体设计器支持所必需的
       //
       InitializeComponent();

       //
       // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
       //
      }

      /// <summary>
      /// 清理所有正在使用的资源。
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
       
        
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows 窗体设计器生成的代码
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {
       this.grpConSet = new System.Windows.Forms.GroupBox();
       this.txtPwd = new System.Windows.Forms.TextBox();
       this.txtUid = new System.Windows.Forms.TextBox();
       this.txtServer = new System.Windows.Forms.TextBox();
       this.label3 = new System.Windows.Forms.Label();
       this.label2 = new System.Windows.Forms.Label();
       this.label1 = new System.Windows.Forms.Label();
       this.grpTblSet = new System.Windows.Forms.GroupBox();
       this.cboField = new System.Windows.Forms.ComboBox();
       this.label8 = new System.Windows.Forms.Label();
       this.cboTbl = new System.Windows.Forms.ComboBox();
       this.cboDB = new System.Windows.Forms.ComboBox();
       this.label5 = new System.Windows.Forms.Label();
       this.label4 = new System.Windows.Forms.Label();
       this.grpResult = new System.Windows.Forms.GroupBox();
       this.btnGo = new System.Windows.Forms.Button();
       this.label6 = new System.Windows.Forms.Label();
       this.nudSize = new System.Windows.Forms.NumericUpDown();
       this.cboSort = new System.Windows.Forms.ComboBox();
       this.label10 = new System.Windows.Forms.Label();
       this.txtWhere = new System.Windows.Forms.TextBox();
       this.label9 = new System.Windows.Forms.Label();
       this.nudIndex = new System.Windows.Forms.NumericUpDown();
       this.btnNext = new System.Windows.Forms.Button();
       this.btnPrev = new System.Windows.Forms.Button();
       this.dgResult = new System.Windows.Forms.DataGrid();
       this.lblState = new System.Windows.Forms.Label();
       this.btnConnect = new System.Windows.Forms.Button();
       this.btnExit = new System.Windows.Forms.Button();
       this.btnRead = new System.Windows.Forms.Button();
       this.btnRefresh = new System.Windows.Forms.Button();
       this.grpConSet.SuspendLayout();
       this.grpTblSet.SuspendLayout();
       this.grpResult.SuspendLayout();
       ((System.ComponentModel.ISupportInitialize)(this.nudSize)).BeginInit();
       ((System.ComponentModel.ISupportInitialize)(this.nudIndex)).BeginInit();
       ((System.ComponentModel.ISupportInitialize)(this.dgResult)).BeginInit();
       this.SuspendLayout();
       //
       // grpConSet
       //
       this.grpConSet.Controls.Add(this.txtPwd);
       this.grpConSet.Controls.Add(this.txtUid);
       this.grpConSet.Controls.Add(this.txtServer);
       this.grpConSet.Controls.Add(this.label3);
       this.grpConSet.Controls.Add(this.label2);
       this.grpConSet.Controls.Add(this.label1);
       this.grpConSet.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.grpConSet.Location = new System.Drawing.Point(16, 16);
       this.grpConSet.Name = "grpConSet";
       this.grpConSet.Size = new System.Drawing.Size(192, 112);
       this.grpConSet.TabIndex = 0;
       this.grpConSet.TabStop = false;
       this.grpConSet.Text = "连接设置";
       //
       // txtPwd
       //
       this.txtPwd.Location = new System.Drawing.Point(72, 72);
       this.txtPwd.Name = "txtPwd";
       this.txtPwd.PasswordChar = '*';
       this.txtPwd.Size = new System.Drawing.Size(104, 21);
       this.txtPwd.TabIndex = 5;
       this.txtPwd.Text = "";
       //
       // txtUid
       //
       this.txtUid.Location = new System.Drawing.Point(72, 48);
       this.txtUid.Name = "txtUid";
       this.txtUid.Size = new System.Drawing.Size(104, 21);
       this.txtUid.TabIndex = 3;
       this.txtUid.Text = "sa";
       //
       // txtServer
       //
       this.txtServer.Location = new System.Drawing.Point(72, 24);
       this.txtServer.Name = "txtServer";
       this.txtServer.Size = new System.Drawing.Size(104, 21);
       this.txtServer.TabIndex = 1;
       this.txtServer.Text = "(local)";
       //
       // label3
       //
       this.label3.AutoSize = true;
       this.label3.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label3.Location = new System.Drawing.Point(16, 74);
       this.label3.Name = "label3";
       this.label3.Size = new System.Drawing.Size(54, 17);
       this.label3.TabIndex = 4;
       this.label3.Text = "登录密码";
       //
       // label2
       //
       this.label2.AutoSize = true;
       this.label2.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label2.Location = new System.Drawing.Point(16, 50);
       this.label2.Name = "label2";
       this.label2.Size = new System.Drawing.Size(54, 17);
       this.label2.TabIndex = 2;
       this.label2.Text = "登录名称";
       //
       // label1
       //
       this.label1.AutoSize = true;
       this.label1.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label1.Location = new System.Drawing.Point(16, 26);
       this.label1.Name = "label1";
       this.label1.Size = new System.Drawing.Size(54, 17);
       this.label1.TabIndex = 0;
       this.label1.Text = "网络地址";
       //
       // grpTblSet
       //
       this.grpTblSet.Controls.Add(this.cboField);
       this.grpTblSet.Controls.Add(this.label8);
       this.grpTblSet.Controls.Add(this.cboTbl);
       this.grpTblSet.Controls.Add(this.cboDB);
       this.grpTblSet.Controls.Add(this.label5);
       this.grpTblSet.Controls.Add(this.label4);
       this.grpTblSet.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.grpTblSet.Location = new System.Drawing.Point(224, 16);
       this.grpTblSet.Name = "grpTblSet";
       this.grpTblSet.Size = new System.Drawing.Size(264, 112);
       this.grpTblSet.TabIndex = 1;
       this.grpTblSet.TabStop = false;
       this.grpTblSet.Text = "表名设置";
       //
       // cboField
       //
       this.cboField.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
       this.cboField.Location = new System.Drawing.Point(72, 72);
       this.cboField.MaxDropDownItems = 20;
       this.cboField.Name = "cboField";
       this.cboField.Size = new System.Drawing.Size(176, 20);
       this.cboField.TabIndex = 5;
       this.cboField.SelectedIndexChanged += new System.EventHandler(this.cboField_SelectedIndexChanged);
       //
       // label8
       //
       this.label8.AutoSize = true;
       this.label8.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label8.Location = new System.Drawing.Point(16, 74);
       this.label8.Name = "label8";
       this.label8.Size = new System.Drawing.Size(54, 17);
       this.label8.TabIndex = 4;
       this.label8.Text = "关键字段";
       //
       // cboTbl
       //
       this.cboTbl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
       this.cboTbl.Location = new System.Drawing.Point(72, 48);
       this.cboTbl.MaxDropDownItems = 20;
       this.cboTbl.Name = "cboTbl";
       this.cboTbl.Size = new System.Drawing.Size(176, 20);
       this.cboTbl.TabIndex = 3;
       this.cboTbl.SelectedIndexChanged += new System.EventHandler(this.cboTbl_SelectedIndexChanged);
       //
       // cboDB
       //
       this.cboDB.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
       this.cboDB.Location = new System.Drawing.Point(72, 24);
       this.cboDB.MaxDropDownItems = 20;
       this.cboDB.Name = "cboDB";
       this.cboDB.Size = new System.Drawing.Size(176, 20);
       this.cboDB.TabIndex = 1;
       this.cboDB.SelectedIndexChanged += new System.EventHandler(this.cboDB_SelectedIndexChanged);
       //
       // label5
       //
       this.label5.AutoSize = true;
       this.label5.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label5.Location = new System.Drawing.Point(16, 50);
       this.label5.Name = "label5";
       this.label5.Size = new System.Drawing.Size(54, 17);
       this.label5.TabIndex = 2;
       this.label5.Text = "关键表名";
       //
       // label4
       //
       this.label4.AutoSize = true;
       this.label4.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label4.Location = new System.Drawing.Point(16, 26);
       this.label4.Name = "label4";
       this.label4.Size = new System.Drawing.Size(54, 17);
       this.label4.TabIndex = 0;
       this.label4.Text = "数据库名";
       //
       // grpResult
       //
       this.grpResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
       this.grpResult.Controls.Add(this.btnGo);
       this.grpResult.Controls.Add(this.label6);
       this.grpResult.Controls.Add(this.nudSize);
       this.grpResult.Controls.Add(this.cboSort);
       this.grpResult.Controls.Add(this.label10);
       this.grpResult.Controls.Add(this.txtWhere);
       this.grpResult.Controls.Add(this.label9);
       this.grpResult.Controls.Add(this.nudIndex);
       this.grpResult.Controls.Add(this.btnNext);
       this.grpResult.Controls.Add(this.btnPrev);
       this.grpResult.Controls.Add(this.dgResult);
       this.grpResult.Controls.Add(this.lblState);
       this.grpResult.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.grpResult.Location = new System.Drawing.Point(16, 136);
       this.grpResult.Name = "grpResult";
       this.grpResult.Size = new System.Drawing.Size(576, 344);
       this.grpResult.TabIndex = 5;
       this.grpResult.TabStop = false;
       this.grpResult.Text = "查询结果";
       //
       // btnGo
       //
       this.btnGo.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
       this.btnGo.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.btnGo.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.btnGo.Location = new System.Drawing.Point(400, 306);
       this.btnGo.Name = "btnGo";
       this.btnGo.Size = new System.Drawing.Size(59, 24);
       this.btnGo.TabIndex = 10;
       this.btnGo.Text = "跳转(&G)";
       this.btnGo.Click += new System.EventHandler(this.btnGo_Click);
       //
       // label6
       //
       this.label6.AutoSize = true;
       this.label6.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label6.Location = new System.Drawing.Point(16, 28);
       this.label6.Name = "label6";
       this.label6.Size = new System.Drawing.Size(54, 17);
       this.label6.TabIndex = 0;
       this.label6.Text = "每页显示";
       //
       // nudSize
       //
       this.nudSize.Location = new System.Drawing.Point(72, 24);
       this.nudSize.Maximum = new System.Decimal(new int[] {
                     1000,
                     0,
                     0,
                     0});
       this.nudSize.Name = "nudSize";
       this.nudSize.Size = new System.Drawing.Size(56, 21);
       this.nudSize.TabIndex = 1;
       this.nudSize.ThousandsSeparator = true;
       this.nudSize.Value = new System.Decimal(new int[] {
                      10,
                      0,
                      0,
                      0});
       //
       // cboSort
       //
       this.cboSort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
       this.cboSort.Items.AddRange(new object[] {
                   "升序",
                   "降序"});
       this.cboSort.Location = new System.Drawing.Point(200, 24);
       this.cboSort.Name = "cboSort";
       this.cboSort.Size = new System.Drawing.Size(64, 20);
       this.cboSort.TabIndex = 3;
       //
       // label10
       //
       this.label10.AutoSize = true;
       this.label10.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label10.Location = new System.Drawing.Point(144, 28);
       this.label10.Name = "label10";
       this.label10.Size = new System.Drawing.Size(54, 17);
       this.label10.TabIndex = 2;
       this.label10.Text = "排序方式";
       //
       // txtWhere
       //
       this.txtWhere.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
       this.txtWhere.Location = new System.Drawing.Point(336, 28);
       this.txtWhere.Name = "txtWhere";
       this.txtWhere.Size = new System.Drawing.Size(224, 21);
       this.txtWhere.TabIndex = 5;
       this.txtWhere.Text = "";
       //
       // label9
       //
       this.label9.AutoSize = true;
       this.label9.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.label9.Location = new System.Drawing.Point(280, 28);
       this.label9.Name = "label9";
       this.label9.Size = new System.Drawing.Size(54, 17);
       this.label9.TabIndex = 4;
       this.label9.Text = "查询条件";
       //
       // nudIndex
       //
       this.nudIndex.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
       this.nudIndex.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.nudIndex.Location = new System.Drawing.Point(472, 308);
       this.nudIndex.Maximum = new System.Decimal(new int[] {
                      999999999,
                      0,
                      0,
                      0});
       this.nudIndex.Name = "nudIndex";
       this.nudIndex.Size = new System.Drawing.Size(88, 21);
       this.nudIndex.TabIndex = 11;
       this.nudIndex.ThousandsSeparator = true;
       this.nudIndex.Value = new System.Decimal(new int[] {
                       1,
                       0,
                       0,
                       0});
       //
       // btnNext
       //
       this.btnNext.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
       this.btnNext.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.btnNext.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.btnNext.Location = new System.Drawing.Point(341, 306);
       this.btnNext.Name = "btnNext";
       this.btnNext.Size = new System.Drawing.Size(59, 24);
       this.btnNext.TabIndex = 9;
       this.btnNext.Text = "下页(&N)";
       this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
       //
       // btnPrev
       //
       this.btnPrev.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
       this.btnPrev.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.btnPrev.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.btnPrev.Location = new System.Drawing.Point(282, 306);
       this.btnPrev.Name = "btnPrev";
       this.btnPrev.Size = new System.Drawing.Size(59, 24);
       this.btnPrev.TabIndex = 8;
       this.btnPrev.Text = "上页(&P)";
       this.btnPrev.Click += new System.EventHandler(this.btnPrev_Click);
       //
       // dgResult
       //
       this.dgResult.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
        | System.Windows.Forms.AnchorStyles.Left)
        | System.Windows.Forms.AnchorStyles.Right)));
       this.dgResult.CaptionVisible = false;
       this.dgResult.DataMember = "";
       this.dgResult.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.dgResult.HeaderForeColor = System.Drawing.SystemColors.ControlText;
       this.dgResult.Location = new System.Drawing.Point(16, 63);
       this.dgResult.Name = "dgResult";
       this.dgResult.ReadOnly = true;
       this.dgResult.Size = new System.Drawing.Size(544, 233);
       this.dgResult.TabIndex = 6;
       //
       // lblState
       //
       this.lblState.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
       this.lblState.AutoSize = true;
       this.lblState.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.lblState.Location = new System.Drawing.Point(16, 310);
       this.lblState.Name = "lblState";
       this.lblState.Size = new System.Drawing.Size(215, 17);
       this.lblState.TabIndex = 7;
       this.lblState.Text = "共 {0} 条记录,第 {0} 页, 共 {0} 页";
       //
       // btnConnect
       //
       this.btnConnect.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.btnConnect.Location = new System.Drawing.Point(504, 24);
       this.btnConnect.Name = "btnConnect";
       this.btnConnect.Size = new System.Drawing.Size(88, 24);
       this.btnConnect.TabIndex = 2;
       this.btnConnect.Text = "开始连接(&C)";
       this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
       //
       // btnExit
       //
       this.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.btnExit.Location = new System.Drawing.Point(504, 72);
       this.btnExit.Name = "btnExit";
       this.btnExit.Size = new System.Drawing.Size(88, 24);
       this.btnExit.TabIndex = 4;
       this.btnExit.Text = "退出程序(&X)";
       this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
       //
       // btnRead
       //
       this.btnRead.FlatStyle = System.Windows.Forms.FlatStyle.System;
       this.btnRead.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.btnRead.Location = new System.Drawing.Point(504, 48);
       this.btnRead.Name = "btnRead";
       this.btnRead.Size = new System.Drawing.Size(88, 24);
       this.btnRead.TabIndex = 3;
       this.btnRead.Text = "读取数据(&R)";
       this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
       //
       // btnRefresh
       //
       this.btnRefresh.Location = new System.Drawing.Point(504, 72);
       this.btnRefresh.Name = "btnRefresh";
       this.btnRefresh.Size = new System.Drawing.Size(88, 24);
       this.btnRefresh.TabIndex = 0;
       //
       // formMain
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
       this.ClientSize = new System.Drawing.Size(608, 494);
       this.Controls.Add(this.btnExit);
       this.Controls.Add(this.btnRead);
       this.Controls.Add(this.btnConnect);
       this.Controls.Add(this.grpResult);
       this.Controls.Add(this.grpTblSet);
       this.Controls.Add(this.grpConSet);
       this.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
       this.Name = "formMain";
       this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
       this.Text = "分页测试 v1.0 - hiDotNet 官方";
       this.Load += new System.EventHandler(this.formMain_Load);
       this.Closed += new System.EventHandler(this.formMain_Closed);
       this.grpConSet.ResumeLayout(false);
       this.grpTblSet.ResumeLayout(false);
       this.grpResult.ResumeLayout(false);
       ((System.ComponentModel.ISupportInitialize)(this.nudSize)).EndInit();
       ((System.ComponentModel.ISupportInitialize)(this.nudIndex)).EndInit();
       ((System.ComponentModel.ISupportInitialize)(this.dgResult)).EndInit();
       this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// 应用程序的主入口点。
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.Run(new formMain());
      }

      private void formMain_Load(object sender, System.EventArgs e)
      {
       // 默认按升序
       cboSort.SelectedIndex = 0;
      }

      // 断开连接
      private bool Disconnect(SqlConnection conObj)
      {
       try
       {
        if (conObj.State != System.Data.ConnectionState.Closed)
         conObj.Close();
        return true;
       }
       catch(Exception)
       {
        return false;
       }
      }

      // 连接 master 数据库
      private void btnConnect_Click(object sender, System.EventArgs e)
      {
       try
       {
        Disconnect(g_con);
        g_con.ConnectionString = "Data Source = " + txtServer.Text.Replace("'", "''") + "; User ID = " + txtUid.Text.Replace("'", "''") + "; Password = " + txtPwd.Text.Replace("'", "''") + "; Database = master";
        g_con.Open();

        // 获取所有数据库
        SqlCommand cmd = new SqlCommand("select [name] from [sysdatabases] order by [name]", g_con);
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        cboDB.ValueMember = "name";
        cboDB.DisplayMember = "name";
        cboDB.DataSource = ds.Tables[0];
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }

      private void formMain_Closed(object sender, System.EventArgs e)
      {
       Disconnect(g_con);
       g_con.Dispose();
      }

      private void btnExit_Click(object sender, System.EventArgs e)
      {
       this.Close();
      }

      private void cboDB_SelectedIndexChanged(object sender, System.EventArgs e)
      {
       try
       {
        if (cboDB.SelectedValue.ToString() == "")
         return;

        g_con.ChangeDatabase(cboDB.SelectedValue.ToString());

        // 获取所有表
        SqlCommand cmd = new SqlCommand("select [id], [name] from [sysobjects] where [type] = 'u' order by [name]", g_con);
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        cboTbl.ValueMember = "id";
        cboTbl.DisplayMember = "name";
        cboTbl.DataSource = ds.Tables[0];
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }

      private void cboTbl_SelectedIndexChanged(object sender, System.EventArgs e)
      {
       try
       {
        if (cboTbl.SelectedValue.ToString() == "")
         return;

        // 获取所有字段
        SqlCommand cmd = new SqlCommand("select [name] from [syscolumns] where [id] = " + cboTbl.SelectedValue.ToString() + " order by [colid]", g_con);
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        cboField.ValueMember = "name";
        cboField.DisplayMember = "name";
        cboField.DataSource = ds.Tables[0];

        // 初始化当前页
        iCurrentPage = 1;
        nudIndex.Value = iCurrentPage;

        // 获取记录总数
        cmd.CommandText = "select [o].[id], [o].[name], [i].[rows] from [sysindexes] [i], [sysobjects] [o] where [i].[first]>0 and [o].[id]=[i].[id] and [o].[type]='u' and [o].[id]=" + cboTbl.SelectedValue.ToString();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
         iRecordCount = (int)dr["rows"];
        lblState.Text = "共 " + iRecordCount.ToString() + " 条记录, 第 " + iCurrentPage.ToString() + " 页,共 " + ((iRecordCount / (int)nudSize.Value) + 1) + " 页";
        dr.Close();
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }

      // 获取分页数据
      private DataSet GetRecordFromPage(string strTable, string strField, int iPageSize, int iPageIndex, int iOrderType, string strWhere)
      {
       try
       {
        SqlCommand cmd = new SqlCommand("GetRecordFromPage", g_con);
        cmd.CommandType = CommandType.StoredProcedure;

        // 构造参数
        SqlParameter pmTable = new SqlParameter("@tblName", SqlDbType.VarChar, 255);
        pmTable.Value = strTable;

        SqlParameter pmField = new SqlParameter("@fldName", SqlDbType.VarChar, 255);
        pmField.Value = strField;
        
        SqlParameter pmSize = new SqlParameter("@PageSize", SqlDbType.Int);
        pmSize.Value = iPageSize;

        SqlParameter pmIndex = new SqlParameter("@PageIndex", SqlDbType.Int);
        pmIndex.Value = iPageIndex;

        SqlParameter pmOrderType = new SqlParameter("@OrderType", SqlDbType.Bit);
        pmOrderType.Value = iOrderType;

        SqlParameter pmWhere = new SqlParameter("@strWhere", SqlDbType.VarChar, 2000);
        pmWhere.Value = txtWhere.Text;

        // 传递参数
        cmd.Parameters.Add(pmTable);
        cmd.Parameters.Add(pmField);
        cmd.Parameters.Add(pmSize);
        cmd.Parameters.Add(pmIndex);
        cmd.Parameters.Add(pmOrderType);
        cmd.Parameters.Add(pmWhere);

        // 获取结果
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);

        lblState.Text = "共 " + iRecordCount.ToString() + " 条记录, 第 " + iCurrentPage.ToString() + " 页,共 " + ((iRecordCount / (int)nudSize.Value) + 1) + " 页";
        return ds;
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;
       }
      }

      private void cboField_SelectedIndexChanged(object sender, System.EventArgs e)
      {
      }

      // 读取默认记录
      private void btnRead_Click(object sender, System.EventArgs e)
      {
       try
       {
        ReadData(iCurrentPage);
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }

      // 获取指定页记录
      private bool ReadData(int iPage)
      {
       try
       {
        if (cboField.SelectedValue.ToString() == "")
         return false;

        dgResult.DataSource = GetRecordFromPage(cboTbl.Text, cboField.SelectedValue.ToString(), (int)nudSize.Value, iPage, (int)cboSort.SelectedIndex, txtWhere.Text).Tables[0];
        return true;
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return false;
       }
      }

      // 跳转到指定页
      private void btnGo_Click(object sender, System.EventArgs e)
      {
       try
       {
        iCurrentPage = (int)nudIndex.Value;
        ReadData(iCurrentPage);
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }

      // 获取上一页记录
      private void btnPrev_Click(object sender, System.EventArgs e)
      {
       try
       {
        if (iCurrentPage > 1)
        {
         iCurrentPage -= 1;
         nudIndex.Value = iCurrentPage;
         ReadData(iCurrentPage);
        }
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }

      // 获取下一页记录
      private void btnNext_Click(object sender, System.EventArgs e)
      {
       try
       {
        if (iCurrentPage < ((iRecordCount / (int)nudSize.Value) + 1))
        {
         iCurrentPage += 1;
         nudIndex.Value = iCurrentPage;
         ReadData(iCurrentPage);
        }
       }
       catch (Exception err)
       {
        MessageBox.Show(this, err.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }
      }
     }
    }

  • 相关阅读:
    OpenCV实现基于傅里叶变换的旋转文本校正
    【来龙去脉系列】QRCode二维码的生成细节和原理
    软件License设计
    常见加密算法简析
    【来龙去脉系列】RSA算法原理
    单目相机标定原理
    CYQ.Data 支持 PostgreSQL 数据库
    ASP.NET Aries 高级开发教程:主题样式及多语言(标签化控制)
    ASP.NET Aries 高级开发教程:Excel导入之代码编写(番外篇)
    ASP.NET Aries 高级开发教程:Excel导入配置之规则说明(下)
  • 原文地址:https://www.cnblogs.com/hhq80/p/611171.html
Copyright © 2011-2022 走看看