908 lines
42 KiB
C#
908 lines
42 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Data.OracleClient;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using System.Reflection;
|
||
using System.Text;
|
||
|
||
namespace EShang.Common
|
||
{
|
||
/// <summary>
|
||
/// 数据表操作类
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
public class OperationDataHelper<T>
|
||
{
|
||
#region 方法 -> 通用表数据操作
|
||
|
||
#region 通用表数据添加
|
||
/// <summary>
|
||
/// 通用表数据添加
|
||
/// Mr.Cai 2018-1-2
|
||
/// </summary>
|
||
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
|
||
/// <param name="t">实体类</param>
|
||
/// <param name="tableName">表名</param>
|
||
/// <param name="primaryKey">主键名</param>
|
||
/// <param name="nextval">主键自增SQL函数</param>
|
||
/// <returns></returns>
|
||
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 + "'";
|
||
}
|
||
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 通用表数据修改
|
||
/// <summary>
|
||
/// 通用表数据修改
|
||
/// Mr.Cai 2018-3-15
|
||
/// </summary>
|
||
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
|
||
/// <param name="listData">实体类集合</param>
|
||
/// <param name="tableName">表名</param>
|
||
/// <param name="whereName">条件名数组</param>
|
||
/// <param name="updateNull">是否更新空值字段</param>
|
||
public static void UpdateTableData(OracleHelper oracleHelper, List<T> listData, string tableName, string[] whereName, bool updateNull = false)
|
||
{
|
||
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 + "'";
|
||
for (int i = 0; i < whereName.Length; i++)
|
||
{
|
||
if (whereName[i].ToUpper().Equals(item.Name.ToUpper()))
|
||
{
|
||
whereModified += (string.IsNullOrEmpty(whereModified) ? "" : " AND ") +
|
||
item.Name + "=" + "'" + value + "'";
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (updateNull)
|
||
{
|
||
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + " = " + "NULL";
|
||
}
|
||
}
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
modifiedValues += (string.IsNullOrEmpty(modifiedValues) ? "" : ",") + item.Name + " = " + "NULL";
|
||
}
|
||
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 通用表数据添加(集合)
|
||
/// <summary>
|
||
/// 通用表数据添加(集合)
|
||
/// Mr.Cai 2018-3-9
|
||
/// </summary>
|
||
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
|
||
/// <param name="listData">实体类集合</param>
|
||
/// <param name="tableName">表名</param>
|
||
/// <param name="isPrimaryKey">是否使用自增主键(序列)</param>
|
||
/// <param name="primaryKey">主键名</param>
|
||
/// <param name="nextval">主键自增SQL函数</param>
|
||
public static void InsertTableData(OracleHelper oracleHelper, List<T> 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<string> sqlStringList = new List<string>();
|
||
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 通用表数据添加(分表)
|
||
/// <summary>
|
||
/// 通用表数据添加(分表)
|
||
/// </summary>
|
||
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
|
||
/// <param name="listData">分表实体类集合</param>
|
||
/// <param name="tableName">主表名</param>
|
||
/// <param name="isPrimaryKey">是否使用自增主键(序列)</param>
|
||
/// <param name="primaryKey">主键名</param>
|
||
/// <param name="nextval">主键自增SQL函数</param>
|
||
public static void InsertSubTableData(OracleHelper oracleHelper, Dictionary<string, List<T>> 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<string> sqlStringList = new List<string>();
|
||
string sqlString = string.Empty;
|
||
foreach (var _Key in listData.Keys)
|
||
{
|
||
|
||
foreach (T t in listData[_Key])
|
||
{
|
||
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}_{_Key}", primaryKey, names, nextval, values));
|
||
}
|
||
else
|
||
{
|
||
sqlStringList.Add(string.Format("INSERT INTO {0}({1}) VALUES({2})", $"{tableName}_{_Key}", 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数据添加(集合)
|
||
/// <summary>
|
||
/// 通用表OracleParameter数据添加(集合)
|
||
/// 注:该方式支持byte []类型数据添加,不支持Boolean、SEQ主键方式添加
|
||
/// Mr.Cai 2018-7-19
|
||
/// </summary>
|
||
/// <param name="oracleHelper">数据库SQL执行帮助类</param>
|
||
/// <param name="listData">实体类集合</param>
|
||
/// <param name="tableName">表名</param>
|
||
public static void InsertTableData_OracleParameter(OracleHelper oracleHelper, List<T> listData, string tableName)
|
||
{
|
||
if (listData == null)
|
||
{
|
||
throw new Exception("数据集合不可为空!");
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(tableName))
|
||
{
|
||
throw new Exception("表名不可为空!");
|
||
}
|
||
|
||
try
|
||
{
|
||
//需传递插入参数的集合
|
||
List<OracleHelper.OracleSQLInfo> OracleSQLInfos = new List<OracleHelper.OracleSQLInfo>();
|
||
foreach (T t in listData)
|
||
{
|
||
//插入语句及所有字段集合
|
||
OracleHelper.OracleSQLInfo oracleSQLInfo = new OracleHelper.OracleSQLInfo();
|
||
//所有字段集合
|
||
List<OracleParameter> oracleParameters = new List<OracleParameter>();
|
||
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.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
|
||
|
||
#region 获取通用表数据查询条件
|
||
/// <summary>
|
||
/// 获取通用表数据查询条件
|
||
/// </summary>
|
||
/// <param name="t">实体类</param>
|
||
/// <param name="QueryType">查询方式:0【模糊查询】,1【精确查询】</param>
|
||
/// <param name="OtherName">查询表定义名称,如A.</param>
|
||
/// <param name="ExcludeName">排除查询的字段</param>
|
||
/// <returns></returns>
|
||
public static string GetWhereSQL(T t, int? QueryType, string OtherName = "", params string[] ExcludeName)
|
||
{
|
||
string WhereSQL = "";
|
||
|
||
var pros = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
|
||
foreach (var item in pros)
|
||
{
|
||
object value = item.GetValue(t, null);
|
||
//如果排除字段中包含当前参数,则执行下一个参数查询
|
||
if (ExcludeName != null && ExcludeName.Contains(item.Name))
|
||
{
|
||
continue;
|
||
}
|
||
//查询字符串类型参数
|
||
if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(String))
|
||
{
|
||
if (value != null && !string.IsNullOrEmpty(value.ToString()))
|
||
{
|
||
if (QueryType == 2&& value.ToString().Split(',').Length>0)//选择父类IN查询
|
||
{
|
||
string[] strList = value.ToString().Split(',');
|
||
string newVaule = string.Empty;
|
||
for (int k = 0; k < strList.Length; k++)
|
||
{
|
||
newVaule += ",'" + strList[k].ToString()+"'";
|
||
}
|
||
if (newVaule.ToString().Length>0)
|
||
{
|
||
newVaule = newVaule.ToString().Substring(1);
|
||
}
|
||
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name +
|
||
(QueryType == 2 ? " in( " + newVaule.ToString() + ")" : " = '" + value + "'");
|
||
}
|
||
else
|
||
{
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name +
|
||
(QueryType == 0 ? " like '%" + value + "%'" : " = '" + value + "'");
|
||
}
|
||
|
||
}
|
||
continue;
|
||
}
|
||
//查询数字类型或bool型参数
|
||
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(Int16) || item.PropertyType == typeof(Int32) || item.PropertyType == typeof(Int64) ||
|
||
item.PropertyType == typeof(Double) || item.PropertyType == typeof(Decimal) || item.PropertyType == typeof(Boolean))
|
||
{
|
||
if (value != null)
|
||
{
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name + " = " + value;
|
||
}
|
||
continue;
|
||
}
|
||
//查询日期类型参数
|
||
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
|
||
item.PropertyType == typeof(DateTime))
|
||
{
|
||
if (value != null)
|
||
{
|
||
WhereSQL += (string.IsNullOrEmpty(WhereSQL) ? "" : " AND ") + OtherName + item.Name + " = " +
|
||
string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value);
|
||
}
|
||
continue;
|
||
}
|
||
}
|
||
|
||
return WhereSQL;
|
||
}
|
||
#endregion
|
||
|
||
#region 获取数据表执行语句(0:新增;1:修改)
|
||
/// <summary>
|
||
/// 获取数据表执行语句
|
||
/// </summary>
|
||
/// <param name="t">泛型数据对象</param>
|
||
/// <param name="ExcuteType">
|
||
/// 执行类型<br/>
|
||
/// 0:新增;1:修改
|
||
/// </param>
|
||
/// <returns></returns>
|
||
/// <param name="tableName">数据库表名称</param>
|
||
/// <param name="keyField">主键值名称</param>
|
||
/// <param name="seqName">表序列名称</param>
|
||
/// <param name="dateFieldList">日期类型的字段执行语句</param>
|
||
/// <param name="excludeField">排除执行的字段</param>
|
||
/// <param name="WhereSQL">where查询语句</param>
|
||
public static string GetTableExcuteSQL(T t, int ExcuteType, string tableName, string keyField,
|
||
string seqName, Dictionary<string, string> dateFieldList, List<string> excludeField, string WhereSQL = "")
|
||
{
|
||
string SQLString, InsertField = "", ExcuteSQL = "";
|
||
|
||
var pros = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
|
||
foreach (var item in pros)
|
||
{
|
||
//排除掉用于查询的字段,这些字段不属于表的字段
|
||
if (excludeField.Contains(item.Name))
|
||
{
|
||
continue;
|
||
}
|
||
//存储插入表中的字段名称
|
||
InsertField += (InsertField == "" ? "" : ",") + item.Name;
|
||
//获取当前对象中字段的值
|
||
object value = item.GetValue(t, null);
|
||
//判断是不是主键值
|
||
if (item.Name == keyField)
|
||
{
|
||
//只有插入语句才会更新主键值
|
||
if (ExcuteType == 0)
|
||
{
|
||
if (value != null)
|
||
{
|
||
//主键值存在则以对象中的值为准
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") + value;
|
||
}
|
||
else
|
||
{
|
||
//否则执行表的序列,获取主键值
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") + seqName + ".NEXTVAL";
|
||
}
|
||
}
|
||
continue;
|
||
}
|
||
else if (dateFieldList.ContainsKey(item.Name))
|
||
{
|
||
//判断是不是日期字段,如果是就按照日期格式的字段去执行
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") + dateFieldList[item.Name];
|
||
continue;
|
||
}
|
||
//查询字符串类型参数
|
||
if (IsType(item.PropertyType, "System.String") || item.PropertyType == typeof(String))
|
||
{
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") +
|
||
(value == null ? "NULL" : "'" + value + "'");
|
||
continue;
|
||
}
|
||
//查询数字类型或bool型参数
|
||
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(Int16) || item.PropertyType == typeof(Int32) || item.PropertyType == typeof(Int64) ||
|
||
item.PropertyType == typeof(Double) || item.PropertyType == typeof(Decimal) || item.PropertyType == typeof(Boolean))
|
||
{
|
||
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") +
|
||
(value == null ? "NULL" : value.ToString());
|
||
continue;
|
||
}
|
||
//查询日期类型参数
|
||
if (IsType(item.PropertyType, "System.Nullable`1[System.DateTime]") ||
|
||
item.PropertyType == typeof(DateTime))
|
||
{
|
||
ExcuteSQL += (string.IsNullOrEmpty(ExcuteSQL) ? "" : ",\r\n") +
|
||
(ExcuteType == 1 ? item.Name + " = " : "") +
|
||
(value == null ? "NULL" : string.Format("TO_DATE('{0}','YYYY/MM/DD HH24:MI:SS')", value));
|
||
continue;
|
||
}
|
||
}
|
||
|
||
switch (ExcuteType)
|
||
{
|
||
case 0://执行插入语句
|
||
SQLString = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", tableName, InsertField, ExcuteSQL);
|
||
break;
|
||
case 1://执行更新语句
|
||
SQLString = string.Format("UPDATE {0} SET {1}{2}", tableName, ExcuteSQL, WhereSQL);
|
||
break;
|
||
default:
|
||
SQLString = "";
|
||
break;
|
||
}
|
||
|
||
return SQLString;
|
||
}
|
||
#endregion
|
||
|
||
#endregion
|
||
|
||
#region 方法 -> 类型匹配
|
||
/// <summary>
|
||
/// 类型匹配
|
||
/// </summary>
|
||
/// <param name="type">类型</param>
|
||
/// <param name="typeName">类型名称</param>
|
||
/// <returns></returns>
|
||
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 值匹配
|
||
/// <summary>
|
||
/// Oracle数据库参数值匹配
|
||
/// </summary>
|
||
/// <param name="type">指定的字段或属性中使用的数据类型</param>
|
||
/// <param name="name">字段名称</param>
|
||
/// <param name="value">字段值</param>
|
||
/// <returns></returns>
|
||
public static OracleParameter GetOracleParameter(OracleType type, string name, object value)
|
||
{
|
||
OracleParameter oracleParameter = new OracleParameter(name, type);
|
||
oracleParameter.Value = value;
|
||
return oracleParameter;
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 按照某个字段排序
|
||
/// </summary>
|
||
/// <param name="list">数据集合</param>
|
||
/// <param name="sortfield">需要排序的字段</param>
|
||
/// <param name="IsDesc">倒序还是顺序</param>
|
||
/// <returns></returns>
|
||
public static List<T> OrderByField(List<T> list, string sortfield, bool IsDesc)
|
||
{
|
||
|
||
var Queryable = list.AsQueryable();
|
||
|
||
var p = Expression.Parameter(typeof(T), "p");
|
||
var x = Expression.Lambda(Expression.Property(p, sortfield), p);
|
||
|
||
return Queryable.Provider.CreateQuery<T>(
|
||
Expression.Call(typeof(Queryable),
|
||
IsDesc ? "OrderByDescending" : "OrderBy",
|
||
new Type[] { Queryable.ElementType, x.Body.Type },
|
||
Queryable.Expression,
|
||
x)).ToList();
|
||
}
|
||
}
|
||
}
|