@[TOC]用C# 语言实现MYSQL 真分页(wcf框架)
真分页与假分页
假分页
从数据库中取出所有的数据,然后分页在界面上显示。访问一次数据库,但由于选择的数据量比较大,所以第一次花费时间比较长,但之后每一页的显示都是直接、快速的,避免对数据库的多次访问。
真分页
确定要显示的数量和内容,然后每次都去数据库取出该少量数据,优点是数据量小,缺点是访问数据库频繁。在大型网站中往往采用真分页,比如百度的图片获取。
为提高效率,我们使用真分页,
SQL帮助类
public class MySQLHelper { //public MySqlConnection GetConn() //{ // // MySqlConnection connect = new MySqlConnection(constructorString); // return connect; //} private MySqlConnection conn = null;//数据库连接 private MySqlCommand cmd = null;//对数据库进行操作,也就是增删改查 private MySqlDataReader sdr = null;//从数据库中只读 public MySQLHelper() { string connStr = "server=192.168.60.54;port=3306;User Id=sentry;password=sentry;Database=sentry_system_dev;SslMode=none;Charset=utf8;"; conn = new MySqlConnection(connStr);//打开数据库的连接,此时并未连接成功 } /// <summary> /// 判断数据源的连接情况 /// </summary> /// <returns>连接</returns> private MySqlConnection GetConn() { if (conn.State == ConnectionState.Closed) //枚举ConnectionState,用于描述数据源当前的连接状态 { conn.Open();//连接数据库 } return conn; } /// <summary> /// 通过ExecuteQuery()方法。执行带参的SQL查询语句或存储过程 /// </summary> /// <param name="cmdText">查询文本</param> /// <param name="paras">参数</param> /// <param name="ct">执行类型</param> /// <returns>受影响的行</returns> public DataTable ExecuteQuery(string cmdText, MySqlParameter[] paras, CommandType ct) { DataTable dt = new DataTable();//实例化数据库内存中的一个表 cmd = new MySqlCommand(cmdText, GetConn()); //cmdText:查询的文本;getConn():连接数据库的方法 cmd.CommandType = ct;//指定命令对象的执行形式,默认为text cmd.Parameters.AddRange(paras);//将参数加入到参数集中 using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) //在执行命令时,将关联 Connection 对象已关闭时关联 DataReader 对象已关闭 { dt.Load(sdr); //将提供的数据源中的值填充到DataTable,如果已存在,则合并 } return dt; //返回表到DAL层 } /// <summary> /// 执行不带参数的SQL查询语句或者存储过程 /// </summary> /// <param name="cmdText">查询SQL语句或存储过程</param> /// <param name="ct">命令类型</param> /// <returns>返回受影响的行数</returns> public DataTable ExecuteQuery(string cmdText, CommandType ct) { DataTable dt = new DataTable(); cmd = new MySqlCommand(cmdText, GetConn()); cmd.CommandType = ct; using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } return dt; } /// <summary> /// 执行不带参数的数据库操作或者存储过程heihei /// </summary> /// <param name="cmdText">增删改查操作</param> /// <param name="ct">命令类型</param> /// <returns>返回受影响的行数</returns> public int ExecuteNonQuery(string cmdText, CommandType ct) { int res; cmd = new MySqlCommand(cmdText, GetConn()); cmd.CommandType = ct; res = cmd.ExecuteNonQuery(); if (conn.State == ConnectionState.Open) { conn.Close(); } return res; } /// <summary> /// 执行带参数的数据库操作或者存储过程 /// </summary> /// <param name="cmdText">增删改查操作</param> /// <param name="paras">要查询的参数</param> /// <param name="ct">命令类型</param> /// <returns>返回受影响的行数</returns> public int ExecuteNonQuery(string cmdText, MySqlParameter[] paras, CommandType ct) { int res; using (cmd = new MySqlCommand(cmdText, GetConn())) { cmd.CommandType = ct; cmd.Parameters.AddRange(paras); res = cmd.ExecuteNonQuery(); } return res; } }
实体层:
public class FaceImageEntity
{
private string userID;//用户姓名
private string imageByte;//图片字节码 public byte[] ImageByte; private string faceToken;//图片的唯一标识 public string UserID { get { return userID; } set { userID = value; } } public string Sex { get; set; } public string UserName { get; set; } public string FaceToken { get { return faceToken; } set { faceToken = value; } } public DateTime CreatTime { get; set; } public DateTime UpadateTime { get; set; } public int ISDelet { get; set; } }
D层:
/// <summary> /// 通过输入页数大小与第多少页来获取数据 /// </summary> /// <param name="inputPageSidze">输入每页的大小</param> /// <param name="inputpageCode">要查询第几个的页数</param> /// <returns></returns> public List<FaceImageEntity> PagingSelectAllImage(int inputPageSize, int inputpageCode) { List<FaceImageEntity> faceToken =new List<FaceImageEntity>(); MySqlParameter[] sqlparams = new MySqlParameter[] { new MySqlParameter("@is_Delet",0), }; string sql= "SELECT * FROM t_faceimage_info where is_Delet=@is_Delet order by create_Time desc limit " + (inputpageCode - 1) * inputPageSize + "," + inputPageSize; DataTable dataTable = sqlHelper.ExecuteQuery(sql, sqlparams, CommandType.Text); foreach (DataRow dataRow in dataTable.Rows) { faceToken.Add(new FaceImageEntity { FaceToken = Convert.ToString(dataRow["face_Token"]), ImageByte = (byte[])dataRow["image_Byte"], UserID = Convert.ToString(dataRow["user_ID"]), ISDelet = Convert.ToInt32(dataRow["is_Delet"]), Sex = Convert.ToString(dataRow["sex"]), UserName = Convert.ToString(dataRow["user_Name"]), CreatTime = Convert.ToDateTime(dataRow["create_Time"]), UpadateTime = Convert.ToDateTime(dataRow["update_Time"]) }); ; } return faceToken; }
B层:
/// <summary> /// 通过输入每页大小与获取第几页, /// </summary> /// <param name="inputPageSidze">每页大小</param> /// <param name="inputpageCode">查询第几页</param> /// <returns>返回数据</returns> public List<FaceImageEntity> PagingSelectAllImage(int inputPageSidze, int inputpageCode) { return FaceImageDAL.PagingSelectAllImage(inputPageSidze,inputpageCode); }