C# Connect MYSQL database and query

Keywords: MySQL Database Windows SQL

ABSTRACT: The result interface developed with MFC is too ugly to be seen by pass, requiring redevelopment with C >._

Previously, using MFC to develop the result interface was too ugly to be pass, requiring C# to redevelop > <, but finally got rid of the pain of VC6.0 and operated Y.  
First connect to the database.
(1) mysql-connector-net is needed to connect MYSQL database with c#. This component can be downloaded by searching on the internet. Installation is directly next and installed by default path.
(2) After building a new Winfrom project, refer to this component.
In Solution Explorer, right-click Reference - > Add Reference - > Browse to install mysql-connector-net, such as my path: C: Program Files (x86) MySQL MySQL Connector Net 6.6.4 Assemblies v2.0
Select MySql.Data.dll and confirm.

(3) Add:

using MySql.Data.MySqlClient;
using System.Data.SqlClient;

(4) 
Here is a simple page layout, using a button, a listview control, a dataGridView control, where the name of the listview control is listview1, and the name of the dataGridView control is dataGridView1.

(4) Simple design of listView for data presentation:
Select GridLines in the property and change it 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;

        }

Add two columns of students, ID can be added according to the actual situation;
Note that add this.listView1.View = System.Windows.Forms.View.Details;
Otherwise, there will be no change.
again

 private void Form1_Load(object sender, EventArgs e)
 {
     bindListCiew();
  }

(5) Add functions for buttons:

 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();
        }

among

  myconn = new MySqlConnection("Host =localhost;Database=****;Username=***;Password=***");
            myconn.Open();

For 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;

Generate a command query data and add it to the dataGridView. Simply add all the data to the control. The form printed by the control is not very good, but it's still a listview.

The rest of the code is to display the listview.
among

listView1.Items.Add(sdr[0].ToString());              listView1.Items[i].SubItems.Add(sdr[1].ToString());
               i++;

This is the addition of row data;
The final results are as follows:

In the student1 table, my data are:

It is better than vc6.0.

We will bear all the crimes and punishments in the world together.

Posted by kit on Sat, 15 Jun 2019 14:29:46 -0700