Home  >  Article  >  Database  >  How to share the process of connecting to MySQL database in vs2010

How to share the process of connecting to MySQL database in vs2010

黄舟
黄舟Original
2017-08-04 13:22:576153browse

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

How to share the process of connecting to MySQL database in vs2010


##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.

How to share the process of connecting to MySQL database in vs2010


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

How to share the process of connecting to MySQL database in vs2010

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

How to share the process of connecting to MySQL database in vs2010


# 5. Add the following code to 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();//关闭连接

How to share the process of connecting to MySQL database in vs2010

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

How to share the process of connecting to MySQL database in vs2010


7. After the database is created, you can execute the project, click The execution result of the display button is as follows. The appearance of username and password indicates that the database connection is successful. Since no data has been added, the following is empty

How to share the process of connecting to MySQL database in vs2010

##

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn