当前位置: 首页 > 编程日记 > 正文

大数量查询分页显示

微软的解决办法


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());
}
}

相关文章:

Sep 26 09:22:41 ck01 kernel: Buffer I/O error on device sda2, logical block 2

错误 kernel: sd 0:2:0:0: SCSI error: return code kernel: end_request: I/O error, dev sda, sector 2308509 kernel: Buffer I/O error on device sda2, logical block 2 kernel: Buffer I/O error on device sda2, logical block 2 硬盘扇区问题&#xff1f; 更换一块硬盘…

启信宝CEO陈青山:AI+大数据驱动下的金融新生态

人工智能怎样潜移默化影响金融业务&#xff1f;合合信息联合创始人、启信宝CEO陈青山在2020世界人工智能大会云端峰会.未来金融论坛上做了“AI大数据&#xff0c;共创金融新生态”的主题演讲&#xff0c;讲述了如何通过数字化构建一个虚拟世界&#xff0c;协助金融类企业建立新…

新手安装ruby on rails(ror)的成功必备手册

2019独角兽企业重金招聘Python工程师标准>>> 如何快速正确的安装 Ruby, Rails 运行环境 每一位使用windows系统来进行ROR开发项目的都是这个世界上折翼的天使。对于新入门的开发者&#xff0c;如何在windows系统上安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是…

Logback学习笔记1

Logback介绍 Logback 分为三个模块&#xff1a;Core、Classic 和 Access。Core模块是其他两个模块的基础。 Classic模块扩展了core模块。 Classic模块相当于log4j的显著改进版。Logback-classic 直接实现了 SLF4J API。 要引入logback&#xff0c;由于Logback-classic依赖slf4j…

微软亚洲研究院副院长刘铁岩:AI如何助力节能减排?

7月10日&#xff0c;在微软举办的WAIC●2020人工智能予力永续未来论坛&#xff0c;微软亚洲研究院副院长刘铁岩围绕 AI 在节能减排领域的应用&#xff0c;探讨了如何推进整个社会的可持续发展。 刘铁岩指出&#xff0c;人工智能在助力节能环保方面具有巨大潜力。例如&#xff…

如何在域控制器上安装asp.net?

作者&#xff1a;网际浪子专栏&#xff08;曾用名littlehb&#xff09; http://blog.csdn.net/littlehb/在域控制器或备份域控制器上安装 Microsoft Visual Studio .NET 或 Microsoft .NET 框架后&#xff0c;如果您尝试运行 ASP.NET 应用程序&#xff0c;则浏览器…

前端思想实现:面向UI编程_____前端框架设计开发

引子&#xff0c;我去小说看多了&#xff0c;写博客竟然写引子了&#xff01;&#xff01;&#xff01;不过&#xff0c;没引子不知道怎么写了。言归正传吧&#xff0c;前端这个职业&#xff0c;也就这几年刚刚火起来的职业&#xff0c;以前那个混乱的年代&#xff0c;前端要么…

如何检查当TextBox设置为多行时是不是超长了?

<asp:RegularExpressionValidator id"revTextBox1" runat"server" ForeColor"Red" Display"Dynamic" ControlToValidate"textBox1" ValidationExpression"(/w|/W){1,100}">格式错误-只能输入不超过100个字符…

仅用2年过渡到自研ARM芯片,苹果的底气从何而来?

作者 | Steven Sinofsky译者 | 弯月&#xff0c;责编 | 屠敏题图 | 东方 IC出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;今年 WWDC 2020 上&#xff0c;苹果宣布自研 ARM 芯片——Apple Silicon&#xff0c;对于苹果而言&#xff0c;Apple Silicon很重要&#…

从“创业输家”到“创智赢家”

年轻就是创业的资本&#xff0c;但同时还要不怕失败&#xff0c;经得起挫折&#xff01;”在上海财经大学的演讲厅内&#xff0c;彭震以自己亲身的经历&#xff0c;向在场的学生们介绍了自己创业背后的艰辛。彭震&#xff0c;现任项丰投资公司董事长&#xff0c;作为首届“ 创智…

2.正则表达式

在计算机科学中&#xff0c;正则表达式是这样解释的&#xff1a;它是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。在很多文本编辑器或其他工具里&#xff0c;正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容。许多程序设计语言都支持利用…

基于监督学习+自监督学习的智能抠图,精确到发丝 | CVPR 2020

来源 | AI算法与图像处理&#xff08;ID:AI_study&#xff09;华盛顿大学的研究者最近发表的论文在CVPR 2020提供了一个新的和简单的智能抠图方法。你可以在家里做这些日常设置&#xff0c;使用固定或手持相机。我们的方法也是最先进的&#xff0c;给出的输出可比专业的结果。在…

安装Jpype

1、下载JPype-0.5.4.2 2、 python setup.py install error: gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE2 -fexceptions -fstack-protector --paramssp-buffer-size4 -m64 -mtunegeneric -D_GNU_SOURCE -fPIC -fPIC -I/usr/lib/jvm…

SQL SERVER中带参数的返回

作者&#xff1a;网际浪子专栏&#xff08;曾用名littlehb&#xff09; http://blog.csdn.net/littlehb/ CREATE PROCEDURE LoginUser loginUN char(50) OUTPUT, loginPW char(40)ASif loginPW (select [password] from users where usernameloginUN) return 0;else ret…

Android Studio 编译单个module

