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).⑤将所有键显示出来 若任何一处发生错误,返 ...
随机推荐
- Java-NIO(九):管道 (Pipe)
Java NIO 管道是2个线程之间的单向数据连接.Pipe有一个source通道和一个sink通道.数据会被写到sink通道,从source通道读取. 代码使用示例: @Test public vo ...
- 解决-Django使用filter过滤时间,无法获取月份的问题
django中的filter日期查询属性有:year.month.day.week_day.hour.minute.second 但是但我在使用过滤查询是却总是无法过滤出月份,各种查资料,最后才发现是 ...
- [LeetCode] Max Chunks To Make Sorted II 可排序的最大块数之二
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- [LeetCode] Asteroid Collision 行星碰撞
We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the ...
- C#利用微软企业库Enterprise Library配置mysql数据库
在C#项目中,很多时候到要用到Enterprise Library.这里只是用一个很简单的小例子来演示一下Enterprise Library在VS2010中操作MySQL数据库的流程. 1,利用En ...
- jQuery滚动指定位置
$(document).ready(function() { $("#scroll").click(function() { $('html, body').animate({ s ...
- [SDOI 2012]Longge的问题
Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一 ...
- ●Codevs 4158 残缺的字符串
题链: http://codevs.cn/problem/4158/ 题解: FFT. 定义两个相同长度的字符串s1,s2的距离为 $$dis(s1,s2)=\sum_{i=0}^{len-1}(s1 ...
- 抽象方法不能是static或native或synchroniz
abstract 是抽象了,只有声明,没有具体的实现方法 static是静态的,是一种属于类而不属于对象的方法或者属性,而我们知道,类其实也是一个对象,他是在class文件加载到虚拟机以后就会产生的对 ...
- Python【第三课】 函数基础
本篇内容 函数基本语法及特性 嵌套函数 递归函数 匿名函数 高阶函数 内置函数 1.函数的基本语法及特性 1.1 函数概念 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提 ...