using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; namespace SqlSugar { public partial class AdoAccessory { protected IDbBind _DbBind; protected IDbFirst _DbFirst; protected ICodeFirst _CodeFirst; protected IDbMaintenance _DbMaintenance; protected IDbConnection _DbConnection; protected virtual SugarParameter[] GetParameters(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord) { var result = new List(); if (parameters != null) { var entityType = parameters.GetType(); var isDictionary = entityType.IsIn(UtilConstants.DicArraySO, UtilConstants.DicArraySS); if (isDictionary) DictionaryToParameters(parameters, sqlParameterKeyWord, result, entityType); else if (parameters is List) { result = (parameters as List); } else if (parameters is SugarParameter[]) { result = (parameters as SugarParameter[]).ToList(); } else { Check.Exception(!entityType.IsAnonymousType(), "The parameter format is wrong. \nUse new{{xx=xx, xx2=xx2}} or \nDictionary or \nSugarParameter [] "); ProperyToParameter(parameters, propertyInfo, sqlParameterKeyWord, result, entityType); } } return result.ToArray(); } protected void ProperyToParameter(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord, List listParams, Type entityType) { PropertyInfo[] properties = null; properties = propertyInfo != null ? propertyInfo : entityType.GetProperties(); foreach (PropertyInfo properyty in properties) { var value = properyty.GetValue(parameters, null); if (properyty.PropertyType.IsEnum()) value = Convert.ToInt64(value); if (value == null || value.Equals(DateTime.MinValue)) value = DBNull.Value; if (properyty.Name.ToLower().Contains("hierarchyid")) { var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, SqlDbType.Udt) { UdtTypeName = "HIERARCHYID", Value = value }; listParams.Add(parameter); } else { var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, value); listParams.Add(parameter); } } } protected void DictionaryToParameters(object parameters, string sqlParameterKeyWord, List listParams, Type entityType) { if (entityType == UtilConstants.DicArraySO) { var dictionaryParameters = (Dictionary)parameters; var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value)); listParams.AddRange(sugarParameters); } else { var dictionaryParameters = (Dictionary)parameters; var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value)); listParams.AddRange(sugarParameters); } } } }