前期自己要把gradle环境变量配置好 在Terminal中gradle命令行编译apk 输入gradle assembleRelease 会编译全部module编译单个modulecd ./xiruan //xiruan是module的名字 //意思是进入module目录gradle assembleRelease //编译出自己签名的apk //签名环境也要配置好gradle ass…

linux下文件字符集转化实战篇

linux下文件字符集转化实战篇------------------------------------------为什么转换编码&#xff0c;起因&#xff1a;分析应用程序日志&#xff0c;文件编码格式gb2312&#xff08;含中文字符&#xff09;&#xff0c;通过linux shell脚本从日志中提取出需要的字段并生成有固…

如何得到数据库中所有表字段及字段中文描述

如何得到数据库中所有表字段及字段中文描述以下资料,通过csdn的一位师兄从SQL版主那得到:sql中SELECT (case when a.colorder1 then d.name else end) N表名, a.colorder N字段序号, a.name N字段名, (case when COLUMNPROPERTY( a.id,a.name,IsIdentity)1 then √else end)…

它估值25亿!被马云领投,是华为“老战友”,网友:也许股价能超茅台!

最近一条新闻被炒的沸沸扬扬&#xff1a;十年以来中国最大IPO&#xff0c;中芯国际将融资532亿元&#xff01;何为IPO&#xff1f;翻译即为一家公司第一次向全社会公开售出它的股份。买的人越多&#xff0c;代表着社会对其信心越大。为什么2020年&#xff0c;能爆发这样1场最大…

改變人生的21種好習慣

當一個人生活枯燥的時候&#xff0c; 他忘了用心體會是一種習慣。 當一個人覺得人生乏味的時候&#xff0c; 他忘了培養幽默是一種習慣。 當一個人體力日差的時候&#xff0c; 他忘了運動建身是一種習慣。 當一個人工作疲憊的時候&#xff0c; 他忘了認真休息是一種習慣。 當一…

Python 编程语言的核心是什么?

作者 | Brett Cannon译者 | 弯月&#xff0c;责编 | 王晓曼头图 | CSDN下载自视觉中国出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;为什么要问这个问题&#xff1f;我想要用Python实现WebAssembly&#xff0c;这并不是什么秘密。这不仅可以让Python进入浏览器&a…

关闭页面不用提示的方法

Response.Write("<script>window.openernull;window.close();</script>") ;

VIM 必知必会12大类型操作

2019独角兽企业重金招聘Python工程师标准>>> VIM是在unix/linux系统下最常用的文本编辑器,呵呵……这就不用我再强调了&#xff0c;现在直接贴上vim的最常用的操作&#xff0c;方便日后温习及查阅&#xff01; 1、插入 i 在光标前插入 I 在行首插入 a 在光标后插入 …

前后端分离的思考与实践(三)

Midway-ModelProxy — 轻量级的接口配置建模框架前言使用Node做前后端分离的开发模式带来了一些性能及开发流程上的优势(见《前后端分离的思考与实践 一》), 但同时也面临不少挑战。在淘宝复杂的业务及技术架构下&#xff0c;后端必须依赖Java搭建基础架构&#xff0c;同时提供…

替换HTML代码

将一些HTML替换掉 eg:strContentstrContent.Replace("&","&amp");strContentstrContent.Replace("","");strContentstrContent.Replace("<","&lt");strContentstrContent.Replace(">&quo…

什么是线程安全?

http://baike.baidu.com/view/1298606.htm 线程安全 目录 什么是线程安全&#xff1f;举例线程安全性线程安全程度不可变线程安全有条件的线程安全线程兼容线程对立什么是线程安全&#xff1f;举例线程安全性线程安全程度不可变线程安全有条件的线程安全线程兼容线程对立展开编…

中国开源大爆发进行时,你没掉队吧?

作者 | 陈利鑫头图 | CSDN 下载自东方 IC出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;从开源&#xff08;Open Source&#xff09;一词提出到如今&#xff0c;开源的概念越来越成熟&#xff0c;作为一种创造及协作模式&#xff0c;开源已经不仅仅局限于软件技术…

eclipse设置

2019独角兽企业重金招聘Python工程师标准>>> eclipse 下载 地址 http://www.eclipse.org/downloads/packages/release/Ganymede/SR2 设置 jdk 参数 jre definition vm arguments -Xms800m -Xmx800m -XX:MaxNewSize256m -XX:MaxPermSize256m -Dfile.encod…

吊打一切:YOLOv4的tricks汇总

来源 | AI算法与图像处理&#xff08;ID:AI_study&#xff09;即使是目标检测在过去几年开始成熟&#xff0c;竞争仍然很激烈。如下所示&#xff0c;YOLOv4声称拥有最先进的精度&#xff0c;同时保持高处理帧速率。它在 MS COCO数据集上&#xff0c;使用Tesla V100以接近65 FPS…

如何播放RM文件?

<EMBED SRC"MY_LIFE.RPM" WIDTH300 HEIGHT134 >Tip:SRC是可以省略的&#xff0c;当mime Type 参数如下时&#xff1a;<EMBED ...,TYPE"audio/x-pn-realaudio-plugin",...>&#xff0c;这样做会产生不可预料的结果&#xff0c;因此强烈推荐你包…

mongodb地理位置索引实现原理

地理位置索引支持是MongoDB的一大亮点&#xff0c;这也是全球最流行的LBS服务foursquare 选择MongoDB的原因之一。我们知道&#xff0c;通常的数据库索引结构是B Tree&#xff0c;如何将地理位置转化为可建立BTree的形式&#xff0c;下文将为你描述。 首先假设我们将需要索引的…