大数量查询分页显示

原创 2007年09月28日 13:48:00
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

public class PagingSample: Form
{
// Form controls.
Button prevBtn = new Button();
Button nextBtn = new Button();

static DataGrid myGrid = new DataGrid();
static Label pageLbl = new Label();

// Paging variables.
static int pageSize = 10; // Size of viewed page.
static int totalPages = 0; // Total pages.
static int currentPage = 0; // Current page.
static string firstVisibleCustomer = ""; // First customer on page to determine location for move previous.
static string lastVisibleCustomer = ""; // Last customer on page to determine location for move next.

// DataSet to bind to DataGrid.
static DataTable custTable;

// Initialize connection to database and DataAdapter.
static SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
static SqlDataAdapter custDA = new SqlDataAdapter("", nwindConn);
static SqlCommand selCmd = custDA.SelectCommand;

public static void GetData(string direction)
{
// Create SQL statement to return a page of records.
selCmd.Parameters.Clear();

switch (direction)
{
case "Next":
selCmd.CommandText = "SELECT TOP " + pageSize + " CustomerID, CompanyName FROM Customers " +
"WHERE CustomerID > @CustomerId ORDER BY CustomerID";
selCmd.Parameters.Add("@CustomerId", SqlDbType.VarChar, 5).Value = lastVisibleCustomer;
break;
case "Previous":
selCmd.CommandText = "SELECT TOP " + pageSize + " CustomerID, CompanyName FROM Customers " +
"WHERE CustomerID < @CustomerId ORDER BY CustomerID DESC";
selCmd.Parameters.Add("@CustomerId", SqlDbType.VarChar, 5).Value = firstVisibleCustomer;
break;
default:
selCmd.CommandText = "SELECT TOP " + pageSize + " CustomerID, CompanyName FROM Customers ORDER BY CustomerID";

// Determine total pages.
SqlCommand totCMD = new SqlCommand("SELECT Count(*) FROM Customers", nwindConn);
nwindConn.Open();
int totalRecords = (int)totCMD.ExecuteScalar();
nwindConn.Close();
totalPages = (int)Math.Ceiling((double)totalRecords / pageSize);

break;
}

// Fill a temporary table with query results.
DataTable tmpTable = new DataTable("Customers");
int recordsAffected = custDA.Fill(tmpTable);

// If table does not exist, create it.
if (custTable == null)
custTable = tmpTable.Clone();

// Refresh table if at least one record returned.
if (recordsAffected > 0)
{
switch (direction)
{
case "Next":
currentPage++;
break;
case "Previous":
currentPage--;
break;
default:
currentPage = 1;
break;
}

pageLbl.Text = "Page " + currentPage + " of " + totalPages;

// Clear rows and add new results.
custTable.Rows.Clear();

foreach (DataRow myRow in tmpTable.Rows)
custTable.ImportRow(myRow);

// Preserve first and last primary key values.
DataRow[] ordRows = custTable.Select("", "CustomerID ASC");
firstVisibleCustomer = ordRows[0][0].ToString();
lastVisibleCustomer = ordRows[custTable.Rows.Count - 1][0].ToString();
}
}



public PagingSample()
{
// Initialize controls and add to form.
this.ClientSize = new Size(360, 274);
this.Text = "NorthWind Data";

myGrid.Location = new Point(10,10);
myGrid.Size = new Size(340, 220);
myGrid.AllowSorting = true;
myGrid.CaptionText = "NorthWind Customers";
myGrid.ReadOnly = true;
myGrid.AllowNavigation = false;
myGrid.PreferredColumnWidth = 150;

prevBtn.Text = "<<";
prevBtn.Size = new Size(48, 24);
prevBtn.Location = new Point(92, 240);
prevBtn.Click += new EventHandler(Prev_OnClick);

nextBtn.Text = ">>";
nextBtn.Size = new Size(48, 24);
nextBtn.Location = new Point(160, 240);

pageLbl.Text = "No Records Returned.";
pageLbl.Size = new Size(130, 16);
pageLbl.Location = new Point(218, 244);

this.Controls.Add(myGrid);
this.Controls.Add(prevBtn);
this.Controls.Add(nextBtn);
this.Controls.Add(pageLbl);
nextBtn.Click += new EventHandler(Next_OnClick);


// Populate DataSet with first page of records and bind to grid.
GetData("Default");
DataView custDV = new DataView(custTable, "", "CustomerID", DataViewRowState.CurrentRows);
myGrid.SetDataBinding(custDV, "");
}



public static void Prev_OnClick(object sender, EventArgs args)
{
GetData("Previous");
}

public static void Next_OnClick(object sender, EventArgs args)
{
GetData("Next");
}
}



public class Sample
{
static void Main()
{
Application.Run(new PagingSample());
}
}  
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

easyui-datagrid使用load()保持分页显示数量

easyui snippet 2017-6-20 14:41:26 我之前用easyui-datagrid都是使用的下面这种方法加载数据,查询按钮就是再调一次这个方法。这样做有个小问题,就是...

数据库查询结果分页显示思路

数据库查询结果分页显示思路,通过计算Result Set指针位置实现分页显示//设置页面显示数量 int pageSize; //获取要显示页面 int currentPage; //需要定义的参数...

【ASP】对数据库查询的内容分页显示算法的改进

上次写的《【ASP】数据库查询的内容分页显示》(点击打开链接)中的实现算法虽然能够在一个特定的表中实现了预想的效果, 但是这一算法移植到其他系统之后,发现通用性不强 首先此分页算法的具体实施是通过...

JSP分页,查询结果分页显示

原文地址:http://wenku.baidu.com/view/fe2933e79b89680203d825b5.html

php数据库查询分页显示操作(一)

PHP数据库查询分页显示操作(一) 在网站后台开发过程中,必不可少的就是与数据库连接进行Create、Update、Retrieve、Delete操作。在信息表显示操作的时候,就会用到分页技术。 ...

java web中使用过滤器实现数据查询的分页显示

jsp: 首页     上一页     第${page}页     下一页     ...

前台jsp获取后台查询的结果集数据并实现分页显示

前台获取后台查询的结果并分页显示。 1、使用ajax异步请求,发送post请求到url,获取结果参数data(data被dao层处理成json对象返回) 2、返回的结果: 在da...

PHP和MYSQL查询和分页显示

有个数据表,里面包含多个字段,在页面上首先有个下拉菜单选择字段类型 然后一个文本框(里面输入关键字)点击查询,并将查询的结果分页显示。该怎么实现?比如我数据表里面有“文档名称”“文档编号”“文档备注信...

get方式获取页码,id号分页显示查询结果

<?php require "../../class/connect.php"; require "../../data/dbcache/class.php"; require "../../c...

php分页显示双表查询的内容

代码部分: header("Content-type: text/html; charset=utf-8"); error_reporting(0); error_reporting...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)