foreword
Recently, I am developing a software, C# connects to the MySQL database, uses the dataGridView control in the software, and then adds a page under the dataGridView control. At the beginning, I found some C# pagination controls, and tried to introduce the project, but there were more or less regrets. So I tried to write this pagination myself.
I stepped on a lot of pitfalls in the process, and now I will write it out and break it down.
1. Software interface
Pagination consists of two Buttons, a TextBox and a Label.
2. Core code
1. Global variables and load functions
int pagenum;//页码
int zts;//总条数
public void pageLode()
{
pagenum = 1;
txt页码.Text = pagenum.ToString();
selectData();
lab总页.Text = (zts / 15 + 1).ToString();
btn上一页.Enabled = false;
btn下一页.Enabled = true;
}
The pageLode() function needs to be placed in the form Load() method.
private void Manager_Load(object sender, EventArgs e)
{
pageLode();
}
2. Calculate the page number
public void selectData()
{
string conn = myDatabase.Common;
MySqlConnection mycon = new MySqlConnection(conn);
try
{
if (mycon.State == System.Data.ConnectionState.Closed)
{
mycon.Open();
}
string sql_count = "SELECT COUNT(uid) as tally FROM code_custom";
MySqlCommand cmd_count = new MySqlCommand(sql_count, mycon);
MySqlDataReader sread = cmd_count.ExecuteReader();
sread.Read();
zts = Convert.ToInt32(sread["tally"]);
}
catch (MySqlException ex)
{
MessageBox.Show("不能连接服务器,本地验证失败!" + ex.Message);
}
finally
{
mycon.Close();
}
}
The data requested by the url network used here.
3. Previous page
private void Btn上一页_Click(object sender, EventArgs e)
{
btn下一页.Enabled = true;
pagenum = pagenum - 1;
txt页码.Text = pagenum.ToString();
if (pagenum <= 1)
{
txt页码.Text = "1";
btn上一页.Enabled = false;
btn重新载入.Focus();
}
selectPage(pagenum);
}
4. Next page
private void Btn下一页_Click(object sender, EventArgs e)
{
btn上一页.Enabled = true;
pagenum = pagenum + 1;
txt页码.Text = pagenum.ToString();
if (pagenum == Convert.ToInt32(lab总页.Text))
{
txt页码.Text = lab总页.Text;
btn下一页.Enabled = false;
btn重新载入.Focus();
}
selectPage(pagenum);
}
5. Read data in pages
public void selectPage(int p)
{
string fenye = (15 * (p - 1)).ToString();
string conn = myDatabase.Common;
MySqlConnection mycon = new MySqlConnection(conn);
try
{
if (mycon.State == System.Data.ConnectionState.Closed)
{
mycon.Open();
}
dataGridView1.Rows.Clear();
string sql_uid = "SELECT * FROM code_custom WHERE uid NOT IN (SELECT uid FROM (SELECT uid FROM code_custom ORDER BY uid desc LIMIT " + fenye + " ) as cd ) ORDER BY uid desc LIMIT 15";
MySqlCommand cmd_uid = new MySqlCommand(sql_uid, mycon);
MySqlDataReader reader = cmd_uid.ExecuteReader();
while (reader.Read())
{
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[1].Value = reader.GetString("uid");
this.dataGridView1.Rows[index].Cells[2].Value = reader.GetString("ucode");
···
···
···
···
}
}
catch (MySqlException ex)
{
MessageBox.Show("不能连接服务器,本地验证失败!" + ex.Message);
}
finally
{
mycon.Close();
}
}
part of stepping on the pit
The most time-consuming part of this part of the code is writing SQL statements. I used to refer to the SQL statement written by MSSQL, but later found that MySQL is different from MSSQL in many places.
For example, there is such a syntax in MSSQL:
SELECT TOP 15 * FROM table_name;
But there is no such syntax as "TOP 15" in MySQL, so it needs to be written like this in MySQL:
SELECT * FROM table_name LIMIT 15;
Secondly, in MySQL syntax, LIMIT is not allowed in subqueries , such as:
SELECT * FROM table_name WHERE uid NOT IN
(SELECT uid FROM code_custom ORDER BY uid desc LIMIT 30) ORDER BY uid desc LIMIT 15;
Such a statement is wrong in MySQL, so such a prompt will appear.
After optimization, you need to add an alias to the subquery. So the correct code is as follows:
SELECT * FROM code_custom WHERE uid NOT IN
(SELECT uid FROM
(SELECT uid FROM code_custom ORDER BY uid desc LIMIT " + fenye + " ) as cd )
ORDER BY uid desc LIMIT 15"
To sum up, I am still not very proficient in mastering SQL statements.