Dapper连接与事务的简单封装
增删改查方面,已经有Dapper.Extension这么强大的工具了,我也实在没啥好写的,就随手写了个看起来比较优雅的连接与事务的封装。在之后使用Dapper.Extension类库时,完全可以照搬进去。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Dapper;
using DapperDemo.Models;
namespace DapperDemo
{
internal class Program
{
public static string ConnString = "Server=(localdb)\\MSSQLLocalDB;Database=DapperDB;";
private static void Main(string[] args)
{
BuckInsert();
BuckInsert();
BuckInsert();
DeleteAfterUpdating();
GetPersonList().ForEach(c=>Console.WriteLine(c.UserName));
}
public static List<Person> GetPersonList()
{
var people = new List<Person>();
ExecuteWithoutTransaction(conn =>
{
people = conn.Query<Person>("select * from Person where id>@id", new {id = 2}).ToList();
});
return people;
}
public static bool BuckInsert()
{
return ExecuteWithTransaction((conn, trans) =>
{
var r = conn.Execute(
@"insert Person(username, password,age,registerDate,address) values (@a, @b,@c,@d,@e)",
new[]
{
new {a = 1, b = 1, c = 1, d = DateTime.Now, e = 1},
new {a = 2, b = 2, c = 2, d = DateTime.Now, e = 2},
new {a = 3, b = 3, c = 3, d = DateTime.Now, e = 3}
},trans);
return r;
});
}
public static bool Update()
{
return ExecuteWithTransaction((conn, trans) =>
{
var r = conn.Execute(@"update Person set password='www.lanhuseo.com' where username=@username",
new {username = 2}, trans);
return r;
});
}
public static bool Delete()
{
return ExecuteWithTransaction((conn, trans) =>
{
var r = conn.Execute(@"delete from Person where id=@id", new {id = 1009}, trans);
return r;
});
}
public static bool DeleteAfterUpdating()
{
return ExecuteWithTransaction((conn, trans) =>
{
var r = conn.Execute(@"update Person set password='www.lanhuseo.com' where id=@id", new {id = 1009}, trans,
null, null);
r += conn.Execute("delete from Person where id=@id", new {id = 1010}, trans, null, null);
return r;
});
}
/// <summary>
/// Used for query
/// </summary>
/// <param name="action"></param>
public static void ExecuteWithoutTransaction(Action<SqlConnection> action)
{
UseConnectObj(action);
}
/// <summary>
/// Used for cud
/// </summary>
/// <returns>Execute Result</returns>
/// <param name="func"></param>
public static bool ExecuteWithTransaction(Func<SqlConnection, IDbTransaction, int> func)
{
var r = 0;
UseConnectObj(conn =>
{
IDbTransaction trans = conn.BeginTransaction();
r = func(conn, trans);
trans.Commit();
});
return r > 0;
}
/// <summary>
/// Use Action Connection
/// </summary>
/// <param name="action"></param>
public static void UseConnectObj(Action<SqlConnection> action)
{
using (var conn = new SqlConnection(ConnString))
{
conn.Open();
action(conn);
}
}
}
}
Dapper连接与事务的简单封装的更多相关文章
- .net core 中简单封装Dapper.Extensions 并使用sqlsuger自动生成实体类
引言 由公司需要使用dapper 同时支持多数据库 又需要支持实体类 又需要支持sql 还需要支持事务 所以采用了 dapper + dapperExtensions 并配套 生成实体类小工具的方 ...
- Redisclient连接方式Hiredis简单封装使用,连接池、屏蔽连接细节
工作须要对Hiredis进行了简单封装,实现功能: 1.API进行统一,对外仅仅提供一个接口. 2.屏蔽上层应用对连接的细节处理: 3.底层採用队列的方式保持连接池,保存连接会话. 4.重连时採用时间 ...
- Dapper连接Oracle
Dapper连接Oracle去年写过了篇博客,名字叫:让dapper支持Oracle 网址:http://www.cnblogs.com/ushou/archive/2012/09/28/270690 ...
- MongoDB Python官方驱动 PyMongo 的简单封装
最近,需要使用 Python 对 MongodB 做一些简单的操作,不想使用各种繁重的框架.出于可重用性的考虑,想对 MongoDB Python 官方驱动 PyMongo 做下简单封装,百度一如既往 ...
- Abp公共连接和事务管理方法
Conection 和事务管理在使用数据库的应用中是一个最重要的概念.当你打开一个连接,开始一个事务,如何来处理这些连接等等. 您也许知道,.NET使用了连接池.所以,创建一个连接实际上是从连接池里得 ...
- 对pymysql的简单封装
#coding=utf-8 #!/usr/bin/python import pymysql class MYSQL: """ 对pymysql的简单封装 "& ...
- ADO简单封装(MFC)
简单封装了一下,不是很严谨. /************************************************************************/ /* INSTRUC ...
- MySQL的C++简单封装
/* *介绍:MySQL的简单封装,支持流操作输入输出MySQL语句,然而并没有什么软用,大二学生自娱自乐,有不足求指点 *作者:MrEO *日期:2016.3.26 */ 头文件 my_sql.h ...
- redis数据库操作的C++简单封装
用c++简单封装了redis的基本操作(hiredis) 接口包括:①链接和断开连接.②设置键值对(set).③查询键值对(get).④删除键值对(del).⑤将所有键显示出来 若任何一处发生错误,返 ...
随机推荐
- hive优化之——控制hive任务中的map数和reduce数
一. 控制hive任务中的map数: 1. 通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文 ...
- 使用net.sf.cssbox实现网页截图
需要引用包,在pom.xml中添加引用: <dependency> <groupId>net.sf.cssbox</groupId> <artifactId& ...
- css常见属性
css常见属性 1.颜色属性 1.1 color属性定义文本的颜色 1.2 color:green 1.3 color:#ff6600 可简写为#f60 1.4 color:rgb(255,255,2 ...
- Spring(4)——面向切面编程(AOP模块)
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
- java学习历程,一年三年五年计划
学习这一部分其实也算是今天的重点,这一部分用来回答很多群里的朋友所问过的问题,那就是你是如何学习Java的,能不能给点建议?今天我是打算来点干货,因此咱们就不说一些学习方法和技巧了,直接来谈每个阶段要 ...
- phantomjs 开发爬虫框架
函数 page.childframescount page.childframesname page.close page.currentframename page.deletelater page ...
- [LeetCode] Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- [LeetCode] Construct the Rectangle 构建矩形
For a web developer, it is very important to know how to design a web page's size. So, given a speci ...
- BOM,Dom 回顾
加给元素: offsetLeft(距离定位父级的距离)/offsetTop(距离定位父级的距离)/offsetWidth(可视宽度)/offHeight(可视高度) clientLeft(左边框宽度) ...
- 关于自定义view--实现自定义水波纹效果
开发中的东西太多,怕自己忘记了,简单记录一下. 声明:此控件借鉴了大佬的想法,在此感谢大佬提供的支持,我只是把大佬的想法拿出来而已. ok,废话到此结束,看效果: 分析一下,我们可以看到,图中有两个圆 ...