1. To connect to the MySQL database, you must first download the MySQL connection. net file,
File is http://download.csdn.net/detail/xiaoliu123586/9145579
2, unzip the Mysql-connector-net-6.6.6-noinstall.zip file just downloaded, there are several versions of the choice, here I choose V4,
Select a few files from all the files in the V4.0 and add them to the reference of the C # project, and then you can write the program to do the database.
3. Database Operation code InsertMySql.cs
1 usingSystem;2 usingSystem.Collections;3 usingSystem.Configuration;4 usingMysql.data;5 usingMySql.Data.MySqlClient;6 usingSystem.Data;7 8 namespacetest19 {Ten class Program One { A Public Static voidMain (string[] args) - { - stringConstr="Server=localhost; User Id=root;password=admin;database=haha"; theMysqlconnection mycon=Newmysqlconnection (constr); - Mycon. Open (); -Mysqlcommand myCMD =NewMysqlcommand ("INSERT INTO User (Id,name) VALUES (' 3 ', ' Ooooss ')", mycon); - if(myCMD. ExecuteNonQuery () >0) + { -Console.WriteLine ("Data inserted successfully! "); + } A console.readline (); at Mycon. Close (); - } - } -}
C # How to connect MySQL database