win7.默认分页文件
(win7页文件设置)
1、功能需求
本实例将通过c# winform实现简单的分页功能所需的基本知识SQL语句,c#语言基础和c# winform一些简单的知识。
2、界面设计这是一个简单的分页查询界面,可以输入任何字段进行查询,在数据准备中提到,整个界面如图1所示。
图1
中间显示是一个DataGridView,编辑好列和id,SortMode选择Automatic,意思是所有列自动铺满DataGridView,如图2所示。
图2
3、数据准备本实例涉及到删查改,因此要有数据表以及对数据表进行操作的代码。如图3所示,数据库表非常简单,对应四个字段。
图3
later_back对应数据库的模型类操作代码如下:
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Bean;
namespace WindowsFormsApp1.SqlHelper
{
class LaterBackHelper
{
public LaterBackHelper(){}
public void insert(LaterBack laterBack)
{
string sql = "insert into later_back(dormitory_id,student_no,time,reason) values('" laterBack.Dormitory_id "','" laterBack.Student_no "'," "'" laterBack.Time "',"
"'" laterBack.Reason "')";
try
{
int iRet = SqlHelperBase.ExecuteSql(sql);
if (iRet > 0)
{
MessageBox.Show(";新增成功", ";系统提示");
}
else
{
MessageBox.Show(";新失败", ";系统提示");
}
}
catch (Exception)
{
throw;
}
}
public int update(LaterBack laterBack)
{
string sql = "update later_back set reason='" laterBack.Reason "' where student_no= '" laterBack.Student_no "' and"
" time = '" laterBack.Time "' and " " dormitory_id= '" laterBack.Dormitory_id "'";
try
{
int iRet = SqlHelperBase.ExecuteSql(sql);受影响的行数在这里返回,为int值。可根据返回值判断插入是否成功。
if (iRet > 0)
{
MessageBox.Show(";修改成功", ";系统提示");
}
else
{
MessageBox.Show(";修改失败", ";系统提示");
}
return iRet;
}
catch (Exception)
{
throw;
}
}
public int delete(LaterBack laterBack)
{
string sql = "delete from later_back where dormitory_id='" laterBack.Dormitory_id "' and" " student_no= '" laterBack.Student_no "' and"
" time = '" laterBack.Time "' and" " reason= '" laterBack.Reason "'";
try
{
int iRet = SqlHelperBase.ExecuteSql(sql);受影响的行数在这里返回,为int值。可根据返回值判断插入是否成功。
if (iRet > 0)
{
MessageBox.Show(";成功删除", ";系统提示");
}
else
{
MessageBox.Show(";删除失败", ";系统提示");
}
return iRet;
}
catch (Exception)
{
throw;
}
}
public List<LaterBack> getAllLaterBacks()
{
List<LaterBack> laterBacks= new List<LaterBack>();
string sql = "select * from later_back";
MySqlDataReader mySqlDataReader = SqlHelperBase.ExecuteReader(sql);
while (mySqlDataReader.Read())
{
LaterBack laterBack = new LaterBack(int.Parse(mySqlDataReader[0].ToString()), int.Parse(mySqlDataReader[1].ToString()), mySqlDataReader[2].ToString(),
mySqlDataReader[3].ToString());
laterBacks.Add(laterBack);
}
mySqlDataReader.Close();
return laterBacks;
}
public List<LaterBack> getAllLaterBacks(int student_no)
{
List<LaterBack> laterBacks= new List<LaterBack>();
string sql = "select * from later_back where student_no ='" student_no "'";
MySqlDataReader mySqlDataReader = SqlHelperBase.ExecuteReader(sql);
while (mySqlDataReader.Read())
{
LaterBack laterBack = new LaterBack(int.Parse(mySqlDataReader[0].ToString()), int.Parse(mySqlDataReader[1].ToString()), mySqlDataReader[2].ToString(),
mySqlDataReader[3].ToString());
laterBacks.Add(laterBack);
}
mySqlDataReader.Close();
return laterBacks;
}
public DataSet getAllDataSet()
{
string sql = "select * from later_back";
return SqlHelperBase.GetDataSet(sql);
}
//模糊查询
public DataSet getDataSet(LaterBack laterBack)
{
string sql="";
if(laterBack.Student_no != -1)
{
if (laterBack.Dormitory_id != -1) sql = "select * from later_back where dormitory_id like '%" laterBack.Dormitory_id
"%' and student_no like '%" laterBack.Student_no "%' and time like '%" laterBack.Time
"%' and reason like '%" laterBack.Reason "%'";
else sql = "select * from later_back where student_no like '%" laterBack.Student_no "%' and time like '%" laterBack.Time
"%' and reason like '%" laterBack.Reason "%'";
}
else
{
if (laterBack.Dormitory_id != -1) sql = "select * from later_back where dormitory_id like '%" laterBack.Dormitory_id
"