using Common.Repository.Interface; using Dapper; using Dapper.Contrib.Extensions; using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; using Common; using Microsoft.EntityFrameworkCore; namespace Shared.Repository { /// /// Dapper帮助类 /// public class DapperDBBase : DbContext, IDapperDBBase { private string sqlConnection; public DapperDBBase() { sqlConnection = sysOptionConfig.YsWebconfig.DapperDbString; } /// /// /// /// public IDbConnection GetConn(string type = null) { if (string.IsNullOrEmpty(sqlConnection)) sqlConnection = sysOptionConfig.YsWebconfig.DapperDbString; IDbConnection conn = new SqlConnection(sqlConnection); conn.Open(); return conn; } /// /// /// /// /// /// public List QuerySP(string SPName) { using (IDbConnection Conn = GetConn()) { return Conn.Query(SPName, null, null, true, 0, CommandType.StoredProcedure).AsList(); } } /// /// /// /// /// /// /// public List QuerySP(string SPName, object parms) { using (IDbConnection Conn = GetConn()) { return Conn.Query(SPName, parms, null, true, 0, CommandType.StoredProcedure).AsList(); } } /// /// /// /// /// /// public async Task> QuerySPAsync(string SPName) { using (IDbConnection Conn = GetConn()) { // return await Task.Run(() => conn.Query(SPName, null, null, true, null, CommandType.StoredProcedure).ToList()); return (await Conn.QueryAsync(SPName, null, null, 0, CommandType.StoredProcedure)).AsList(); } } /// /// /// /// /// /// /// public async Task> QuerySPAsync(string SPName, object parms) { using (IDbConnection Conn = GetConn()) { return (await Conn.QueryAsync(SPName, parms, null, 0, CommandType.StoredProcedure)).AsList(); } } /// /// /// /// /// public async Task DeleteAsync(int Id, string deleteSql) { using (IDbConnection Conn = GetConn()) { return await Conn.ExecuteAsync(deleteSql, new { Id = Id }); } } /// /// /// /// /// public async Task DetailFirstAsync(int Id, string detailSql) { using (IDbConnection Conn = GetConn()) { return await Conn.QueryFirstOrDefaultAsync(detailSql, new { Id = Id }); } } /// /// 自定义执行多表事务存储过程 /// /// 存储过程过程名 /// 参数 /// /// /// public async Task ExecSPTransAsync(string SPName, object parms, IDbConnection conn, IDbTransaction tran) { if (conn.State == ConnectionState.Open) { return await conn.ExecuteAsync(SPName, parms, tran, 20, CommandType.StoredProcedure); } else { return -1; } } /// /// 执行中包含事务 /// /// /// /// public async Task ExecSPTransAsync(string SPName, object parms) { using (IDbConnection Conn = GetConn()) { using (IDbTransaction tran = Conn.BeginTransaction()) { var ret = await ExecSPTransAsync(SPName, parms, Conn, tran); tran.Commit(); return ret; } } } /// /// 不带事务的执行存储过程 /// /// /// /// public async Task ExecSPAsync(string SPName, object parms) { using (IDbConnection Conn = GetConn()) { return await ExecSPTransAsync(SPName, parms, Conn, null); } } /// /// /// /// /// /// /// public async Task QueryDataSetAsync(string Sql, object parms, CommandType commandType) { var ds = new DataSet(); using (IDbConnection Conn = GetConn()) { var reader = await Conn.ExecuteReaderAsync(Sql, parms, null, 0, commandType); int i = 0; while (!reader.IsClosed) { ds.Tables.Add($"table{i}"); ds.Tables[i].Load(reader); i++; } } return ds; } /// /// /// /// /// /// public async Task InsertAsync(string insertSql, object parms) { using (IDbConnection Conn = GetConn()) { return await Conn.ExecuteAsync(insertSql, parms); } } /// /// /// /// /// /// public async Task> SelectAsync(string selectSql) { using (IDbConnection Conn = GetConn()) { return (await Conn.QueryAsync(selectSql)).AsList(); } } /// /// /// /// /// /// public async Task UpdateAsync(string updateSql, object parms) { using (IDbConnection Conn = GetConn()) { return await Conn.ExecuteAsync(updateSql, parms); } } /// /// 获取所有数据 /// Author GHB 2021-6-7 /// /// 所有数据 public async Task> QueryAllAsync() where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.GetAllAsync(); } } /// /// 通过主键查询数据。 /// Author GHB 2021-6-7 /// /// 主键值 /// 查询结果 public async Task QueryOneAsync(int id) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.GetAsync(id); } } /// /// 插入一组数据并且返回自动生成的主键的值 /// Author GHB 2021-6-7 /// /// 要插入的数据 /// 自动生成的主键的值 public async Task InsertByEntityAsync(T entity) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.InsertAsync(entity); } } /// /// 批量插入数据并且返回插入数据的个数 /// Author GHB 2021-6-7 /// /// 要插入的数据 /// 数量 public async Task InsertByEntitysAsync(IEnumerable entitys) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.InsertAsync(entitys); } } /// /// 根据主键修改数据 /// Author GHB 2021-6-7 /// /// 修改后的数据 /// 是否修改成功 public async Task UpdateByEntityAsync(T entity) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.UpdateAsync(entity); } } /// /// 批量修改数据 /// Author GHB 2021-6-7 /// /// 实体模型 /// public async Task UpdateByEntitysAsync(IEnumerable entitys) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.UpdateAsync(entitys); } } /// /// 根据主键删除数据 /// Author GHB 2021-6-7 /// /// 实体模型 /// public async Task DeleteByEntityAsync(T entity) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.DeleteAsync(entity); } } /// /// 批量删除数据 /// Author GHB 2021-6-7 /// /// 实体模型 /// public async Task DeleteByEntitysAsync(IEnumerable entitys) where T : class { using (IDbConnection Conn = GetConn()) { return await Conn.DeleteAsync(entitys); } } /// /// 查找唯一记录,如果存在多个记录,则异常 /// Author GHB 2021-6-11 /// /// 返回类型 /// 查询语句 /// 查询参数 /// public async Task QuerySingleOrDefaultAsync(string sql, object parms) { using (IDbConnection Conn = GetConn()) { return await Conn.QuerySingleOrDefaultAsync(sql, parms); } } /// /// 默认返回第一条数据 /// /// 返回类型 /// 查询语句 /// public async Task QueryFirstOrDefaultAsync(string sql) { using (IDbConnection Conn = GetConn()) { return await Conn.QueryFirstOrDefaultAsync(sql); } } /// /// 根据查询SQL直接返回datatable /// Author GHB 2021-6-15 /// /// 实体模型 /// public async Task QueryToDatableAsync(string querySQL) { using (IDbConnection Conn = GetConn()) { DataTable myDatatable = new DataTable("myDatatable"); var reader = await Conn.ExecuteReaderAsync(querySQL); myDatatable.Load(reader); return myDatatable; } } /// /// 根据查询SQL直接返回datatable /// Author GHB 2021-6-15 /// /// 实体模型 /// public async Task QueryToDatableAsync(string querySQL, DynamicParameters parms) { using (IDbConnection Conn = GetConn()) { DataTable myDatatable = new DataTable("myDatatable"); var reader = await Conn.ExecuteReaderAsync(querySQL, parms); myDatatable.Load(reader); return myDatatable; } } /// /// 执行SQL语句返回结果gjg /// /// /// /// public async Task ExecuteScalarAsync(string Sql, object parms) { using (IDbConnection Conn = GetConn()) { return await Conn.ExecuteScalarAsync(Sql, parms); } } /// /// 执行中包含事务sql /// /// /// public async Task ExecSqlTransAsync(List listSql) { int iResult = 0; using (IDbConnection Conn = GetConn()) { using (IDbTransaction tran = Conn.BeginTransaction()) { try { foreach (var sql in listSql) { await Conn.ExecuteAsync(sql, null); } tran.Commit(); iResult = 1; } catch { tran.Rollback(); iResult = -1; } } } return iResult; } /// /// 开启事务 /// /// /// public IDbTransaction BeginTransaction(IDbConnection conn) { IDbTransaction tran = conn.BeginTransaction(); return tran; } /// /// 提交事务 /// /// /// public void Commit(IDbTransaction tran, IDbConnection conn) { tran.Commit(); } /// /// 回滚事务 /// /// /// public void Rollback(IDbTransaction tran, IDbConnection conn) { tran.Rollback(); } /// 数据库连接名 private static string _connection = string.Empty; /// 获取连接名 private static string Connection { get { return _connection; } set { _connection = value; } } /// 定义一个标识确保线程同步 private static readonly object locker = new object(); private static DapperDBBase uniqueInstance; /// /// 获取实例,这里为单例模式,保证只存在一个实例 /// /// public static DapperDBBase GetInstance() { // 双重锁定实现单例模式,在外层加个判空条件主要是为了减少加锁、释放锁的不必要的损耗 if (uniqueInstance == null) { lock (locker) { if (uniqueInstance == null) { uniqueInstance = new DapperDBBase(); } } } return uniqueInstance; } } }