vs web api mysql_vs2013 C# webapi Mysql新手,求各位大神指导

最近喜欢喜欢上了webapi模板。ajax请求数据,webap输出json,angular将数据绑定DOM,简单明了,简直是小前端的福音。实在忍不住,装上vs,连上数据库,成功输出数据。

visual studio2013 新建webapi

解决方案,右键管理NuGet程序包,MySql.Data  MySql.Data.Entity

然后配置文件(web.config)添加链接字符串。

添加MySql辅助类,mysqlHelper

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

/This connectionString forthe 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 framework

private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

已经可以使用mysql

var strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;

MySqlCommand cmd= newMySqlCommand();using (MySqlConnection conn = newMySqlConnection(strConn))

{

conn.Open();

}

感谢海洋教我。

执行select语句

var strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;

MySqlCommand cmd= newMySqlCommand();using (MySqlConnection conn = newMySqlConnection(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值