Dapepr讲解
上篇文章我们介绍了,什么是Dapepr,有什么好处,性能的对比,还有多表多数据添加操作(事务的封装)等等。本篇文章我们继续讲解。。。。。如果本篇文章看不懂,请看我上一篇文章:Dapeper的入门
Select查询
1、首先我们先在SQLMapper中QueryMultiple()方法,在梳理Dapper中无意中发现,个人感觉不错,
// // 摘要: // Execute a command that returns multiple result sets, and access each in turn:
// 上句话的翻译是: 执行一个返回多个结果集的命令,并依次访问每个结果集
public static SqlMapper.GridReader QueryMultiple(this IDbConnection cnn, CommandDefinition command); // // 摘要: // Execute a command that returns multiple result sets, and access each in turn public static SqlMapper.GridReader QueryMultiple(this IDbConnection cnn, string sql, object param = null, IDbTransaction transaction = null,
int? commandTimeout = null, CommandType? commandType = null); // // 摘要: // Execute a command that returns multiple result sets, and access each in turn public static Task<SqlMapper.GridReader> QueryMultipleAsync(this IDbConnection cnn, CommandDefinition command); // // 摘要: // Execute a command that returns multiple result sets, and access each in turn public static Task<SqlMapper.GridReader> QueryMultipleAsync(this IDbConnection cnn, string sql, object param = null,
IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null);
2、老规矩我们在DapperDBContext 中加个方法,我们可以根据不同业务扩展方法,
/// <summary> /// WueryMultiplie 执行一个返回多个结果集的命令,并依次访问每个结果集 /// </summary> /// <param name="sql"></param> /// <param name="param"></param> /// <param name="transaction"></param> /// <param name="commandTimeout"></param> /// <param name="commandType"></param> /// <param name="databaseOption"></param> /// <returns></returns> public static Dapper.SqlMapper.GridReader QueryMultiple(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null,
CommandType? commandType = null, int databaseOption = 1) { var conn = ConnectionFactory.MySqlConnection(); var sw = new Stopwatch(); sw.Start(); try { // 因为using 的作用域之后,连接自动关闭, //这里需要说明的是,在数据读取完毕之后,不再需要SqlDataReader时,必须将其进行手动关闭 var restult = conn.QueryMultiple(sql, param, transaction, commandTimeout, commandType); sw.Stop(); return restult; } catch (Exception ex) { LogHelper.ErrorLog(ex.ToString()); throw new Exception(ex.ToString()); } }
注意事项:不知道你在上面代码中有没有遇到一个问题,上篇文章我们封装用到using,这次没有用到,是因为封装过程报错:报错:“阅读器关闭时尝试调用 Read 无效” ,原因也很简单:因为using 的作用域之后,连接自动关闭,这里需要说明的是,在数据读取完毕或者不再需要SqlDataReader时,必须将其进行手动关闭。
3、SQL调用
/// <summary> /// 单表查数据 /// </summary> /// <returns></returns> public List<Department> select() { string sql = @"select *from Department"; var list = DapperDBContext.Query<Department>(sql, null).ToList();
//(多个结果集)为了比较就写一起 var all = selectAll(); var dep = all.Read<Department>().ToList(); var auth = all.Read<AuthResources>().ToList(); return list; } /// <summary> /// 多个结果集 /// </summary> /// <returns></returns> public Dapper.SqlMapper.GridReader selectAll() { var sql = "select * from Department; select * from AuthResources"; var multiReader = DapperDBContext.QueryMultiple(sql); return multiReader; }
效果:
- 博主是利用读书、参考、引用、复制和粘贴等多种方式打造成自己的纯镀 24k 文章,请原谅博主成为一个无耻的文档搬运工!
- 小弟刚迈入博客编写,文中如有不对,欢迎板砖扶正,希望对你有所帮助。