| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 467 人关注过本帖
标题:大数量查询分页显示 微软的解决办法
收藏  订阅  推荐  打印
chb592657370
Rank: 2
来自:武汉交通职业学院
等级:注册会员
帖子:120
积分:1414
注册:2006-12-9
大数量查询分页显示 微软的解决办法

微软的解决办法
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());
}
}
 
 
搜索更多相关主题的帖子: 微软  数量  办法  查询  
2008-5-12 17:39
lavender_lhy
Rank: 1
来自:北京
等级:新手上路
帖子:12
积分:266
注册:2008-5-27

这个是微软哪里的解决方案?
2008-5-29 17:00
gupiao175
Rank: 3Rank: 3
等级:中级会员
帖子:292
积分:3350
注册:2007-6-27
楼主能解释一下吗?就各段代码,谢谢!

楼主能解释一下吗?就各段代码,谢谢!

事上无难事,只盼有心人!
2008-5-29 17:05
hebingbing
Rank: 12Rank: 12Rank: 12
来自:泰山学院
等级:版主
威望:10
帖子:3065
积分:32368
注册:2007-10-22

那上面不都有注释了吗?
英语没有比我差的了吧……

【www.bcsnjd.cn】编程少年基地:提供在线视频教程、电子图书、程序源码等众多资源,欢迎访问
本人免费做asp.net,c#,asp,vb等毕业设计。要求想法极具创新QQ:491131231
2008-5-29 17:07
gupiao175
Rank: 3Rank: 3
等级:中级会员
帖子:292
积分:3350
注册:2007-6-27

我英语确实很差,到现在连4级都没考过!

事上无难事,只盼有心人!
2008-5-29 17:23
hebingbing
Rank: 12Rank: 12Rank: 12
来自:泰山学院
等级:版主
威望:10
帖子:3065
积分:32368
注册:2007-10-22

我也一样啊……
不过马上又要考了,听说有买的答案……
呵呵……

【www.bcsnjd.cn】编程少年基地:提供在线视频教程、电子图书、程序源码等众多资源,欢迎访问
本人免费做asp.net,c#,asp,vb等毕业设计。要求想法极具创新QQ:491131231
2008-5-29 17:30
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.059484 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved