1. Create a new project named MySQL, programming environment Select C #, and then select Windows Forms Application, create a new form to display the datasets queried to the SQL database
2. Drag a button and DataGridView control from the toolbox to the Form1 form, the button is to trigger the connection database to get the dataset, the name of the button is displayed, The DataGridView control is used to display the contents of a DataSet 3. Click the References folder in Solution Explorer and then right-select Add Reference and select Browse to open Mysql.data.dll, which is a dynamic library of C # connection to the MySQL database. It encapsulates a lot of common operations database methods 4. Add using MySql.Data.MySqlClient in the Form1.cs code in Solution Explorer; This is what the code actually references in Mysql.data.dll, with this C # It is easy to operate SQL database 5. Add the following code to the button's Click event
String str = "server=127.0.0.1; User Id=root; Password=123456;database=test; CHARSET=GBK; ";
Mysqlconnection con = new mysqlconnection (str);//instantiation of links
Con. Open ();//Open connection
String strcmd = "SELECT * from user";
Mysqlcommand cmd = new Mysqlcommand (Strcmd, con);
Mysqldataadapter ada = new Mysqldataadapter (cmd);
DataSet ds = new DataSet ();
Ada. Fill (DS);//Query results populate data set
Datagridview1.datasource = ds. Tables[0];
Con. Close ();//Closed connection
6. Use the Navicat software to create a new table user in the database test, and then create a new two fields username and password (columns in the diagram), Navicat software is a GUI tool for MySQL, responsible for creating new tables and database operations such as backups. Intuitive interface to operate 7. After the database has been built, you can execute the project, click the Show button to execute the results as follows, the username and password indicate that the database connection was successful, because there is no data added so the following is empty
VS2015 How to connect MySQL database graphics