AdoAccessory.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. namespace SqlSugar
  9. {
  10. public partial class AdoAccessory
  11. {
  12. protected IDbBind _DbBind;
  13. protected IDbFirst _DbFirst;
  14. protected ICodeFirst _CodeFirst;
  15. protected IDbMaintenance _DbMaintenance;
  16. protected IDbConnection _DbConnection;
  17. protected virtual SugarParameter[] GetParameters(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord)
  18. {
  19. List<SugarParameter> result = new List<SugarParameter>();
  20. if (parameters != null)
  21. {
  22. var entityType = parameters.GetType();
  23. var isDictionary = entityType.IsIn(UtilConstants.DicArraySO, UtilConstants.DicArraySS);
  24. if (isDictionary)
  25. DictionaryToParameters(parameters, sqlParameterKeyWord, result, entityType);
  26. else if (parameters is List<SugarParameter>)
  27. {
  28. result = (parameters as List<SugarParameter>);
  29. }
  30. else if (parameters is SugarParameter[])
  31. {
  32. result = (parameters as SugarParameter[]).ToList();
  33. }
  34. else
  35. {
  36. Check.Exception(!entityType.IsAnonymousType(), "The parameter format is wrong. \nUse new{{xx=xx, xx2=xx2}} or \nDictionary<string, object> or \nSugarParameter [] ");
  37. ProperyToParameter(parameters, propertyInfo, sqlParameterKeyWord, result, entityType);
  38. }
  39. }
  40. return result.ToArray();
  41. }
  42. protected void ProperyToParameter(object parameters, PropertyInfo[] propertyInfo, string sqlParameterKeyWord, List<SugarParameter> listParams, Type entityType)
  43. {
  44. PropertyInfo[] properties = null;
  45. if (propertyInfo != null)
  46. properties = propertyInfo;
  47. else
  48. properties = entityType.GetProperties();
  49. foreach (PropertyInfo properyty in properties)
  50. {
  51. var value = properyty.GetValue(parameters, null);
  52. if (properyty.PropertyType.IsEnum())
  53. value = Convert.ToInt64(value);
  54. if (value == null || value.Equals(DateTime.MinValue)) value = DBNull.Value;
  55. if (properyty.Name.ToLower().Contains("hierarchyid"))
  56. {
  57. var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, SqlDbType.Udt);
  58. parameter.UdtTypeName = "HIERARCHYID";
  59. parameter.Value = value;
  60. listParams.Add(parameter);
  61. }
  62. else
  63. {
  64. var parameter = new SugarParameter(sqlParameterKeyWord + properyty.Name, value);
  65. listParams.Add(parameter);
  66. }
  67. }
  68. }
  69. protected void DictionaryToParameters(object parameters, string sqlParameterKeyWord, List<SugarParameter> listParams, Type entityType)
  70. {
  71. if (entityType == UtilConstants.DicArraySO)
  72. {
  73. var dictionaryParameters = (Dictionary<string, object>)parameters;
  74. var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value));
  75. listParams.AddRange(sugarParameters);
  76. }
  77. else
  78. {
  79. var dictionaryParameters = (Dictionary<string, string>)parameters;
  80. var sugarParameters = dictionaryParameters.Select(it => new SugarParameter(sqlParameterKeyWord + it.Key, it.Value));
  81. listParams.AddRange(sugarParameters); ;
  82. }
  83. }
  84. }
  85. }