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

最近喜欢喜欢上了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 test
public 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 framework
private 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

时间: 2024-08-29 09:13:52

vs2013 C# webapi Mysql新手,求各位大神指导的相关文章

菜鸟求zendframwork大神指导简单问题

xxx.phtml文件与aaaController.php文件中的xxxAction()方法貌似是绑定的,有没有这样的可能性,xxx.phtml中表单的action能指定aaaController.php中指定的函数执行呢,因为xxxAction()会随着xxx.phtml加载而执行,不能控制.求解救  返回IT界网站首页>> --------解决方案-------- 被你绕晕了..  aaaController xxxAction 什么的  aaa/xxx 默认会呈现  aaa文件夹下的 x

求各位大神指导两个小问题

3nbvne躺比泌钠傅良<http://weibo.com/UXacgp/230927983169302553174016> apg9zn吞胸掳八沽哨<http://weibo.com/p/230927983117254201712640?9Cn8_20180414> q90cnr殖亿爸雷噬澜<http://weibo.com/HyjqhXRp/230927983104181856378880> bfk1pp庞扒裳却纪缀<http://weibo.com/p201

新人迷茫,求大神指导

毕业快半年,工作还算稳定.但是作为新手有点迷茫,求各路大神指导. 1.工作上使用的是c#/net,web开发,想学点新东西但是因为工作上用不到怕学完就忘了,而且也怀疑学习这些新东西的价值,比如python.之前实习学过mybaties,因为换工作没用,已经忘得差不多了.才有了这种迷茫想法.也有不学东西就等着做码农的想法,怎么破. 2.程序猿有必要掌握多门语言吗?工作上大部分只会用到一种语言,多个技能多口饭,但是这个学习成本值得付出吗? 3.我不是计算机专业,对编程原理,算法,软件工程的概念都不清

hdu 1008为何不对?求大神指导!

/*#include<iostream> using namespace std; int main(){ int n; while (cin >> n){ if (n == 0) break; else if (n > 0 && n < 100){ int sum=0, max,min; int *f = new int[n]; for (int i = 0; i < n; i++) cin >> f[i]; max = min =

求一个大神运维大师 教我 我刚从事这个行业 我勤奋好学 求师傅

求一个大神运维大师 教我 我刚从事这个行业  我勤奋好学 求师傅 hello world  师傅 求指导  可以提要求  我喜欢linux 饥渴难耐了 感谢                    师傅

耐克的定制页用canvas如何实现....跪求前端大神指点。

选择鞋子的鞋底 鞋底会变色,也可以添加自己定制的id,这个东西看的是用canvas做的,但是小弟确实不知道怎么去做,求大神指点一二,不胜感激! nike的定制页地址:http://store.nike.com/cn/zh_cn/product/air-presto-id/?piid=41054&pbid=1045075390#?pbid=1045075390

求各路大神帮忙?????

这是action中的插入的方法 public void doInsertStudent() throws IOException{ this.ajaxRun(new AjaxRun() { @Override public void run(BaseResultVO ajaxRsp) throws Exception { StudentModel model = getModel(); Map<String, String> returnMap = new HashMap<String,

【真&#183;新手初篇】菜鸟们都戳进来看看(欢迎大神指导)

作为一只菜鸟,我希望这篇文能帮助到同是新人的你 ———————————————————————————————————————————————————— 首先我必须感谢@CoffeeDeveloper,他的文章给了我行动的勇气,读读他置顶的文章相信对你也会有帮助 如果你想学习HTML,我向你推荐一个网站(部分收费课程),里面有一整套学习教程(虽说那老师的口音...). 视频中提及的一款软件intellji IDEA ,如果有需要的戳这里 .那啥,找提取码就把鼠标移到链接上. 缺少学习和参考的材料

大神指导(想做产品)

本人是个.net的开发人员  ,参加工作快两年 ,我感觉自己不会在"写程序"的道路上走很久 所以我想早点转型 我看了产品经理的职位 很感兴趣 觉得很有挑战性 但是本人的经验几乎为0  但是却有一颗好学勤奋的心   希望大神吐槽我一下 给我一条明路  谢谢