当前位置:网站首页>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);
}
边栏推荐
- C语言实现XML生成解析库(XML扩展)
- Opencv3 6.3 reduced pixel sampling with filters
- Global and Chinese markets for conventional rubber track 2022-2028: Research Report on technology, participants, trends, market size and share
- 常量指针和指针常量
- SQLyog远程连接centos7系统下的MySQL数据库
- Animation synchronization of CarSim real-time simulation
- Use Matplotlib to draw a preliminary chart
- E-R画图明确内容
- OpenCV3 6.2 低通滤波器的使用
- 【学习笔记】反向误差传播之数值微分
猜你喜欢
浅谈深度学习模型中的后门
Replace convolution with full connection layer -- repmlp
Using transformer for object detection and semantic segmentation
C语言实现XML生成解析库(XML扩展)
CarSim problem failed to start solver: path_ ID_ OBJ(X) was set to Y; no corresponding value of XXXXX?
MySQL optimization
It's great to save 10000 pictures of girls
Use Matplotlib to draw a preliminary chart
服务器的内网可以访问,外网却不能访问的问题
利用Transformer来进行目标检测和语义分割
随机推荐
C语言实现XML生成解析库(XML扩展)
Carsim-实时仿真的动画同步问题
用MLP代替掉Self-Attention
力扣方法总结:双指针
Matlab数学建模工具
Where do you find the materials for those articles that have read 10000?
Specification for package drawing
Opencv3 6.3 reduced pixel sampling with filters
EKLAVYA -- 利用神经网络推断二进制文件中函数的参数
Daily practice (19): print binary tree from top to bottom
C language implements XML generation and parsing library (XML extension)
E-R画图明确内容
Go functions make, slice, append
OpenCV3 6.3 用滤波器进行缩减像素采样
用MLP代替掉Self-Attention
Sequence problem for tqdm and print
静态库和动态库
业务架构图
C语言的库函数
11月24号,我们为“满月”庆祝