public T Query(string sql, object param)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result= dbConnection.QueryFirstOrDefault<T>(sql, param);
dbConnection.Close();
return result;
} } /// <summary>
/// 多用于事务
/// </summary>
/// <param name="dbConnection"></param>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public T Query(IDbConnection dbConnection, string sql, object param, IDbTransaction transaction = null)
{
return dbConnection.QueryFirstOrDefault<T>(sql, param, transaction);
} public async Task<T> QueryAsync(string sql, object param)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result= await dbConnection.QueryFirstOrDefaultAsync<T>(sql, param);
dbConnection.Close();
return result;
}
}
public async Task<T> QueryAsync(IDbConnection dbConnection, string sql, object param, IDbTransaction transaction = null)
{
return await dbConnection.QueryFirstOrDefaultAsync<T>(sql, param, transaction);
} public IList<T> QueryToList(string sql, object param)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = dbConnection.Query<T>(sql, param).ToList();
dbConnection.Close();
return result;
}
}
public IList<T> QueryToList<T>(string sql, object param)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = dbConnection.Query<T>(sql, param).ToList();
dbConnection.Close();
return result;
}
}
public IList<T> QueryToList(IDbConnection dbConnection, string sql, object param, IDbTransaction transaction = null)
{
return dbConnection.Query<T>(sql, param, transaction).ToList();
}
public async Task<IList<T>> QueryToListAsync(string sql, object param)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = (await dbConnection.QueryAsync<T>(sql, param)).ToList();
dbConnection.Close();
return result;
}
} public async Task<IList<T>> QueryToListAsync<T>(string sql, object param)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = (await dbConnection.QueryAsync<T>(sql, param)).ToList();
dbConnection.Close();
return result;
}
} public async Task<IList<T>> QueryToListAsync(IDbConnection dbConnection, string sql, object param, IDbTransaction transaction = null)
{
return (await dbConnection.QueryAsync<T>(sql, param, transaction)).ToList();
}
/// <summary>
/// 查询数据库,返回指定ID的对象
/// </summary>
/// <param name="id">主键的值</param>
/// <returns></returns>
public T FindById(object id)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = dbConnection.Get<T>(id);
dbConnection.Close();
return result;
}
}
public T FindById(IDbConnection dbConnection, object id, IDbTransaction transaction = null)
{
return dbConnection.Get<T>(id, transaction);
}
public T GetById(int id)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = dbConnection.Get<T>(id);
dbConnection.Close();
return result;
}
}
public T GetById(IDbConnection dbConnection, int id, IDbTransaction transaction = null)
{
return dbConnection.Get<T>(id, transaction);
} public async Task<T> GetByIdAsync(int id)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result= await dbConnection.GetAsync<T>(id);
dbConnection.Close();
return result;
}
}
public async Task<T> GetByIdAsync(IDbConnection dbConnection, int id, IDbTransaction transaction = null)
{
return await dbConnection.GetAsync<T>(id, transaction);
}
/// <summary>
/// 查询数据库,返回指定ID的对象
/// </summary>
/// <param name="id">主键的值</param>
/// <returns></returns>
public async Task<T> FindByIdAsync(object id)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result= await dbConnection.GetAsync<T>(id);
dbConnection.Close();
return result;
}
}
public async Task<T> FindByIdAsync(IDbConnection dbConnection, object id, IDbTransaction transaction = null)
{
return await dbConnection.GetAsync<T>(id, transaction);
}
/// <summary>
/// 插入返回主键
/// </summary>
/// <param name="sql"></param>
/// <param name="param"></param>
/// <returns>
/// //string sql = @"Insert into users (UserName) values (@name);
/// //object param=new Object(){ name = member.UserName}
/// </returns>
public async Task<int> InsertedIdAsync(string sql, object param = null)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
sql = sql.TrimEnd(';') + ";select LAST_INSERT_ID()";
var result= await dbConnection.ExecuteScalarAsync<int>(sql, param);
dbConnection.Close();
return result;
}
}
public async Task<int> InsertedIdAsync(IDbConnection dbConnection, string sql, object param = null, IDbTransaction transaction = null)
{
sql=sql.TrimEnd(';') +";select LAST_INSERT_ID()";
return await dbConnection.ExecuteScalarAsync<int>(sql, param, transaction);
}
public int InsertedId(string sql, object param = null)
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
sql = sql.TrimEnd(';') + ";select LAST_INSERT_ID()";
var result= dbConnection.ExecuteScalar<int>(sql, param);
dbConnection.Close();
return result;
}
}
public int InsertedId(IDbConnection dbConnection, string sql, object param = null, IDbTransaction transaction = null)
{
sql = sql.TrimEnd(';') + ";select LAST_INSERT_ID()";
return dbConnection.ExecuteScalar<int>(sql, param, transaction);
} public IList<T> GetAll()
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = dbConnection.GetAll<T>().ToList();
dbConnection.Close();
return result;
}
}
public IList<T> GetAll(IDbConnection dbConnection, IDbTransaction transaction = null)
{
return dbConnection.GetAll<T>(transaction).ToList();
} public async Task<IList<T>> GetAllAsync()
{
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
var result = (await dbConnection.GetAllAsync<T>()).ToList();
dbConnection.Close();
return result;
}
}
public async Task<IList<T>> GetAllAsync(IDbConnection dbConnection, IDbTransaction transaction = null)
{
return (await dbConnection.GetAllAsync<T>(transaction)).ToList();
} public bool Insert(T entity)
{
bool result = false;
using (IDbConnection dbConnection = Connection)
{
if (dbConnection.State == ConnectionState.Closed)
{
dbConnection.Open();
}
result = dbConnection.Insert(entity) > ;
dbConnection.Close();
}
return result;
}
public bool Insert(T entity, IDbConnection dbConnection, IDbTransaction transaction = null)
{
bool result = dbConnection.Insert(entity, transaction) > ;
return result;
}

