C# learning summary (3) - code to connect mysql database (VS tool)

  • VS tools have ready-made methods for oracle and sqlserver databases, but mysql also needs to operate by itself

download mysql reference file

Download link: http://pan.baidu.com/s/1bpg0Tl1 Password: bfed


Add a mysql reference file to the project

Right-click on the project as shown in the figure below:
write picture description here
Click "Add Reference"
write picture description here
, click "Browse" in the dialog box to find the reference file, and click OK. After the addition is complete, open the "Reference" in the project, as shown in the figure below, the reference added in the
write picture description here
red box is


connect database code

code show as below:

...
using MySql.Data.MySqlClient
...
            string conn = "server=localhost;user id=root;password=123456;database=test";
            MySqlConnection mysql_conn = new MySqlConnection(conn);
            mysql_conn.Open();
            string sql = "insert into student values('11','22')";

            MySqlCommand mysql = new MySqlCommand(sql, mysql_conn);
            int num = (int)mysql.ExecuteNonQuery();

The specific functions do not need to be explained, the main attention:
* The two class names of MySqlCommand and MySqlConnection are enough.
* using System.Data.MysqlClient don't forget this sentence

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325987520&siteId=291194637