先看效果图:
代码如下:
在这里插入代码片
//页行数
public int bookSeach_pagesize = 10;
//当前页
public int bookSeach_pageIndex = 1;
//总页数
public int bookSeach_pageCount;
//-----图书查询---图书查询分页方法
public void BookSeach_Pager()
{
//按钮禁用
SimpleButton_BookSeach_First.Enabled = false;//首页
SimpleButton_BookSeach_PrePager.Enabled = false;//上一页
SimpleButton_BookSeach_NextPager.Enabled = false;//下一页
SimpleButton_BookSeach_Next.Enabled = false;//尾页
SimpleLabelItem_BookSeach_AllCount.Text = "图书总数:" + 0 + "本";
//查询数据库bookSeach_pagesize
//获取开始的记录数
int startIndex = (bookSeach_pageIndex - 1) * bookSeach_pagesize + 1;
//1---10
//11--20
//获取结束的记录数
int endIndex = bookSeach_pageIndex * bookSeach_pagesize;
//异步执行代码
if (this.IsHandleCreated) //指示是否关联句柄
{
this.BeginInvoke(new EventHandler(delegate
{
string resultes = sqlcmd.TestPing();
if (resultes.Equals("1"))
{
//网络连接成功
bool con_falg = sqlcmd.TestConnection(sqlcmd.mysql);
if (con_falg)
{
//true连接数据库成功
string fisbn, author, iclass, bookname, locals;
Bookinfo bf = new Bookinfo();
UserList = new List<Bookinfo>();
fisbn = f1_f_txt_isbn.Text.Length != 0 ? " and a.isbn like '%" + f1_f_txt_isbn.Text.Trim() + "%'" : "";
author = f1_txt_f_author.Text.Length != 0 ? " and author like '%" + f1_txt_f_author.Text.Trim() + "%'" : ""; ;
iclass = f1_txt_f_class.Text.Length != 0 ? " and a.classs like '%" + f1_txt_f_class.Text.Trim() + "%'" : "";
bookname = f1_txt_f_bookname.Text.Length != 0 ? " and a.bookname like '%" + f1_txt_f_bookname.Text.Trim() + "%'" : "";
locals = f1_f_txt_local.Text.Length != 0 ? " and b.locals like '%" + f1_f_txt_local.Text.Trim() + "%'" : "";
DataTable rs = new DataTable();
//查询总数
string sqlcount = "SELECT count(*) FROM bookinfo a RIGHT JOIN localbook b on a.isbn=b.isbn";
string sql =
"SELECT a.isbn,a.bookname,price,author,publiss,bookimg,b.locals,contents,a.classs FROM bookinfo a RIGHT JOIN localbook b on a.isbn=b.isbn";
string sqlwhere = fisbn + author + iclass + bookname + locals;
if (sqlwhere != "")
{
sql = sql + " where " + sqlwhere.Substring(4); ;
sqlcount = sqlcount +" where " +sqlwhere.Substring(4);
}
sqlcount = sqlcount + " limit 1";
//关闭数据库
comm.Close();
//总行数
int row = comm.GetRecordCount(sqlcount);
//关闭数据库
Close_DB();
SimpleLabelItem_BookSeach_AllCount.Text = "图书总数:" + row + "本";
//获取总页数
if (row > 0)
{
if (row > bookSeach_pagesize)
{
if (row % bookSeach_pagesize > 0)
{
bookSeach_pageCount = row / bookSeach_pagesize + 1;
}
else
{
bookSeach_pageCount = row / bookSeach_pagesize;
}
SimpleLabelItem_BookSeach_Pager.Text = string.Format("第{0}页,共{1}页", bookSeach_pageIndex, bookSeach_pageCount);
}
else
{
bookSeach_pageCount = 1;
endIndex = row;
SimpleLabelItem_BookSeach_Pager.Text = string.Format("第{0}页,共{1}页", bookSeach_pageIndex, bookSeach_pageCount);
}
}
else
{
bookSeach_pageCount = 0;
SimpleLabelItem_BookSeach_Pager.Text = string.Format("第{0}页,共{1}页", bookSeach_pageIndex, bookSeach_pageCount);
}
string oisbn = "";
string nisbn = "";
//关闭数据库
Close_DB();
//打开数据库
comm = Connect_DB();
sql