博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ADO.NET 根据实体类自动生成添加修改语句仅限Oracle使用
阅读量:5344 次
发布时间:2019-06-15

本文共 11352 字,大约阅读时间需要 37 分钟。

话不多说直接上代码,新手上路,高手路过勿喷,请多多指教。

///         /// 等于号        ///         private readonly static string eq = string.Format(string.Empty + Convert.ToChar(32) + Convert.ToChar(61) + Convert.ToChar(32));        ///         /// 条件变量        ///         private readonly static string where = string.Format(string.Empty + Convert.ToChar(32) + "WHERE 1" + eq + "1" + Convert.ToChar(32));        ///         /// 异常信息        ///         private readonly static string errInfo = string.Format("传入的key不存在");        ///         /// 录入信息存在风险        ///         private readonly static string injection = string.Format("录入信息存在风险");

 

///         /// 添加Sql语句函数 fg        ///         /// 
/// /// 主键名称,必须是实体对象中存在 /// 主键值(序列) /// ///
fg: 添加根据实体类接收的数据进行拼接sql语句
public static string Added
(T t, string key, string value, out string outmsg) { bool blo = false; outmsg = string.Empty; string sql = string.Empty, error = string.Empty; StringBuilder sqlField = new StringBuilder(); StringBuilder sqlValue = new StringBuilder(); try { if (t == null) return sql; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) throw new Exception("对象实例化失败,请检查主键是否正确"); Type type = t.GetType(); sqlField.AppendFormat("INSERT INTO " + type.Name + Convert.ToChar(32) + Convert.ToChar(40)); sqlValue.AppendFormat(Convert.ToChar(32) + "VALUES" + Convert.ToChar(40)); sqlField.AppendFormat(key); sqlValue.AppendFormat(value); PropertyInfo[] props = type.GetProperties(); blo = props.Any(o => o.Name.ToUpper() != key); if (!blo) throw new Exception(errInfo); Parallel.ForEach(props, p => { if (p.Name.Equals(key)) return; if (p.GetValue(t, null) == null) return; switch (p.PropertyType.Name) { case "String": var tmp = (string)p.GetValue(t, null); blo = VerificationHelper.VerificationByStr(tmp); if (!blo) error = string.Format(injection); sqlField.AppendFormat(", " + p.Name); sqlValue.AppendFormat(", '" + (string.IsNullOrEmpty(tmp) ? tmp : tmp.Contains("'") ? tmp.Replace("'", "''") : tmp.Trim()) + "'"); break; case "DateTime": sqlField.AppendFormat(", " + p.Name); sqlValue.AppendFormat(", TO_DATE('" + p.GetValue(t, null) + "','YYYY-MM-DD HH24:MI:SS')"); break; default: sqlField.AppendFormat(", " + p.Name); sqlValue.AppendFormat(", '" + p.GetValue(t, null) + "'"); break; } }); if (!string.IsNullOrEmpty(error)) throw new Exception(error); sqlField.AppendFormat(string.Empty + Convert.ToChar(41)); sqlValue.AppendFormat(string.Empty + Convert.ToChar(41)); sql = sqlField + sqlValue.ToString(); return sql; } catch (Exception ex) { outmsg = ex.Message; } return sql; } ///
/// 修改 sql 方法函数 /// ///
///
///
主键,必须是实体对象中存在的字段 ///
主键值 ///
///
fg: 修改函数封装根据接收的实体对象生成sql修改语句
public static string Edited
(T t, string key, string value, out string outmsg) { bool blo = false; outmsg = string.Empty; string sql = string.Empty, error = string.Empty, fieldVars = string.Empty; StringBuilder sqlstr = new StringBuilder(); StringBuilder newSql = new StringBuilder(); try { if (t == null) return sql; if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) throw new Exception("对象实例化失败,请检查主键是否正确"); key = key.ToUpper(); Type type = t.GetType(); PropertyInfo[] props = type.GetProperties(); sqlstr.AppendFormat("UPDATE" + Convert.ToChar(32) + type.Name + Convert.ToChar(32) + "SET" + Convert.ToChar(32)); blo = props.Any(o => o.Name.ToUpper() != key); if (!blo) throw new Exception(errInfo); Parallel.ForEach(props, p => { fieldVars = p.Name.ToUpper(); if (fieldVars.Equals(key)) return; if (p.GetValue(t, null) == null) return; switch (p.PropertyType.Name) { case "String": var tmp = (string)p.GetValue(t, null); tmp = tmp.ToUpper(); blo = VerificationHelper.VerificationByStr(tmp); if (!blo) error = string.Format(injection); sqlstr.AppendFormat(fieldVars + eq + "'" + (string.IsNullOrEmpty(tmp) ? tmp : tmp.Contains("'") ? tmp.Replace("'", "''") : tmp.Trim()) + "', "); break; case "DateTime": sqlstr.AppendFormat(fieldVars + eq + "TO_DATE('" + p.GetValue(t, null) + "','YYYY-MM-DD HH24:MI:SS'), "); break; default: sqlstr.AppendFormat(fieldVars + eq + "'" + p.GetValue(t, null) + "', "); break; } }); if (!string.IsNullOrEmpty(error)) throw new Exception(error); sql = sqlstr.ToString().Remove(sqlstr.Length - 2); sqlstr.Clear(); sqlstr.AppendFormat(sql); sqlstr.AppendFormat(where + "AND" + Convert.ToChar(32) + key + eq + value); sql = sqlstr.ToString(); return sql; } catch (Exception ex) { outmsg = ex.Message; } return sql; } ///
/// 根据条件修改 sql 方法函数 /// ///
///
///
修改条件,直接写条件即可 ///
///
fg: 修改函数封装根据接收的实体对象生成sql修改语句
public static string Editeds
(T t, string key, string condition, out string outmsg) { bool blo = false; outmsg = string.Empty; string sql = string.Empty, error = string.Empty, tmpstr = string.Empty; StringBuilder sqlstr = new StringBuilder(); StringBuilder newSql = new StringBuilder(); try { if (t == null) return sql; if (string.IsNullOrEmpty(condition)) throw new Exception("请先完善条件后,再尝试"); else condition = condition.Trim(); string[] array = condition.Split(' '); string str = array.FirstOrDefault(); str = str.ToUpper(); if (str.Equals("OR")) throw new Exception("拼接条件部分开始不能使用\"OR\"关键字"); if (str.Equals("AND")) { array[0] = string.Empty; foreach (var item in array) tmpstr += item + Convert.ToChar(32); } else tmpstr = condition; Type type = t.GetType(); PropertyInfo[] props = type.GetProperties(); sqlstr.AppendFormat("UPDATE" + Convert.ToChar(32) + type.Name + Convert.ToChar(32) + "SET" + Convert.ToChar(32)); blo = props.Any(o => o.Name.ToUpper() != key); if (!blo) throw new Exception(errInfo); Parallel.ForEach(props, p => { if (p.Name.Equals(key)) return; if (p.GetValue(t, null) == null) return; switch (p.PropertyType.Name) { case "String": var tmp = (string)p.GetValue(t, null); blo = VerificationHelper.VerificationByStr(tmp); if (!blo) error = string.Format(injection); sqlstr.AppendFormat(p.Name + eq + "'" + (string.IsNullOrEmpty(tmp) ? tmp : tmp.Contains("'") ? tmp.Replace("'", "''") : tmp.Trim()) + "', "); break; case "DateTime": sqlstr.AppendFormat(p.Name + eq + "TO_DATE('" + p.GetValue(t, null) + "','YYYY-MM-DD HH24:MI:SS'), "); break; default: sqlstr.AppendFormat(p.Name + eq + "'" + p.GetValue(t, null) + "', "); break; } }); if (!string.IsNullOrEmpty(error)) throw new Exception(error); sql = sqlstr.ToString().Remove(sqlstr.Length - 2); sqlstr.Clear(); sqlstr.AppendFormat(sql); sqlstr.AppendFormat(where + "AND" + Convert.ToChar(32) + tmpstr); sql = sqlstr.ToString(); return sql; } catch (Exception ex) { outmsg = ex.Message; } return sql; }
///     /// 验证帮助类    ///     public class VerificationHelper    {        private static string sqlinjectStr = "=;--;delete ;drop ;alert ;insert ;and ;or ;";        ///         /// 验证字符串是否存在sql注入        ///         /// 验证字符串        /// 
不存在则验证通过,返回true,否则返回false
public static bool VerificationByStr(string str) { string[] strarr = sqlinjectStr.Split(';'); bool result = true; string itemstr = string.Empty; if (string.IsNullOrEmpty(str)) { return true; } else { str = str.ToLower().Trim(); foreach (string item in strarr) { if (!string.IsNullOrEmpty(item)) { if (str.IndexOf(item) > -1 || str.Contains(item)) { itemstr = item; result = false; break; } } } } return result; } }

 

原创不易,作者地址:

转载于:https://www.cnblogs.com/FGang/p/11179086.html

你可能感兴趣的文章
Jquery | 基础 | 事件的链式写法
查看>>
ASP数据库连接方法语法汇总
查看>>
如何实现ZBrush 4R7中按钮颜色的自定义
查看>>
jquery自定义验证方法
查看>>
两个大数相加,使用字符串模拟相加过程
查看>>
vmware workstation虚拟机中的redhat嘟嘟的响
查看>>
http请求中java中的302和sendRedirect的区别
查看>>
ES之六:ElasticSearch中Filter和Query的异同
查看>>
水浒三十二员猛将真实排名(卢俊义非第一)
查看>>
ORACLE 包[转]
查看>>
Simple Pipelined Function
查看>>
实验七作业
查看>>
Apache shiro
查看>>
HTML5树叶飘落动画
查看>>
Flume笔记--source端监听目录,sink端上传到HDFS
查看>>
shell脚本采集系统cpu、内存、磁盘、网络信息
查看>>
使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法...
查看>>
mysql 数据操作 单表查询 limit 练习
查看>>
由于网站数据过大,导致内存不足。终于找到解决办法
查看>>
常用函数编写
查看>>