C # reads data from database MySQL

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.