class=”markdown_views prism-atom-one-dark”>
Before using MFC to develop the result, the interface was too ugly and was passed, and asked to use C# to re-develop >_<, but finally got rid of the egg pain of VC6.0 and operated Y.
Let’s connect to the database first.
(1) Mysql-connector-net is needed to connect to the MYSQL database with c#. This component can be downloaded by searching online. To install, just press next and install according to the default path;
(2 ) After creating a Winfrom project, reference this component
Right-click reference in the solution explorer -> add reference -> browse, the browse path is the path where mysql-connector-net is installed, for example, my path is: C: \Program Files (x86)\MySQL\MySQL Connector Net 6.6.4\Assemblies\v2.0
Select MySql.Data.dll, and then confirm
(3) Add in the project:
using MySql.Data.MySqlClient;
using System.Data.SqlClient;
- 1
- 2
(4)
Here is a simple page layout, using a button, a listview control, and a dataGridView control, where the name of the listview control is listview1, and the name of the dataGridView control is dataGridView1
(4) Simple design for listView to suit data presentation:
Select GridLines in the properties to change to true
Create a new function as follows:
private void bindListCiew()
{
this.listView1.Columns.Add("student");
this.listView1.Columns.Add("ID");
this.listView1.View = System.Windows.Forms.View .Details;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
Add two columns of students, ID can be added according to the actual situation;
Pay attention to add this.listView1.View = System.Windows.Forms.View.Details;
Otherwise there will be no change
private void Form1_Load (object sender, EventArgs e)
{
bindListCiew();
}
- 1
- 2
- 3
- 4
(5) Add a function to the button:
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection myconn = null;
MySqlCommand mycom = null;
MySqlDataAdapter myrec = null;
myconn = new MySqlConnection("Host=localhost;Database=student;Username=lemon;Password=123" );
myconn.Open();
mycom = myconn.CreateCommand();
mycom.CommandText = "SELECT *FROM student1";
MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
DataSet ds = new DataSet();
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
string sql = string.Format("select * from student1 ");
mycom.CommandText = sql;
mycom.CommandType = CommandType.Text;
MySqlDataReader sdr = mycom.ExecuteReader();
int i = 0;
while (sdr.Read())
{
listView1.Items.Add(sdr[0].ToString());
listView1.Items[i].SubItems.Add(sdr[1].ToString());
i++;
}
myconn.Close();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
Where
myconn = new MySqlConnection("Host =localhost;Database=****;Username=***;Password=***");
myconn.Open();
- 1
- 2
For the database connection, enter Database, username, password
mycom = myconn.CreateCommand ();
mycom.CommandText = "SELECT *FROM student1";
MySqlDataAdapter adap = new MySqlDataAdapter(mycom);
DataSet ds = new DataSet();
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
- 1
- 2
- 3
- 4
- 5
- 6
Generate a command to query data and add it to the dataGridView. Here, simply add all the data to the control. The table printed by this control is not very good-looking. I think the listview is still good-looking
The rest of the code is to display the listview
where
listView1 .Items .Add(sdr [0].ToString()); listView1.Items[i].SubItems.Add(sdr[1].ToString());
i++;
- 1
- 2
This is the addition of row data;
The final result is:
In the student1 table, my data is:
Points to note: 1. Modify the version of the .net framework
Baidu experience: jingyan.baidu.com
Recently, I installed vs2013 on the machine to develop a personal blog site. After I finished it, I put it on the server. There were a series of problems. Later, I found out that it was a problem with the .net framework version. The project built with vs2013 is 4.5 by default. But my server is 2003 and does not support .net framework4.5. Finally, it was solved by changing the .net framework version of the project. I will write it out and share it with you today.
Baidu experience: jingyan.baidu.com
tool/material
-
vs2013
Baidu experience: jingyan.baidu.com
Original project modification
-
1
1. Open the project
FoundI used to build a project with vs, and opened it with vs. If it is a lower version, there may be problems, but today we mainly discuss how to modify the .net framework version, so it does not have much impact.
-
2
2. Find project properties
After opening the project, right-click on the project to be modified, remember that it is the project name, not the solution. After right-clicking, there will be a property below, select properties.
-
3
3. Modified version
After clicking Properties, a control panel will appear, find the application > target framework drop-down box, and select the .net framework version that suits you.
END
Baidu experience: jingyan.baidu.com
new item creation
-
1
1. New project
The same as above, open vs, click File>New>Project.
-
2
2. Select version
After clicking the new project, a control panel will appear. There is a drop-down box on the top of the panel, which contains different versions of .net framework. Just select the appropriate project and click OK.
-
3
3. View version
After the project is newly built, there will be a web.config in the root directory, click to open, and you can see your code, which version is marked.
END
Baidu experience: jingyan.baidu.com
Notes
-
There may be some problems in the modified version of the old project. It is recommended to create an empty project and copy the corresponding content into it.
-
If the server is 2003, it is recommended to use a version below 4.0, and 2008 can use a version above 4.0.
The content of experience is for reference only. If you need to solve specific problems (especially in the fields of law and medicine), it is recommended that you consult professionals in related fields in detail.
Report
Declare of the author: This experience is my original creation based on real experience, without permission, please do not reprint.
2. The number of database rows is out of bounds
��Experience: jingyan.baidu.com
Notes
-
There may be some problems in the modified version of the old project. It is recommended to create an empty project and copy the corresponding content into it.
-
If the server is 2003, it is recommended to use a version below 4.0, and 2008 can use a version above 4.0.
The content of experience is for reference only. If you need to solve specific problems (especially in the fields of law and medicine), it is recommended that you consult professionals in related fields in detail.
Report
Declare of the author: This experience is my original creation based on real experience, without permission, please do not reprint.
2. The number of database rows is out of bounds