You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
5.0 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. namespace SqlSugar
  6. {
  7. public class SugarParameter : DbParameter
  8. {
  9. public bool IsRefCursor { get; set; }
  10. public SugarParameter(string name, object value)
  11. {
  12. this.Value = value;
  13. this.ParameterName = name;
  14. if (value != null)
  15. {
  16. SettingDataType(value.GetType());
  17. }
  18. }
  19. public SugarParameter(string name, object value, Type type)
  20. {
  21. this.Value = value;
  22. this.ParameterName = name;
  23. SettingDataType(type);
  24. }
  25. public SugarParameter(string name, object value, Type type,ParameterDirection direction)
  26. {
  27. this.Value = value;
  28. this.ParameterName = name;
  29. this.Direction = direction;
  30. SettingDataType(type);
  31. }
  32. public SugarParameter(string name, object value, Type type, ParameterDirection direction,int size)
  33. {
  34. this.Value = value;
  35. this.ParameterName = name;
  36. this.Direction = direction;
  37. this.Size = size;
  38. SettingDataType(type);
  39. }
  40. private void SettingDataType(Type type)
  41. {
  42. if (type == UtilConstants.ByteArrayType)
  43. {
  44. this.DbType = System.Data.DbType.Binary;
  45. }
  46. else if (type == UtilConstants.GuidType)
  47. {
  48. this.DbType = System.Data.DbType.Guid;
  49. }
  50. else if (type == UtilConstants.IntType)
  51. {
  52. this.DbType = System.Data.DbType.Int32;
  53. }
  54. else if (type == UtilConstants.ShortType)
  55. {
  56. this.DbType = System.Data.DbType.Int16;
  57. }
  58. else if (type == UtilConstants.LongType)
  59. {
  60. this.DbType = System.Data.DbType.Int64;
  61. }
  62. else if (type == UtilConstants.DateType)
  63. {
  64. this.DbType = System.Data.DbType.DateTime;
  65. }
  66. else if (type == UtilConstants.DobType)
  67. {
  68. this.DbType = System.Data.DbType.Double;
  69. }
  70. else if (type == UtilConstants.DecType)
  71. {
  72. this.DbType = System.Data.DbType.Decimal;
  73. }
  74. else if (type == UtilConstants.ByteType)
  75. {
  76. this.DbType = System.Data.DbType.Byte;
  77. }
  78. else if (type == UtilConstants.FloatType)
  79. {
  80. this.DbType = System.Data.DbType.Single;
  81. }
  82. else if (type == UtilConstants.BoolType)
  83. {
  84. this.DbType = System.Data.DbType.Boolean;
  85. }
  86. else if (type == UtilConstants.StringType)
  87. {
  88. this.DbType = System.Data.DbType.String;
  89. }
  90. }
  91. public SugarParameter(string name, object value, bool isOutput)
  92. {
  93. this.Value = value;
  94. this.ParameterName = name;
  95. if (isOutput)
  96. {
  97. this.Direction = ParameterDirection.Output;
  98. }
  99. }
  100. public override System.Data.DbType DbType
  101. {
  102. get; set;
  103. }
  104. public override ParameterDirection Direction
  105. {
  106. get; set;
  107. }
  108. public override bool IsNullable
  109. {
  110. get; set;
  111. }
  112. public override string ParameterName
  113. {
  114. get; set;
  115. }
  116. public int _Size;
  117. public override int Size
  118. {
  119. get
  120. {
  121. if (_Size == 0 && Value != null)
  122. {
  123. var isByteArray = Value.GetType() == UtilConstants.ByteArrayType;
  124. if (isByteArray)
  125. _Size = -1;
  126. else
  127. {
  128. var length = Value.ToString().Length;
  129. _Size = length < 4000 ? 4000 : -1;
  130. }
  131. }
  132. if (_Size == 0)
  133. _Size = 4000;
  134. return _Size;
  135. }
  136. set
  137. {
  138. _Size = value;
  139. }
  140. }
  141. public override string SourceColumn
  142. {
  143. get; set;
  144. }
  145. public override bool SourceColumnNullMapping
  146. {
  147. get; set;
  148. }
  149. public string UdtTypeName
  150. {
  151. get;
  152. set;
  153. }
  154. public override object Value
  155. {
  156. get; set;
  157. }
  158. public Dictionary<string, object> TempDate
  159. {
  160. get; set;
  161. }
  162. /// <summary>
  163. /// 如果类库是.NET 4.5请删除该属性
  164. /// If the SqlSugar library is.NET 4.5, delete the property
  165. /// </summary>
  166. public override DataRowVersion SourceVersion
  167. {
  168. get; set;
  169. }
  170. public override void ResetDbType()
  171. {
  172. this.DbType = System.Data.DbType.String;
  173. }
  174. }
  175. }