[C # and MySQL] connect to the local MySQL using MySQL connect net in

Source: Internet
Author: User
Tags mysql connect

[Step description]

1. Create a VC project, install MySQL, and prepare the data

2. Install MySQL connect net

3. Reference MySQL database objects in the project

4.CodeWrite connection and query statements


【Reference MySQL database objects in the project]




【Write the connection and query statements in the code.]

 

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. MVC; using MySQL. data. mysqlclient; // MySQL must be applied to connect to and operate on MySQL. data. mysqlclientnamespace fir1_vc2prj. controllers {[handleerror] public class homecontroller: controller {public actionresult index () {string myinfo = "Welcome to ASP. net MVC! "; /// // Obtain the returned value of mysq //////////////// //// // mysqlconnection myconn = NULL; mysqlcommand mycom = NULL; mysqldatareader myrec = NULL; // concatenate String Assembly myconn = new mysqlconnection ("host = localhost; database = test; username = root; Password = fulei "); // connect to myconn. open (); If (myconn. state. tostring () = "open") {myinfo = myinfo + "<br> MySQL connection succeeded";} // query command value assignment, you can write multiple statements, separate multiple statements with a comma (;). mycom = new mysqlcommand ("select * From persioninfo", myconn); myrec = mycom. executereader (); // read again and again. If the read fails, the while (myrec. read () {myinfo = myinfo + myrec ["name"] + "" + myrec ["sex"] ;}//// close the related object myrec. close (); myconn. close (); /// // obtain the returned value of mysq ////////////// //// // viewdata ["message"] = myinfo; return view () ;}public actionresult about () {return view ();}}}

 

 

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.