版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kansas_Jason/article/details/53783336
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private String connString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\AdventureWorksDW2008\AdventureWorksDW_Data.mdf;
Integrated Security=True;Connect Timeout=30;User Instance=True";
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataAdapter adapter = null;
private SqlDataReader reader = null;
private DataSet ds = new DataSet();
private int pageSize = 5;
private int rowNums = 0;
private int currentPage = 0;
public void FillDataGridView() {
try
{
conn = new SqlConnection(connString);
if (conn.State != ConnectionState.Open) {
conn.Open();
}
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT TOP ("+pageSize+") EmployeeKey, ParentEmployeeKey, ParentEmployeeNationalIDAlternateKey,
EmployeeNationalIDAlternateKey FROM DimEmployee WHERE (EmployeeKey NOT IN (SELECT TOP ("+ pageSize*currentPage +") EmployeeKey FROM DimEmployee AS DimEmployee_1)) ORDER BY EmployeeKey";
adapter = new SqlDataAdapter(cmd);
ds.Clear();
adapter.Fill(ds,"DimEmployee");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "DimEmployee";
}
catch (Exception)
{
throw;
}
}
public void GetNum() {
try
{
conn = new SqlConnection(connString);
if (conn.State != ConnectionState.Open) {
conn.Open();
}
cmd = conn.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) from DimEmployee";
reader = cmd.ExecuteReader();
if (reader.Read()) {
rowNums = reader.GetInt32(0);
}
}
catch (Exception)
{
throw;
}
}
public Form1()
{
InitializeComponent();
GetNum();
}
private void button1_Click(object sender, EventArgs e)
{
currentPage = 0;
FillDataGridView();
}
private void button2_Click(object sender, EventArgs e)
{
if (currentPage > 0)
{
currentPage--;
}
else {
button2.Enabled = false;
}
FillDataGridView();
button3.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
if (currentPage < rowNums/pageSize)
{
currentPage++;
}
else
{
button3.Enabled = false;
}
FillDataGridView();
button2.Enabled = true;
}
private void button4_Click(object sender, EventArgs e)
{
currentPage = rowNums / pageSize;
FillDataGridView();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (conn.State != ConnectionState.Closed) {
conn.Close();
}
if (reader != null)
reader.Close();
}
}
}