vs2013 C # webapi MySQL novice, ask God to guide you

Source: Internet
Author: User
Tags connectionstrings

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

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.