///数据库(mysql)连接类MySQLConnect
///若要使用请安装mysql或者将相应的代码改为Sql数据库操作代码
///“*”部分为数据库的连接的ID和密码,自行添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
namespace测试001
{
class MySQLConncet
{
private string MySqlCon = "Database=mysql;DataSource=localhost;UserID=*****;"
+ "Password=******;CharSet=utf8;port=3306";
public DataTable ExecuteQuery(string sqlStr)
{
MySqlCommand cmd;
MySqlConnection con;
MySqlDataAdapter msda;
MySqlDataReader myReader;
DataTable InformationTable;
con = new MySqlConnection(MySqlCon);
cmd = new MySqlCommand(sqlStr,con);
msda = new MySqlDataAdapter(cmd);
InformationTable = new DataTable();
try
{
con.Open();
cmd.CommandType = CommandType.Text;
msda.Fill(InformationTable);
myReader = cmd.ExecuteReader();
con.Close();
return InformationTable;
}
catch (MySqlException ex)
{
Console.WriteLine(ex.Message);
return InformationTable;
}
finally
{
con.Close();
}
}
}
}
///分页显示代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
//数据库是Mysql
namespace测试001
{
///<summary>
///分页显示
///</summary>
public partial class frmSp : Form
{
int pageSize = 0; //每页显示行数
int nMax =0; //总记录数
int pageCount = 0; //页数
int pageCurrent = 0; //当前页号
int nCurrent = 0; //当前记录行
DataSet ds = new DataSet();
DataTable dtInfor= new DataTable();
public frmSp()
{
InitializeComponent();
}
private void frmSp_Load(object sender, EventArgs e)
{
this.splitContainer1.Dock= DockStyle.Fill;
this.dgvInfor.Dock= DockStyle.Fill;
this.splitContainer1.IsSplitterFixed= true;
this.splitContainer1.FixedPanel= FixedPanel.Panel1;
string str = "SELECT * FROM `t_user_info`";
MySQLConncet mysqlCons = new MySQLConncet();
this.dtInfor= mysqlCons.ExecuteQuery(str);
ds = new DataSet();
ds.Tables.Add(dtInfor);
dtInfor = ds.Tables[0];
InitDataSet();
}
private void InitDataSet()
{
pageSize = 20;//初始化每页显示行数
nMax = dtInfor.Rows.Count;
pageCount = (nMax / pageSize);//总页数
if (nMax %pageSize > 0)
++pageCount;
pageCurrent = 1;
nCurrent = 0;
LoadData();
}
private void LoadData()
{
int nStartPostion = 0;
int nEndPoston = 0;
DataTable dtTemp= dtInfor.Clone();//克隆DataTable结构框架
if(pageCurrent == pageCount)
{
nEndPoston = nMax;
}
else
{
nEndPoston = pageSize *pageCurrent;//下一页的最后一个位置
}
nStartPostion = nCurrent;
tsp_Label1.Text = "/" +pageCount.ToString();
tsp_txtBox1.Text = Convert.ToString(pageCurrent);
for (int i =nStartPostion; i < nEndPoston; ++i)
{
dtTemp.ImportRow(dtInfor.Rows[i]);
nCurrent++;
}
bdsInfor.DataSource = dtTemp;
bdnInfor.BindingSource = bdsInfor;
dgvInfor.DataSource = bdsInfor;
}
private void bdnInfor_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if(e.ClickedItem.Text == "关闭")
this.Close();
if(e.ClickedItem.Text == "上一页")
{
if(pageCurrent <= 1)
{
//pageCurrent++;
MessageBox.Show("已经是第一页", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
pageCurrent--;
nCurrent = pageSize *(pageCurrent - 1);
}
LoadData();
}
if(e.ClickedItem.Text == "下一页")
{
if(pageCurrent >= pageCount)
{
MessageBox.Show("已经是最后一页", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
pageCurrent++;
nCurrent = pageSize *(pageCurrent - 1);
}
LoadData();
}
}
private void tsp_txtBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
pageCurrent = Convert.ToInt32(tsp_txtBox1.Text);
nCurrent = pageSize *(pageCurrent - 1);
if(pageCurrent > pageCount || pageCurrent < 1)
{
MessageBox.Show("页码输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
LoadData();
}
}
}
}
///界面设置,控件如下图
///运行效果
///demo下载链接https://download.csdn.net/download/ricardomtan/10524685