using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.OracleClient; using System.Linq; using System.Reflection; using System.Text; namespace Transmission.SDK { public class OperationDataHelper { #region 方法 -> 通用表数据操作 #region 通用表数据添加 /// /// 通用表数据添加 /// Mr.Cai 2018-1-2 /// /// 数据库SQL执行帮助类 /// 实体类 /// 表名 /// 主键名 /// 主键自增SQL函数 /// public static void InsertTableData(OracleHelper oracleHelper, T t, string tableName, string primaryKey, string nextval) { if (string.IsNullOrEmpty(tableName)) { throw new Exception("表名不可为空!"); } if (string.IsNullOrEmpty(primaryKey)) { throw new Exception("表名主键名不可为空!"); } if (string.IsNullOrEmpty(nextval)) { throw new Exception("序列不可为空!"); } try { var pros = typeof(T).GetProperties(); string names = string.Empty; string values = string.Empty; foreach (var item in pros) { //排除主键字段 if (item.Name.ToUpper() != primaryKey.ToUpper() && item.Name.ToUpper() != (primaryKey + "_Encrypt").ToUpper() && item.Name.ToUpper() != "KEYID") { object value = item.GetValue(t, null); if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(System.String)) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value.ToString().Replace("'", " ") + "'"; } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") || IsType(item.PropertyType, "System.Nullable`1[System.Int32]") || IsType(item.PropertyType, "System.Nullable`1[System.Int64]") || IsType(item.PropertyType, "System.Nullable`1[System.Double]") || IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") || IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") || item.PropertyType == typeof(System.Int16) || item.PropertyType == typeof(System.Int32) || item.PropertyType == typeof(System.Int64) || item.PropertyType == typeof(System.Double) || item.PropertyType == typeof(System.Decimal) || item.PropertyType == typeof(System.Boolean)) { if (value != null) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + value; } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") || item.PropertyType == typeof(System.DateTime)) { if (value != null) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value); } else { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", DateTime.Now.ToString()); } continue; } } } string _SqlString = string.Format("INSERT INTO {0}({1},{2}) VALUES({3},{4})", tableName, primaryKey, names, nextval, values); oracleHelper.ExcuteSql(_SqlString); } catch (Exception ex) { throw ex; } } #endregion #region 通用表数据修改 /// /// 通用表数据修改 /// Mr.Cai 2018-3-15 /// /// 数据库SQL执行帮助类 /// 实体类集合 /// 表名 /// 条件名数组 public static void UpdateTableData(OracleHelper oracleHelper, List listData, string tableName, string[] whereName) { if (listData == null) { throw new Exception("数据集合不可为空!"); } if (whereName == null) { throw new Exception("修改条件名不可为空!"); } if (string.IsNullOrEmpty(tableName)) { throw new Exception("表名不可为空!"); } try { string[] sqlString = new string[listData.Count]; int index = 0;//索引 foreach (T t in listData) { var pros = typeof(T).GetProperties(); string modifiedValues = string.Empty; string whereModified = string.Empty; foreach (var item in pros) { // isPrimaryKey = true 排除主键字段 if (item.Name.ToUpper().Contains(("_Encrypt").ToUpper()) || item.Name.ToUpper() == "KEYID") { continue; } object value = item.GetValue(t, null); if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(System.String)) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + "=" + "'" + value.ToString().Replace("'", " ") + "'"; for (int i = 0; i < whereName.Length; i++) { if (whereName[i].ToUpper().Equals(item.Name.ToUpper())) { whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") + item.Name + "=" + "'" + value.ToString().Replace("'", " ") + "'"; } } } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") || IsType(item.PropertyType, "System.Nullable`1[System.Int32]") || IsType(item.PropertyType, "System.Nullable`1[System.Int64]") || IsType(item.PropertyType, "System.Nullable`1[System.Double]") || IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") || IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") || item.PropertyType == typeof(System.Int16) || item.PropertyType == typeof(System.Int32) || item.PropertyType == typeof(System.Int64) || item.PropertyType == typeof(System.Double) || item.PropertyType == typeof(System.Decimal) || item.PropertyType == typeof(System.Boolean)) { if (value != null) { modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + "=" + value; for (int i = 0; i < whereName.Length; i++) { if (whereName[i].ToUpper().Equals(item.Name.ToUpper())) { whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") + item.Name + "=" + value; } } } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") || item.PropertyType == typeof(System.DateTime)) { if (value != null) { modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + "=" + string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value); for (int i = 0; i < whereName.Length; i++) { if (whereName[i].ToUpper().Equals(item.Name.ToUpper())) { whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") + item.Name + "=" + string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value); } } } continue; } } sqlString[index] += string.Format("UPDATE {0} SET {1} WHERE {2}", tableName, modifiedValues, whereModified); index++; } oracleHelper.ExecuteSqlTran(sqlString); } catch (Exception ex) { //LogHelper.WriteServiceLog(sqlString[0]); throw ex; } } #endregion #region 通用表数据添加(集合) /// /// 通用表数据添加(集合) /// Mr.Cai 2018-3-9 /// /// 数据库SQL执行帮助类 /// 实体类集合 /// 表名 /// 是否使用自增主键(序列) /// 主键名 /// 主键自增SQL函数 public static void InsertTableData(OracleHelper oracleHelper, List listData, string tableName, bool isPrimaryKey = false, string primaryKey = "", string nextval = "") { if (listData == null) { throw new Exception("数据集合不可为空!"); } if (string.IsNullOrEmpty(tableName)) { throw new Exception("表名不可为空!"); } if (isPrimaryKey)//不插入原来主键,使用自增主键(序列),则判断必要条件 { if (string.IsNullOrEmpty(primaryKey)) { throw new Exception("表名主键名不可为空!"); } if (string.IsNullOrEmpty(nextval)) { throw new Exception("序列不可为空!"); } } try { List sqlStringList = new List(); string sqlString = string.Empty; foreach (T t in listData) { var pros = typeof(T).GetProperties(); string names = string.Empty; string values = string.Empty; foreach (var item in pros) { // isPrimaryKey = true 排除主键字段 if ((isPrimaryKey && item.Name.ToUpper() == primaryKey.ToUpper()) || item.Name.ToUpper().Contains((primaryKey + "_Encrypt").ToUpper()) || item.Name.ToUpper() == "KEYID") { continue; } object value = item.GetValue(t, null); if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(System.String)) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value.ToString().Replace("'", " ") + "'"; } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") || IsType(item.PropertyType, "System.Nullable`1[System.Int32]") || IsType(item.PropertyType, "System.Nullable`1[System.Int64]") || IsType(item.PropertyType, "System.Nullable`1[System.Double]") || IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") || IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") || item.PropertyType == typeof(System.Int16) || item.PropertyType == typeof(System.Int32) || item.PropertyType == typeof(System.Int64) || item.PropertyType == typeof(System.Double) || item.PropertyType == typeof(System.Decimal) || item.PropertyType == typeof(System.Boolean)) { if (value != null) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + value; } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") || item.PropertyType == typeof(System.DateTime)) { if (value != null) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value); } continue; } } if (isPrimaryKey) { sqlStringList.Add(string.Format("INSERT INTO {0}({1},{2}) VALUES({3},{4})", tableName, primaryKey, names, nextval, values)); } else { sqlStringList.Add(string.Format("INSERT INTO {0}({1}) VALUES({2})", tableName, names, values)); } } if (sqlStringList != null && sqlStringList.Count > 0) { oracleHelper.ExecuteSqlTran(sqlStringList); } else { throw new Exception("SQL语句不可为空!"); } } catch (Exception ex) { //LogHelper.WriteSendLog(ex.Message); throw ex; } } #endregion #region 通用表OracleParameter数据添加(集合) /// /// 通用表OracleParameter数据添加(集合) /// 注:该方式支持byte []类型数据添加,不支持Boolean、SEQ主键方式添加 /// Mr.Cai 2018-7-19 /// /// 数据库SQL执行帮助类 /// 实体类集合 /// 表名 /// 是否使用自增主键(序列) /// 主键名 /// 主键自增SQL函数 public static void InsertTableData_OracleParameter(OracleHelper oracleHelper, List listData, string tableName) { if (listData == null) { throw new Exception("数据集合不可为空!"); } if (string.IsNullOrEmpty(tableName)) { throw new Exception("表名不可为空!"); } try { //需传递插入参数的集合 List OracleSQLInfos = new List(); foreach (T t in listData) { //插入语句及所有字段集合 OracleHelper.OracleSQLInfo oracleSQLInfo = new OracleHelper.OracleSQLInfo(); //所有字段集合 List oracleParameters = new List(); string sqlString = "INSERT INTO " + tableName + " ({0}) VALUES ({1})"; var pros = typeof(T).GetProperties(); string names = string.Empty; string values = string.Empty; foreach (var item in pros) { object value = item.GetValue(t, null); if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(System.String)) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name; oracleParameters.Add(GetOracleParameter(OracleType.VarChar, item.Name, value)); } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.Int16]") || IsType(item.PropertyType, "System.Nullable`1[System.Int32]") || IsType(item.PropertyType, "System.Nullable`1[System.Int64]") || IsType(item.PropertyType, "System.Nullable`1[System.Double]") || IsType(item.PropertyType, "System.Nullable`1[System.Decimal]") || item.PropertyType == typeof(System.Int16) || item.PropertyType == typeof(System.Int32) || item.PropertyType == typeof(System.Int64) || item.PropertyType == typeof(System.Double) || item.PropertyType == typeof(System.Decimal)) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name; oracleParameters.Add(GetOracleParameter(OracleType.Number, item.Name, value)); } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System.Boolean]") || item.PropertyType == typeof(System.Boolean)) { throw new Exception("ORACLE数据 不支持 Boolean,请使用Number 类型!"); } if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") || item.PropertyType == typeof(System.DateTime)) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name; oracleParameters.Add(GetOracleParameter(OracleType.DateTime, item.Name, value)); } continue; } if (IsType(item.PropertyType, "System.Nullable`1[System..Byte[]]") || item.PropertyType == typeof(System.Byte[])) { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; values += (string.IsNullOrEmpty(values) ? "" : ",") + ":" + item.Name; oracleParameters.Add(GetOracleParameter(OracleType.Blob, item.Name, value)); } continue; } } if (!string.IsNullOrEmpty(names) && !string.IsNullOrEmpty(values)) { oracleSQLInfo.sqlString = string.Format(sqlString, names, values); oracleSQLInfo.sqlStringArray = oracleParameters; OracleSQLInfos.Add(oracleSQLInfo); } else { throw new Exception("不可存在所有值为空的SQL数据!"); } } if (OracleSQLInfos != null && OracleSQLInfos.Count > 0) { oracleHelper.ExecuteSqlTran(OracleSQLInfos); } else { throw new Exception("SQL语句集合不可为空!"); } } catch (Exception ex) { //LogHelper.WriteSendLog(ex.Message); throw ex; } } #endregion #endregion #region 方法 -> 类型匹配 /// /// 类型匹配 /// /// 类型 /// 类型名称 /// public static bool IsType(Type type, string typeName) { if (type.ToString() == typeName) return true; if (type.ToString() == "System.Object") return false; return IsType(type.BaseType, typeName); } #endregion #region OracleParameter 值匹配 public static OracleParameter GetOracleParameter(OracleType type, string name, object value) { OracleParameter oracleParameter = new OracleParameter(name, type); oracleParameter.Value = value; return oracleParameter; } #endregion } }