参照:

https://www.cnblogs.com/wuhuacong/p/9952900.html

使用Dapper.Contrib的更多相关文章

  1. Dapper扩展之~~~Dapper.Contrib

    平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...

  2. Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property

    异常处理:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 原来Model是这样滴 修改后是这样滴 注意点:Model里面的Table和Key ...

  3. Dapper.Contrib——更加优雅地使用Dapper进行增删改查

    简介 Dapper是介于Entity framework与ADO的折中选择.既满足手写查询的高性能需求,又简化了数据库对象映射为内存对象的繁杂工作.Dapper.Contrib是对Dapper的进一步 ...

  4. Dapper.Contrib拓展及数据库生成实体

    1.关于Dapper.Contrib Dapper.Contrib是Dapper的一个拓展类库,Dapper是一个轻量级ORM框架,这个不了解的请自行收集资料,本文主要讲讲,数据库生成实体,并通过实体 ...

  5. EF架构~Dapper.Contrib不能将Linq翻译好发到数据库,所以请不要用它

    回到目录 对于Dapper是一个轻量级的数据访问框架,而需要使用者去自己做SQL,它,只是一个数据访问者! 对些,Dapper推出了Contrib扩展包,它可以友好的让开发人员使用linq,而不需要写 ...

  6. 使用Dapper.Contrib 开发.net core程序,兼容多种数据库

    关于Dapper的介绍,我想很多人都对它有一定的了解,这个类似一个轻型的ORM框架是目前应用非常火的一个东西,据说各方面的性能都不错,而且可以支持多种数据库,在开始介绍这个文章之前,我花了不少功夫来学 ...

  7. DapperExtensions and Dapper.Contrib在表构架不是默认dbo时的处理 DapperExtensions and Dapper.Contrib with non-dbo Schema

    什么是数据库的Schema dbo是一个构架(schema),与sql2000不同的是,在sql2005中,表的调用格式如下:"数据库名.构架名.表名",同一个用户可以被授权访问多 ...

  8. Dapper.Contrib.Extensions问题

    Dapper.Contrib.Extensions问题 Dapper.Extension.1.0.0.1\lib\net45\Dapper.Extension.dll Dapper.Contrib.1 ...

  9. SQL 横转竖 、竖专横 (转载) 使用Dapper.Contrib 开发.net core程序,兼容多种数据库 C# 读取PDF多级书签 Json.net日期格式化设置 ASPNET 下载共享文件 ASPNET 文件批量下载 递归,循环,尾递归 利用IDisposable接口构建包含非托管资源对象 《.NET 进阶指南》读书笔记2------定义不可改变类型

    SQL 横转竖 .竖专横 (转载)   普通行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 ...

  10. VS2017 MVC项目,新建控制器提示未能加载文件或程序集“Dapper.Contrib解决方法

    VS2017中MVC项目中,右键新建控制器时,提示 未能加载文件或程序集“Dapper.Contrib, Version=1.50.0.0, Culture=neutral, PublicKeyTok ...

随机推荐

  1. Django图书管理系统(前端对数据库的增删改查)

    图书管理系统 出版社的管理 源码位置:https://gitee.com/machangwei-8/learning_materials/tree/master/%E9%A1%B9%E7%9B%AE/ ...

  2. Win10任务栏卡死解决方法

    有时,切换输入法,或者点击windows底部菜单,电脑鼠标会变成忙碌转圈圈的状态,无法操作底部菜单功能: 1. ctrl+shift+esc(或者ctrl+alt+delete)的方式调出任务管理器: ...

  3. python中的lambda、map、reduce、filter

    filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple(取决 ...

  4. python算法与数据结构-算法介绍(31)

    一.算法和数据结构 什么是算法和数据结构?如果将最终写好运行的程序比作战场,我们程序员便是指挥作战的将军,而我们所写的代码便是士兵和武器. 那么数据结构和算法是什么?答曰:兵法!故,数据结构和算法是一 ...

  5. Alpha冲刺随笔七:第七天

    课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(十天冲刺) 团队名称:葫芦娃队 作业目标:在十天冲刺里对每天的任务进行总结. 随笔汇总:https://www.cnblogs ...

  6. 2.spring的主要模块作用

    spring中大约包含20过个模块, 主要包括以下几部分: 1. Core Container Core Container(核心容器)包含Core,Beans,Context和Expression ...

  7. TCP/IP 协议栈及 OSI 参考模型详解

    OSI参考模型 OSI RM:开放系统互连参考模型(open systeminterconnection reference model) OSI参考模型具有以下优点: 简化了相关的网络操作: 提供设 ...

  8. 导入Excel数据到Oracle数据库的脚本

    在cmd运行窗口中输入:sqlldr customermanager/123@orcl control="E:\CustomerData\excelInputOracle\insert.ct ...

  9. Storage事件及综合案例

    说到Storage事件,那么就得先给大家说一下localstorage和sessionstorage: 1.localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. ...

  10. [学习笔记]约数&欧拉函数

    约数 一.概念 约数,又称因数.整数a除以整数b(b≠0) 除得的商正好是整数而没有余数,我们就说a能被b整除,或b能整除a.a称为b的倍数,b称为a的约数. 二.性质 1.整数唯一分解 1)定义 对 ...