vs2013 C# webapi Mysql新手,求各位大神指導

來源:互聯網
上載者:User

標籤:

最近喜歡喜歡上了webapi模板。ajax請求資料,webap輸出json,angular將資料繫結DOM,簡單明了,簡直是小前端的福音。實在忍不住,裝上vs,連上資料庫,成功輸出資料。

visual studio2013 建立webapi

解決方案,右鍵管理NuGet程式包,MySql.Data  MySql.Data.Entity

然後設定檔(web.config)添加連結字串。

<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" />--><add name="MySQLConnString" connectionString="Server=localhost;Port=3306;Database=products;Uid=root;Pwd=" providerName="MySql.Data.MySqlClient" /></connectionStrings>

 

  

添加MySql輔助類,mysqlHelper

name和下文的System.Configuration.ConfigurationManager.AppSettings相同

/This connectionString for the local testpublic static readonly string connectionStringManager = 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 is static types of static variables, since it is static, that is a definition of global use.//All parameters are using this hash table, how to ensure that others in the change does not affect their time to read it//Before ,the method can use the lock method to lock the table, does not allow others to modify.when it has 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 static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

 

已經可以使用mysql

var strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;MySqlCommand cmd = new MySqlCommand();using (MySqlConnection conn = new MySqlConnection(strConn)){conn.Open();}

感謝海洋教我。

執行select語句

var strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;MySqlCommand cmd = new MySqlCommand();using (MySqlConnection conn = new MySqlConnection(strConn)){conn.Open();MySqlCommand mycmd = new MySqlCommand("insert into product(name,price) values(‘小王‘,‘11‘)", conn);MySqlCommand objCmd = new MySqlCommand("select * from `product` ", conn);MySqlDataReader r = objCmd.ExecuteReader();int i = 0;while (r.Read()){try{products[i].Id = r.GetInt32(0);products[i].Name = r.GetString(2);products[i].Price = r.GetInt32(1);i++;}catch{}}conn.Close();}

發布

建立設定檔

ok

引用請註明http://www.cumt.top/blog/?p=107

vs2013 C# webapi Mysql新手,求各位大神指導

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.