当前位置:网站首页>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);
}
边栏推荐
- Go functions make, slice, append
- Vscode下中文乱码问题
- C language implements XML generation and parsing library (XML extension)
- Learn to write article format
- Backup, recovery and repair of XFS file system
- 常量指针和指针常量
- 【学习笔记】Matlab自编图像卷积函数
- Programmers can only be 35? The 74 year old programmer in the United States has been programming for 57 years and has not retired
- Hystrix dashboard cannot find hystrix Stream solution
- Array and string processing, common status codes, differences between PHP and JS (JS)
猜你喜欢
包图画法注意规范
Specification for package drawing
简易打包工具的安装与使用
Open3d learning notes 1 [first glimpse, file reading]
OpenCV3 6.2 低通滤波器的使用
浅谈深度学习中的对抗样本及其生成方法
Use of opencv3 6.2 low pass filter
It's great to save 10000 pictures of girls
Using super ball embedding to enhance confrontation training
Target detection for long tail distribution -- balanced group softmax
随机推荐
Go functions make, slice, append
Graph Pooling 简析
Fundamentals of music theory (brief introduction)
笔记本电脑卡顿问题原因
Matlab数学建模工具
CarSim problem failed to start solver: path_ ID_ OBJ(X) was set to Y; no corresponding value of XXXXX?
OpenCV常用方法出处链接(持续更新)
VS Code配置问题
稀疏矩阵存储
深入理解JVM
力扣方法总结:滑动窗口
On the confrontation samples and their generation methods in deep learning
It's great to save 10000 pictures of girls
Global and Chinese markets for Salmonella typhi nucleic acid detection kits 2022-2028: Research Report on technology, participants, trends, market size and share
E-R画图明确内容
Where do you find the materials for those articles that have read 10000?
Target detection for long tail distribution -- balanced group softmax
install. IMG production method
Look for we media materials from four aspects to ensure your creative inspiration
我的vim配置文件