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 OperatingData.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(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); 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(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); 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 + "'"; } } } 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) { throw ex; } } #endregion #region 通用表数据添加(集合) /// /// 通用表数据添加(集合) /// 朱梓毅 2021-8-3 重新整理 /// /// 数据库SQL执行帮助类 /// 实体类集合 /// 表名 public static void InsertTableData(OracleHelper oracleHelper, List listData, string tableName) { 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) { //获取字段对应的值 object value = item.GetValue(t, null); //如果是字符串类型的字段,获取拼接SQL语句需要的字段名和值 if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(System.String) || item.Name.ToLower() == "item") { if (value != null && !string.IsNullOrEmpty(value.ToString())) { names += (string.IsNullOrEmpty(names) ? "" : ",") + item.Name; //此处要注意如果值里面包含了单引号,需要将其转化成两个单引号 values += (string.IsNullOrEmpty(values) ? "" : ",") + "'" + value.ToString().Replace("'", "''") + "'"; } continue; } //如果是数值或者bool类型的字段,获取拼接SQL语句需要的字段名和值 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; } //如果是数值或者bool类型的字段,获取拼接SQL语句需要的字段名和值 //值的格式为TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS'),{0}用value填充 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; } } //如果主键以数据包中的键值为准,则执行如下代码 sqlStringList.Add(string.Format("INSERT INTO {0}({1}) VALUES({2})", tableName, names, values)); } if (sqlStringList != null && sqlStringList.Count > 0) { //批量执行SQL语句 oracleHelper.ExecuteSqlTran(sqlStringList); } else { throw new Exception("SQL语句不可为空!"); } } catch (Exception ex) { //LogHelper.WriteSendLog(ex.Message); throw ex; } } #endregion #region 通用表数据添加(集合) /// /// 通用表数据添加(集合) /// Mr.Cai 2018-3-9 /// /// 数据库SQL执行帮助类 /// 实体类集合 /// 表名 /// 是否使用自增主键(序列) /// 主键名 /// 主键自增SQL函数 public static void InsertTableData(OracleHelper oracleHelper, List listData, string tableName, bool isPrimaryKey, 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 排除主键字段,primaryKey为主键值,加上_Encrypt为加密字段,KEYID也是主键值字段 if ((isPrimaryKey && item.Name.ToUpper() == primaryKey.ToUpper()) || item.Name.ToUpper().Contains((primaryKey + "_Encrypt").ToUpper()) || item.Name.ToUpper() == "KEYID" || item.Name.ToLower() == "item") { continue; } //获取字段对应的值 object value = item.GetValue(t, null); //如果是字符串类型的字段,获取拼接SQL语句需要的字段名和值 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; } //如果是数值或者bool类型的字段,获取拼接SQL语句需要的字段名和值 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; } //如果是数值或者bool类型的字段,获取拼接SQL语句需要的字段名和值 //值的格式为TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS'),{0}用value填充 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) { //批量执行SQL语句 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) || item.Name.ToLower() == "item") { 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.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]") || 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 方法 -> 判断授权码是否正确 /// /// 判断授权码是否正确 /// /// 数据库SQL执行帮助类 /// 授权码 /// public static string[] IsCorrectCode(OracleHelper _OracleHelper, string code) { if (string.IsNullOrEmpty(code)) { return null; } try { string[] codes = code.ToDecrypt().Split('|'); string _SERVERPARTCODE = codes[0]; string _SHOPCODE = codes[1]; string _ENDDATE = codes.Length > 2 ? codes[2] : ""; string _MACADDRESS = codes.Length > 3 ? codes[3] : ""; //LogHelper.WriteReceiveLog(code.ToDecrypt()); if (_SERVERPARTCODE == "888888" || string.IsNullOrEmpty(_ENDDATE) || DateTime.Now <= DateTime.Parse(_ENDDATE).AddDays(1)) { if (!string.IsNullOrEmpty(_SERVERPARTCODE)) { string _SqlString = string.Format( "SELECT * FROM HIGHWAY_EXCHANGE.T_AUTHORIZATION WHERE AUTHORIZATION_CODE = '{0}' {1}{2}", code, " AND SERVERPARTCODE = '" + _SERVERPARTCODE + "'", string.IsNullOrEmpty(_MACADDRESS) ? "" : " AND MACHINE_MACADDRESS = '" + _MACADDRESS + "'"); DataSet dt = _OracleHelper.ExcuteSqlGetDataSet(_SqlString); if (dt.Tables[0].Rows.Count > 0) { return codes; } } } } catch (Exception ex) { WebService.SDK.LogHelper.WriteSendLog(ex.Message); } return null; } #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 } }