usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;namespacee-commerce { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) { } Private voidButton1_Click (Objectsender, EventArgs e) { stringstr ="Host=localhost; User Id=root; password=; Port = 3306;database=animals; Charset=utf8;"; Mysqlconnection Conn=Newmysqlconnection (str); Conn. Open ();// ////Get the database and open the connection //String sql = "Select *from student"; //mysqlcommand cmd = new Mysqlcommand (SQL, conn); //Mysqldataadapter MDA = new Mysqldataadapter (cmd); //DataSet ds = new DataSet (); //MDA. Fill (DS); //Datagridview1.datasource = ds. Tables[0]; ////mysqldatareader dr = cmd. ExecuteReader (); //Conn. Close (); /*1. Five steps to access the database 2. Get the SQL statement that you want to send 3. Execute SQL statement 4. Get the execution after the meet 5. Close connection Remember to close the connection */ ////1 Get the connection to the database and open the connection //string url = "User id=root; password=; Host=localhost; port=3306;database=xx;charset=xx; ";//mysqlconnection con = new mysqlconnection (URL); //con. Open (); ////2. To get the SQL statement to send //String sql = "SELECT ID from student where name= ' Kobe '"; //mysqlcommand com = new Mysqlcommand (sql, con); ////3 Execute SQL statement //Mysqldatareader reader = com. ExecuteReader (); //Reader. Read ();//Read () Each call returns a row of data from the result set//Reader. GetInt32 (0); /*1. Five steps to access the database 2. Get the SQL statement that you want to send 3. Execute SQL statement 4. Get the execution after the meet 5. Close connection Remember to close the connection */ //1 Get the connection to the database and open the connection stringURL ="User Id=root; password=; Host=localhost; port=3306;database=students;"; Mysqlconnection Con=Newmysqlconnection (URL); Con. Open (); //2. To get the SQL statement to send stringsql ="Select *from student where name= ' Kobe '"; Mysqlcommand com=Newmysqlcommand (sql, con); //3 Execute SQL statementMysqldatareader reader =com. ExecuteReader (); Reader. Read ();//Read () Each call returns a row of data from the result set//Reader. GetInt32 (0);Console.WriteLine (reader. GetInt32 (0)); Con. Close (); ////database to be increased and censored////1 Get the connection to the database and open the connection //string url = "User id=root; Password=root; Host=localhost; port=3306;database=students; ";//mysqlconnection con = new mysqlconnection (URL); //con. Open (); //String sql = "INSERT into student values (one, ' hahahaha ', ' Man ', 6)";//Add Information//mysqlcommand cmd = new Mysqlcommand (sql, con); //int result = cmd. ExecuteNonQuery (); //Console.WriteLine ("Database open result:" +result); //con. Close (); //findstudent ("Kobe"); } Public Static BOOLFindstudent (stringname) { BOOLb =false; stringURL ="User Id=root; Password=root; Host=localhost; port=3306;database=students;"; Mysqlconnection Con=Newmysqlconnection (URL); Con. Open (); stringsql ="Select *from studnet where Name= ' {0} '"; SQL=string. Format (sql, name); //Console.WriteLine (SQL);Mysqlcommand cmd =Newmysqlcommand (sql, con); Mysqldatareader Reader=cmd. ExecuteReader (); b=Reader. Read (); returnb; } }}
C # reads data from database MySQL