Recently like to like on the Webapi template. Ajax request data, WEBAP output json,angular data-bound DOM, simple and straightforward, is simply a small front-end of the gospel. Can not help, installed VS, connected to the database, the successful output of data.
Visual studio2013 New Webapi
Solution, right-manage nuget packages, Mysql.data MySql.Data.Entity
Then the configuration file (Web. config) adds a link string.
<connectionStrings><!--<add name= "defaultconnection" providername= "System.Data.SqlClient" connectionstring= "Data source= (LocalDb) \ V11.0;initial catalog=aspnet-mvcapplication1-20150910132908;integrated Security=sspi; attachdbfilename=| Datadirectory|\aspnet-mvcapplication1-20150910132908.mdf "/> -<Addname= "Mysqlconnstring"connectionString= "Server=localhost; port=3306;database=products; Uid=root; Pwd= "ProviderName= "MySql.Data.MySqlClient" /></connectionStrings>
Add MySQL helper class, Mysqlhelper
Name and System.Configuration.ConfigurationManager.AppSettings are the same as below
/this connectionString forThe local test Public Static ReadOnly stringConnectionstringmanager = system.configuration.configurationmanager.appsettings["mysqlconnstring"];//configurationmanager.connectionstrings["Mysqlconnstring"]. ConnectionString;//Hashtable to store the parameter information, the hash table can store any type of argument//Here the Hashtable are static types of static variables, since it is static and that's a definition of global use.//All parameters is using this hash table, what to ensure that others in the change does not affect their time to read I T//before, the method can use the-lock method to lock the table, does isn't allow others to modify.when it had readed then Unlocked table.//Now . NET provides a HashTable's Synchronized methods to achieve the same function, no need to manually lock, completed Directly by the system frameworkPrivate StaticHashtable Parmcache = hashtable.synchronized (NewHashtable ());
You can already use MySQL
var strconn = system.web.configuration.webconfigurationmanager.connectionstrings[" Mysqlconnstring"new Mysqlcommand (); using New mysqlconnection (strconn)) {Conn. Open ();}
Thanks to the ocean for teaching me.
Execute SELECT statement
varstrconn = system.web.configuration.webconfigurationmanager.connectionstrings["mysqlconnstring"]. ConnectionString; Mysqlcommand cmd=NewMysqlcommand ();using(Mysqlconnection conn =Newmysqlconnection (strconn)) {Conn. Open (); Mysqlcommand myCMD=NewMysqlcommand ("INSERT INTO product (Name,price) VALUES (' Xiao Wang ', ' one ')", conn); Mysqlcommand objcmd=NewMysqlcommand ("SELECT * from ' product '", conn); Mysqldatareader R=Objcmd.executereader ();inti =0; while(R.read ()) {Try{products[i]. Id= R.getint32 (0);p Roducts[i]. Name= R.getstring (2);p Roducts[i]. Price= R.getint32 (1); I++;}Catch{}}conn. Close ();}
Release
New configuration file
Ok
Quote Please specify http://www.cumt.top/blog/?p=107
vs2013 C # webapi MySQL novice, ask God to guide you