ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)
2016-12-02 11:33
1266 查看
操作 MySql 数据库使用
project.json 代码:
测试数据库脚本:
Product 代码:
ProductRepository 代码(数据访问操作):
调用代码:
参考资料:
Use Dapper ORM With ASP.NET Core
.NET Core 使用 Dapper 操作 MySQL
MySql.Data程序包(MySql 开发,其他第三方可能会有些问题)。
project.json 代码:
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" }, "Dapper": "1.50.2", "MySql.Data": "7.0.6-IR31" }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
测试数据库脚本:
CREATE TABLE `products` ( `ProductID` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(255) DEFAULT NULL, `Quantity` int(11) DEFAULT NULL, `Price` int(11) DEFAULT NULL, PRIMARY KEY (`ProductID`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=gbk;
Product 代码:
public class Product { [Key] public int ProductId { get; set; } public string Name { get; set; } public int Quantity { get; set; } public double Price { get; set; } }
ProductRepository 代码(数据访问操作):
public class ProductRepository { private string connectionString; public ProductRepository() { connectionString = @"server=localhost;database=dapperdemo;uid=root;pwd=123456;"; } public IDbConnection Connection { get { return new MySqlConnection(connectionString); } } public void Add(Product prod) { using (IDbConnection dbConnection = Connection) { string sQuery = "INSERT INTO Products (Name, Quantity, Price)" + " VALUES(@Name, @Quantity, @Price)"; dbConnection.Open(); dbConnection.Execute(sQuery, prod); } } public IEnumerable<Product> GetAll() { using (IDbConnection dbConnection = Connection) { dbConnection.Open(); return dbConnection.Query<Product>("SELECT * FROM Products"); } } public Product GetByID(int id) { using (IDbConnection dbConnection = Connection) { string sQuery = "SELECT * FROM Products" + " WHERE ProductId = @Id"; dbConnection.Open(); return dbConnection.Query<Product>(sQuery, new { Id = id }).FirstOrDefault(); } } public void Delete(int id) { using (IDbConnection dbConnection = Connection) { string sQuery = "DELETE FROM Products" + " WHERE ProductId = @Id"; dbConnection.Open(); dbConnection.Execute(sQuery, new { Id = id }); } } public void Update(Product prod) { using (IDbConnection dbConnection = Connection) { string sQuery = "UPDATE Products SET Name = @Name," + " Quantity = @Quantity, Price= @Price" + " WHERE ProductId = @ProductId"; dbConnection.Open(); dbConnection.Execute(sQuery, prod); } } public void TransactionTest() { using (IDbConnection dbConnection = Connection) { string sQuery = "UPDATE Products SET Name = 'xishuai222'" + " WHERE ProductId = 1"; dbConnection.Open(); using (var transaction = dbConnection.BeginTransaction()) { dbConnection.Execute(sQuery); ///to do throw exception transaction.Commit(); } } } }
调用代码:
public class Program { public static void Main(string[] args) { var productRepository = new ProductRepository(); var product = new Product() { Name = "xishuai" }; productRepository.Add(product); var products = productRepository.GetAll(); foreach (var item in products) { Console.WriteLine($"id: {item.ProductId}; name: {item.Name}"); } productRepository.TransactionTest(); Console.ReadKey(); } }
参考资料:
Use Dapper ORM With ASP.NET Core
.NET Core 使用 Dapper 操作 MySQL
相关文章推荐
- asp.net core使用mysql.data和dapper操作mysql数据库
- ASP.NET Core:使用EntityFrameworkCore操作MySql来丰富仓储模块
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- ASP.NET Boilerplate 学习 AspNet Core2 浏览器缓存使用 c#基础,单线程,跨线程访问和线程带参数 wpf 禁用启用webbroswer右键菜单 EF Core 2.0使用MsSql/MySql实现DB First和Code First ASP.NET Core部署到Windows IIS QRCode.js:使用 JavaScript 生成
- docker4dotnet #3 在macOS上使用Visual Studio Code和Docker开发asp.net core和mysql应用
- Asp.Net Core使用MySql.Data连接MySql示例
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 使用VS Code开发调试ASP.NET Core 1.0
- 【ABP开发】:asp.net core 中使用mysql
- Asp.net Core 1.1 升级后操作mysql出错的解决办法
- ASP .Net Core 使用 Dapper 轻型ORM框架
- asp.net 搭配 mysql 运行 的时候 @ 和 ? 的问题,Connector Net 最新版本,1.0版本和6.5.4版本的区别,为什么发布的时候没有包含MySql.Data.dll?
- ASP.NET Core 1.0 中使用Log日志
- Asp.Net Core 项目搭建 基础配置 和MySql 的使用
- ASP .Net Core 使用 Dapper 轻型ORM框架
- ASP.NET Core:使用Dapper和SwaggerUI来丰富你的系统框架
- ASP.NET Core:使用Dapper和SwaggerUI来丰富你的系统框架
- ASP.net(c#)+MySql 事务的使用方法