How to connect to mysql in vs2015?
1. Create a new project named mysql, select c# for the programming environment, then select the windows form application, and create a new form to display the data set 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 to the database to obtain the data set. The name of the button is display, and the datagridview control is used to Display the contents of the data set
3. Click the reference folder in the solution explorer and right-click and select Add Reference. Select Browse and open mysql.data.dll. This It is a dynamic library for C# to connect to mysql database. It encapsulates many commonly used methods for operating databases
4. In the code of form1.cs in the solution explorer Add using MySql.Data.MySqlClient; this is the actual reference to the content of mysql.data.dll in the code. With this c#, you can easily operate the sql database
5. Add the following code in the click event of the button
string str = "Server=127.0.0.1;User ID=root;Password=123456;Database=test;CharSet=gbk;"; MySqlConnection con = new MySqlConnection(str);//实例化链接 con.Open();//开启连接 string strcmd = "select * from user"; MySqlCommand cmd = new MySqlCommand(strcmd, con); MySqlDataAdapter ada = new MySqlDataAdapter(cmd); DataSet ds = new DataSet(); ada.Fill(ds);//查询结果填充数据集 dataGridView1.DataSource = ds.Tables[0]; con.Close();//关闭连接
Copy after login
6. Use navicat software to create a new table user in the database test, and then create two new fields username and password (Field in the picture), navicat software is a graphical interface tool for mysql, responsible for database operations such as new tables and backups, and can be operated intuitively through the interface
7, After the database is built, you can execute the project. Click the Show button to execute the results as follows. The username and password appear, indicating that the database connection is successful. Since no data has been added, the following is empty
##Related learning recommendations:
mysql tutorial(video)
The above is the detailed content of How to connect to mysql in vs2015?. For more information, please follow other related articles on the PHP Chinese website!