[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 ();}}}