当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

Simply test the two different data transmission methods of content length and chunked

w10升级至W11系统,黑屏但鼠标与桌面快捷方式能用,如何解决

CVPR19-Deep Stacked Hierarchical Multi-patch Network for Image Deblurring论文复现

MySQL优化

In the era of short video, how to ensure that works are more popular?

OpenCV3 6.2 低通滤波器的使用

Principes fondamentaux de la théorie musicale (brève introduction)

Programmers can only be 35? The 74 year old programmer in the United States has been programming for 57 years and has not retired

Carsim-问题Failed to start Solver: PATH_ID_OBJ(X) was set to Y; no corresponding value of XXXXX?

浅谈深度学习中的对抗样本及其生成方法
随机推荐
笔记本电脑卡顿问题原因
Static library and dynamic library
Real world anti sample attack against semantic segmentation
Global and Chinese market of tillage finishing machines 2022-2028: Research Report on technology, participants, trends, market size and share
AR system summary harvest
【MnasNet】《MnasNet:Platform-Aware Neural Architecture Search for Mobile》
针对tqdm和print的顺序问题
My VIM profile
Business architecture diagram
用MLP代替掉Self-Attention
力扣方法总结:查找类
Multi site high availability deployment
Organigramme des activités
Brief introduction of prompt paradigm
Meta learning Brief
Force buckle method summary: sliding window
Global and Chinese markets for magnetic resonance imaging (MRI) transmission 2022-2028: Research Report on technology, participants, trends, market size and share
使用C#语言来进行json串的接收
力扣每日一题刷题总结:二叉树篇(持续更新)
Daily practice (19): print binary tree from top to bottom