Dapper在.Net中的使用(二)
配置和通用方法
1、获取数据库配置和配置数据连接
1 /// <summary> 2 /// 打开Oracle链接 3 /// </summary> 4 /// <returns></returns> 5 public static OracleConnection OpenOracleConnection() 6 { 7 return OpenOracleConnection(DapperOracleEnum.BasicConnecting); 8 } 9 10 /// <summary> 11 /// 打开Oracle链接 12 /// </summary> 13 /// <param name="oracleEnum"></param> 14 /// <returns></returns> 15 public static OracleConnection OpenOracleConnection(DapperOracleEnum oracleEnum) 16 { 17 switch ((int)oracleEnum) 18 { 19 default: 20 var connection = new OracleConnection(BasicConnecting); 21 connection.Open(); 22 return connection; 23 } 24 }
2、查询
1 /// <summary> 2 /// 获取列表 3 /// </summary> 4 /// <typeparam name="T"></typeparam> 5 /// <param name="sql"></param> 6 /// <returns></returns> 7 public static List<T> GetList<T>(string sql) 8 { 9 using (IDbConnection conn = OpenOracleConnection()) 10 { 11 try 12 { 13 var result = conn.Query<T>(sql); 14 if (result != null) 15 { 16 return result.ToList(); 17 } 18 } 19 catch (Exception ex) 20 { 21 NlogHelper.WtiteDapper("Dapper", "GetList", sql, ex.Message); 22 throw new Exception(ex.Message); 23 } 24 } 25 return new List<T>(); 26 } 27 28 /// <summary> 29 /// 获取列表 30 /// </summary> 31 /// <typeparam name="T"></typeparam> 32 /// <param name="sql"></param> 33 /// <param name="oracleEnum"></param> 34 /// <returns></returns> 35 public static List<T> GetList<T>(string sql, DapperOracleEnum oracleEnum) 36 { 37 using (IDbConnection conn = OpenOracleConnection(oracleEnum)) 38 { 39 try 40 { 41 var result = conn.Query<T>(sql); 42 if (result != null) 43 { 44 return result == null ? new List<T>() : result.ToList(); 45 } 46 } 47 catch (Exception ex) 48 { 49 NlogHelper.WtiteDapper("Dapper", "GetList", sql, ex.Message); 50 throw new Exception(ex.Message); 51 } 52 } 53 return new List<T>(); 54 } 55 56 /// <summary> 57 /// 获取详情 58 /// </summary> 59 /// <typeparam name="T"></typeparam> 60 /// <param name="sql"></param> 61 /// <returns></returns> 62 public static T GetDetail<T>(string sql) where T : new() 63 { 64 using (IDbConnection conn = OpenOracleConnection()) 65 { 66 try 67 { 68 var result = conn.Query<T>(sql); 69 if (result != null) 70 { 71 return result == null ? new T() : result.ToList().FirstOrDefault(); 72 } 73 } 74 catch (Exception ex) 75 { 76 NlogHelper.WtiteDapper("Dapper", "GetDetail", sql, ex.Message); 77 throw new Exception(ex.Message); 78 } 79 } 80 return new T(); 81 } 82 /// <summary> 83 /// 获取详情 84 /// </summary> 85 /// <typeparam name="T"></typeparam> 86 /// <param name="sql"></param> 87 /// <param name="oracleEnum"></param> 88 /// <returns></returns> 89 public static T GetDetail<T>(string sql, DapperOracleEnum oracleEnum) where T : new() 90 { 91 using (IDbConnection conn = OpenOracleConnection(oracleEnum)) 92 { 93 try 94 { 95 var result = conn.Query<T>(sql); 96 if (result != null) 97 { 98 return result == null ? new T() : result.ToList().FirstOrDefault(); 99 } 100 } 101 catch (Exception ex) 102 { 103 NlogHelper.WtiteDapper("Dapper", "GetDetail", sql, ex.Message); 104 throw new Exception(ex.Message); 105 } 106 } 107 return new T(); 108 } 109 110 /// <summary> 111 /// 获取列的值 112 /// </summary> 113 /// <param name="sql"></param> 114 /// <returns></returns> 115 public static object GetScalar(string sql) 116 { 117 using (IDbConnection conn = OpenOracleConnection()) 118 { 119 try 120 { 121 object result = conn.ExecuteScalar(sql); 122 if (result != null) 123 { 124 return result; 125 } 126 } 127 catch (Exception ex) 128 { 129 NlogHelper.WtiteDapper("Dapper", "GetScalar", sql, ex.Message); 130 throw new Exception(ex.Message); 131 } 132 } 133 return null; 134 }
3、更新
1 /// <summary> 2 /// 更新 3 /// </summary> 4 /// <param name="sql"></param> 5 /// <returns></returns> 6 public static int Edit(string sql) 7 { 8 using (IDbConnection conn = OpenOracleConnection()) 9 { 10 try 11 { 12 var result = conn.Execute(sql); 13 if (result == 0) return 0; 14 } 15 catch (Exception ex) 16 { 17 NlogHelper.WtiteDapper("Dapper", "Edit", sql, ex.Message); 18 throw new Exception(ex.Message); 19 } 20 } 21 return 1; 22 } 23 24 /// <summary> 25 /// 更新 26 /// </summary> 27 /// <param name="dapper"></param> 28 /// <returns></returns> 29 public static int Edit(DapperModel dapper) 30 { 31 using (IDbConnection conn = OpenOracleConnection()) 32 { 33 try 34 { 35 var result = conn.Execute(dapper.Sql, dapper.Param); 36 if (result == 0) return 0; 37 } 38 catch (Exception ex) 39 { 40 NlogHelper.WtiteDapper("Dapper", "Edit", dapper, ex.Message); 41 throw new Exception(ex.Message); 42 } 43 } 44 return 1; 45 } 46 47 /// <summary> 48 /// 批量更新 49 /// </summary> 50 /// <param name="sqlList"></param> 51 /// <returns></returns> 52 public static int EditList(List<string> sqlList) 53 { 54 using (IDbConnection conn = OpenOracleConnection()) 55 { 56 string sql = string.Empty; 57 try 58 { 59 IDbTransaction transaction = conn.BeginTransaction(); 60 var result = 0; 61 foreach (var item in sqlList) 62 { 63 sql = item; 64 result += conn.Execute(item, transaction); 65 } 66 transaction.Commit(); 67 //if (result != sqlList.Count) return 0; 68 } 69 catch (Exception ex) 70 { 71 NlogHelper.WtiteDapper("Dapper", "EditList", sql, ex.Message); 72 throw new Exception(ex.Message); 73 } 74 } 75 return 1; 76 } 77 78 /// <summary> 79 /// 批量更新 80 /// </summary> 81 /// <param name="dapperList"></param> 82 /// <returns></returns> 83 public static int EditList(List<DapperModel> dapperList) 84 { 85 using (IDbConnection conn = OpenOracleConnection()) 86 { 87 DapperModel sql = new DapperModel();//记录错误的语句 88 try 89 { 90 IDbTransaction transaction = conn.BeginTransaction(); 91 var result = 0; 92 foreach (var dapper in dapperList) 93 { 94 sql = dapper; 95 result += conn.Execute(dapper.Sql, dapper.Param, transaction); 96 } 97 transaction.Commit(); 98 //if (result != dapperList.Count) return 0; 99 } 100 catch (Exception ex) 101 { 102 NlogHelper.WtiteDapper("Dapper", "EditList", sql.ToJson(), ex.Message); 103 throw new Exception(ex.Message); 104 } 105 } 106 return 1; 107 }
4、删除
1 /// <summary> 2 /// 删除 3 /// </summary> 4 /// <param name="sql"></param> 5 /// <returns></returns> 6 public static int Del(string sql) 7 { 8 using (IDbConnection conn = OpenOracleConnection()) 9 { 10 try 11 { 12 var result = conn.Execute(sql); 13 if (result == 0) return 0; 14 } 15 catch (Exception ex) 16 { 17 NlogHelper.WtiteDapper("Dapper", "Edit", sql, ex.Message); 18 throw new Exception(ex.Message); 19 } 20 } 21 return 1; 22 }
5、获取下一个序列
1 /// <summary> 2 /// 获取下一个序列 3 /// </summary> 4 /// <param name="sequenceName"></param> 5 /// <returns></returns> 6 public static int NextSequence(string sequenceName) 7 { 8 string sql = $@"select {sequenceName}.nextval as sequencevalue from dual"; 9 var sequence = GetScalar(sql); 10 if (sequence != null) return sequence.ToInt(); 11 return -1; 12 }
6、调用存储过程
1 /// <summary> 2 /// 调用存储过程 3 /// </summary> 4 /// <param name="sql"></param> 5 /// <returns></returns> 6 public static object GetCommand(string sql) 7 { 8 return GetCommand(sql, null); 9 } 10 /// <summary> 11 /// 调用存储过程 12 /// </summary> 13 /// <param name="sql"></param> 14 /// <param name="parames"></param> 15 /// <returns></returns> 16 public static object GetCommand(string sql, DynamicParameters parames) 17 { 18 return GetCommand(sql, parames, CommandType.StoredProcedure); 19 } 20 /// <summary> 21 /// 调用存储过程 22 /// </summary> 23 /// <param name="sql"></param> 24 /// <param name="parames"></param> 25 /// <param name="type"></param> 26 /// <returns></returns> 27 public static object GetCommand(string sql, DynamicParameters parames, CommandType type) 28 { 29 using (IDbConnection conn = OpenOracleConnection()) 30 { 31 try 32 { 33 object result = conn.Query(sql, parames, null, true, null, type); ; 34 if (result != null) 35 { 36 return result; 37 } 38 } 39 catch (Exception ex) 40 { 41 NlogHelper.WtiteDapper("Dapper", "GetCommand", sql, ex.Message); 42 throw new Exception(ex.Message); 43 } 44 } 45 return null; 46 }
7、实体
1 /// <summary> 2 /// Dapper操作 3 /// </summary> 4 public class DapperModel 5 { 6 /// <summary> 7 /// sql语句 8 /// </summary> 9 public string Sql { get; set; } 10 /// <summary> 11 /// 参数 12 /// </summary> 13 public object Param { get; set; } 14 } 15 16 /// <summary> 17 /// 数据库类型 18 /// </summary> 19 public enum DapperOracleEnum 20 { 21 /// <summary> 22 /// ITS_Basic 23 /// </summary> 24 BasicConnecting = 0, 25 }
备注NlogHelper是基于NLOG日志系统的帮助类
转载于:https://www.cnblogs.com/raniynight/p/10450850.html
Dapper在.Net中的使用(二)相关推荐
- Java IO在Android中应用(二):APK加固
Java I/O在Android中应用(二):APK加固套壳 前言(废话) 我,有两把键盘,第一把是Poker III(黑轴),第二把是Poker II(红轴).工作的时候我常用的是红轴的Poker ...
- c语言中二维数组怎么,c语言中什么是二维数组
c语言中什么是二维数组 二维数组A[m][n],这是一个m行,n列的二维数组.以下是由应届毕业生小编整理关于c语言二维数组的内容,希望对大家有所帮助!更多相关内容请关注应届毕业生考试 网. c语言的二 ...
- C语言错误c2063,请教C语言中关于将二维数组复制到一个三维数组的问题
请教C语言中关于将二维数组复制到一个三维数组的问题 小白正在填写一段代码,是有点类似与俄罗斯方块的一段游戏代码,代码如下 要求在增加参数,变量和函数的情况下完成此代码,只能填写// write app ...
- 【100题】三十五 求一个矩阵中最大的二维矩阵(元素和最大)
一,题目: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 0 3 4 2 3 4 5 1 1 1 5 3 0 中最大的是: 4 5 5 3 要求:(1)写出算法;(2)分析时间复杂度;(3) ...
- (翻译)开始iOS 7中自动布局教程(二)
(翻译)开始iOS 7中自动布局教程(二) 这篇教程的前半部分被翻译出来很久了,我也是通过这个教程学会的IOS自动布局.但是后半部分(即本篇)一直未有翻译,正好最近跳坑翻译,就寻来这篇教程,进行 ...
- autocad2007二维图画法_CAD中如何绘制二维图形
当我们在使用CAD软件绘制图纸时,我们一般情况下绘制的都是二维图形,用到的绘图工具也有很多.那在浩辰CAD软件中如何绘制二维图形呢?今天就为大家简单介绍下. CAD软件中绘制二维图的方法: 1.绘图菜 ...
- vue 项目中 自动生成 二维码
vue 项目中 自动生成 二维码 最近在写一个vue项目,要求根据卡号可以自动生成一个二维码,并渲染在指定位置,因为第一次做类似业务,小编在网上找了找,发现了很多,具体起来主要用的就两种: QRc ...
- 在vue中使用element-ui二次封装面包屑导条
在vue中使用element-ui二次封装面包屑导条 由于这几天写了一个后台管理系统,多次使用的到了面包屑导航,所以我就把它封装起来使用了: 效果图 第一步 安装element-ui npm i el ...
- 面试题--5个数能组成多少中不同的二叉搜索树的结构--卡特兰数
面试题--5个数能组成多少中不同的二叉搜索树的结构--卡特兰数 题目描述: 1,2,3,4,5 五个数字,能组成多少种不同的二叉搜索树的结构?(D) A. 32 B. 36 C. 40 D. 42 解 ...
- 线索树找*p的中序后继且中序遍历 二叉线索树
//线索树找*p的中序后继且中序遍历 二叉线索树 #define thread 1 #define link 0 typedef struct Bt{char data;struct Bt *lc;/ ...
最新文章
- 从事了两年 AI 研究,我学到了什么?
- 适合写python的电脑_这篇写给想选计算机专业的学弟学妹们
- Ngnix负载均衡安装及配置
- Python 基本数据类型、运算符
- java入职华为,通过这9个Java面试题,就可以入职华为啦
- php7.1 伪静态失效,解决CodeIgniter伪静态失效
- micropython和python区别-什么是MicroPython?
- 知方可补不足~说说吧!timestamp有什么用?
- [leetcode]72. Edit Distance 最少编辑步数
- 关键路径过程详解、算法及其实现
- 手机滚动截屏软件推荐
- 重装系统win10步骤和教程,win10怎么自己重装系统
- 空时编码的理论与实践(3)空时编码设计准则
- cs_Censorship_CreateUpdateDelete///cs_Censorships_Get
- 石油化工行业智能供应链管理系统解决方案:数智化供应链平台推动企业转型智能化发展
- Java + Swing + MySQL实现图书管理系统
- Origin 如何输出高清图片
- 怎么用计算机算三角函数值,如何在手机计算器中由三角函数值求角度
- LeetCode 玩筹码
- 【渝粤教育】广东开放大学 搜索引擎营销(SEM) 形成性考核 (47)