当前位置:网站首页>Using C language to realize MySQL true paging
Using C language to realize MySQL true paging
2022-07-02 08:11:00 【Ren yabing】
@[TOC] use C# Language implementation MYSQL True Pagination (wcf frame )
True paging and false paging
Fake paging
Get all the data from the database , Then the page is displayed on the interface . Access the database once , However, due to the large amount of data selected , So it takes a long time for the first time , But then every page is displayed directly 、 fast , Avoid multiple accesses to the database .
True Pagination
Determine the quantity and content to display , Then every time I go to the database to get this small amount of data , The advantage is that the amount of data is small , The disadvantage is frequent access to the database . True paging is often used in large websites , For example, Baidu's image acquisition .
To improve efficiency , We use true paging ,
SQL Helper classes
public class MySQLHelper
{
//public MySqlConnection GetConn()
//{
//
// MySqlConnection connect = new MySqlConnection(constructorString);
// return connect;
//}
private MySqlConnection conn = null;// Database connection
private MySqlCommand cmd = null;// Operate on the database , That is to say, add, delete, modify and search
private MySqlDataReader sdr = null;// Read only from the database
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);// Open the connection to the database , The connection is not successful at this time
}
/// <summary>
/// Judge the connection of the data source
/// </summary>
/// <returns> Connect </returns>
private MySqlConnection GetConn()
{
if (conn.State == ConnectionState.Closed) // enumeration ConnectionState, Used to describe the current connection status of the data source
{
conn.Open();// Connect to database
}
return conn;
}
/// <summary>
/// adopt ExecuteQuery() Method . Execute with parameters SQL Query statement or stored procedure
/// </summary>
/// <param name="cmdText"> Query text </param>
/// <param name="paras"> Parameters </param>
/// <param name="ct"> Execution type </param>
/// <returns> Affected lines </returns>
public DataTable ExecuteQuery(string cmdText, MySqlParameter[] paras, CommandType ct)
{
DataTable dt = new DataTable();// Instantiate a table in the database memory
cmd = new MySqlCommand(cmdText, GetConn()); //cmdText: The text of the query ;getConn(): How to connect to the database
cmd.CommandType = ct;// Specify the execution form of the command object , The default is text
cmd.Parameters.AddRange(paras);// Add parameters to the parameter set
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) // When executing a command , Associate Connection Associate when object is closed DataReader Object closed
{
dt.Load(sdr); // Fill the values in the provided data source into DataTable, If it already exists , Then merge
}
return dt; // Return table to DAL layer
}
/// <summary>
/// Execute without parameters SQL Query statements or stored procedures
/// </summary>
/// <param name="cmdText"> Inquire about SQL Statement or stored procedure </param>
/// <param name="ct"> Command type </param>
/// <returns> Returns the number of affected rows </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>
/// Execute database operations or stored procedures without parameters heihei
/// </summary>
/// <param name="cmdText"> Add, delete, change and check operation </param>
/// <param name="ct"> Command type </param>
/// <returns> Returns the number of affected rows </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>
/// Execute database operations or stored procedures with parameters
/// </summary>
/// <param name="cmdText"> Add, delete, change and check operation </param>
/// <param name="paras"> Parameters to query </param>
/// <param name="ct"> Command type </param>
/// <returns> Returns the number of affected rows </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;
}
}
Physical layer :
public class FaceImageEntity
{
private string userID;// User name
private string imageByte;// Picture bytecode
public byte[] ImageByte;
private string faceToken;// Unique identification of the picture
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 layer :
/// <summary>
/// Get the data by entering the page size and page number
/// </summary>
/// <param name="inputPageSidze"> Enter the size of each page </param>
/// <param name="inputpageCode"> The number of pages to query </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 [email protected]_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 layer :
/// <summary>
/// By entering the size of each page and getting the page number ,
/// </summary>
/// <param name="inputPageSidze"> The size of each page </param>
/// <param name="inputpageCode"> Look up the page </param>
/// <returns> Return the data </returns>
public List<FaceImageEntity> PagingSelectAllImage(int inputPageSidze, int inputpageCode)
{
return FaceImageDAL.PagingSelectAllImage(inputPageSidze,inputpageCode);
}
边栏推荐
- 利用超球嵌入来增强对抗训练
- install. IMG production method
- 力扣每日一题刷题总结:字符串篇(持续更新)
- Nacos service registration in the interface
- Find and rfind methods in string
- Opencv's experience of confusing X and Y coordinates
- Get the width and height of the screen in real time (adaptive)
- OpenCV3 6.2 低通滤波器的使用
- Use Matplotlib to draw a preliminary chart
- Global and Chinese market of snow sweepers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
Matlab数学建模工具
Static library and dynamic library
用MLP代替掉Self-Attention
Carsim-实时仿真的动画同步问题
C语言实现XML生成解析库(XML扩展)
Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?
Remplacer l'auto - attention par MLP
针对语义分割的真实世界的对抗样本攻击
乐理基础(简述)
随机推荐
I'll show you why you don't need to log in every time you use Taobao, jd.com, etc?
Carla-UE4Editor导入RoadRunner地图文件(保姆级教程)
力扣方法总结:双指针
Library function of C language
Eklavya -- infer the parameters of functions in binary files using neural network
On the back door of deep learning model
Using super ball embedding to enhance confrontation training
Meta Learning 简述
Daily practice (19): print binary tree from top to bottom
最长等比子序列
高中数学必修一
笔记本电脑卡顿问题原因
Global and Chinese market of snow sweepers 2022-2028: Research Report on technology, participants, trends, market size and share
install. IMG production method
学习写文章格式
用C# 语言实现MYSQL 真分页
Sparse matrix storage
STL速查手册
Array and string processing, common status codes, differences between PHP and JS (JS)
业务架构图