博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET Core 1.0 使用 Dapper 操作 MySql(包含事务)
阅读量:7253 次
发布时间:2019-06-29

本文共 3790 字,大约阅读时间需要 12 分钟。

操作 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
GetAll() { using (IDbConnection dbConnection = Connection) { dbConnection.Open(); return dbConnection.Query
("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
(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();    }}

参考资料:

转载地址:http://ymzdm.baihongyu.com/

你可能感兴趣的文章
WEB项目 后台接收前端数组
查看>>
信号量与条件变量的区别
查看>>
关于plsql连接oracle数据库session失效时间设置
查看>>
三阶魔方花样玩法,公式汇总
查看>>
Python os
查看>>
Ubuntu使用ssh公钥实现免密码登录
查看>>
记一次720度托马斯回旋过狗!
查看>>
Atitit 图像处理的心得与疑惑 attilax总结
查看>>
mysql 关于日期时间的字段类型
查看>>
基于libvlc和wxWidgets的简单播放器代码阅读
查看>>
去除字符串中的html标记及标记中的内容
查看>>
windows下如何安装和启动MySQL
查看>>
Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0
查看>>
mysql操作命令梳理(3)-pager
查看>>
opencv3——ANN算法的使用
查看>>
[OSG]OSG的相关扩展
查看>>
MVC ---- EF高级增删改
查看>>
使用wireshark分析tcp/ip报文之报文头
查看>>
magent实现memcached集群的一个问题
查看>>
php 下 html5 XHR2 + FormData + File API 上传文件
查看>>