vs2010 c# language to connect mysql database
1. Create a new project named mysql, select c# for the programming environment, then select the windows form application, and create a new form Used to display the data set queried to the sql database
##2. Drag a button and datagridview control from the toolbox to form1. The button triggers 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 content of the data set.
3. Click on the referenced folder in Solution Explorer and then Right-click and select Add Reference, select Browse and open mysql.data.dll. This is a dynamic library for C# to connect to the mysql database. It encapsulates many commonly used methods for operating databases
4. Add using MySql.Data.MySqlClient to the code of form1.cs in the solution explorer; 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
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();//关闭连接
6. Use navicat software to create a new database test Table user, and then create two new fields username and password (fields in the picture). Navicat software is a graphical interface tool for mysql. It is responsible for database operations such as new tables and backups, and can be operated intuitively through the interface
##
The above is the detailed content of How to share the process of connecting to MySQL database in vs2010. For more information, please follow other related articles on the PHP Chinese website!