SqlParameter.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // System.Data.SqlClient.SqlParameter
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Data;
  33. using System.Data.ProviderBase;
  34. using System.Data.Common;
  35. using java.sql;
  36. namespace System.Data.SqlClient
  37. {
  38. public class SqlParameter : AbstractDbParameter, IDbDataParameter, ICloneable
  39. {
  40. #region Fields
  41. private SqlDbType _sqlDbType;
  42. #endregion // Fields
  43. #region Constructors
  44. public SqlParameter()
  45. {
  46. }
  47. public SqlParameter(String parameterName, Object value)
  48. : this(parameterName, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, value,false)
  49. {
  50. }
  51. public SqlParameter(String parameterName, SqlDbType dbType)
  52. : this(parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null, true)
  53. {
  54. }
  55. public SqlParameter(String parameterName, SqlDbType dbType, int size)
  56. : this(parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null, true)
  57. {
  58. }
  59. public SqlParameter(String parameterName, SqlDbType dbType, int size, String sourceColumn)
  60. : this(parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null, true)
  61. {
  62. }
  63. public SqlParameter(
  64. String parameterName,
  65. SqlDbType dbType,
  66. int size,
  67. ParameterDirection direction,
  68. bool isNullable,
  69. byte precision,
  70. byte scale,
  71. String sourceColumn,
  72. DataRowVersion sourceVersion,
  73. Object value) : this(parameterName,dbType,size,direction,isNullable,precision,scale,sourceColumn,sourceVersion,value,true)
  74. {
  75. }
  76. public SqlParameter(
  77. String parameterName,
  78. SqlDbType dbType,
  79. int size,
  80. ParameterDirection direction,
  81. bool isNullable,
  82. byte precision,
  83. byte scale,
  84. String sourceColumn,
  85. DataRowVersion sourceVersion,
  86. Object value,
  87. bool dbTypeExplicit)
  88. {
  89. ParameterName = parameterName;
  90. SqlDbType = dbType;
  91. Size = size;
  92. Direction = direction;
  93. IsNullable = isNullable;
  94. Precision = precision;
  95. Scale = scale;
  96. SourceColumn = sourceColumn;
  97. SourceVersion = sourceVersion;
  98. if (!dbTypeExplicit) {
  99. _isDbTypeSet = false;
  100. }
  101. Value = value;
  102. }
  103. #endregion // Constructors
  104. #region Properties
  105. public override DbType DbType
  106. {
  107. get { return SqlConvert.SqlDbTypeToDbType(_sqlDbType); }
  108. set { SqlDbType = SqlConvert.DbTypeToSqlDbType(value); }
  109. }
  110. public SqlDbType SqlDbType
  111. {
  112. get { return _sqlDbType; }
  113. set {
  114. _sqlDbType = value;
  115. _isDbTypeSet = true;
  116. }
  117. }
  118. public override byte Precision
  119. {
  120. get { return _precision; }
  121. set { _precision = value; }
  122. }
  123. public override int Size
  124. {
  125. get {
  126. int retVal = base.Size;
  127. return retVal;
  128. }
  129. set {
  130. if (value < 0) {
  131. throw ExceptionHelper.InvalidSizeValue(value);
  132. }
  133. if (value != 0) {
  134. base.Size = value;
  135. }
  136. else {
  137. base.Size = -1;
  138. }
  139. }
  140. }
  141. protected internal override string Placeholder {
  142. get {
  143. return ParameterName;
  144. }
  145. }
  146. public override Object Value
  147. {
  148. get { return base.Value; }
  149. set {
  150. if (!_isDbTypeSet && (value != null) && (value != DBNull.Value)) {
  151. _sqlDbType = SqlConvert.ValueTypeToSqlDbType(value.GetType());
  152. }
  153. base.Value = value;
  154. }
  155. }
  156. #endregion // Properties
  157. #region Methods
  158. public override String ToString()
  159. {
  160. return ParameterName;
  161. }
  162. public override object Clone()
  163. {
  164. SqlParameter clone = new SqlParameter();
  165. CopyTo(clone);
  166. clone._sqlDbType = _sqlDbType;
  167. return clone;
  168. }
  169. protected internal sealed override object ConvertValue(object value)
  170. {
  171. // can not convert null or DbNull to other types
  172. if (value == null || value == DBNull.Value) {
  173. return value;
  174. }
  175. // .NET throws an exception to the user.
  176. object convertedValue = Convert.ChangeType(value,SqlConvert.SqlDbTypeToValueType(SqlDbType));
  177. return convertedValue;
  178. }
  179. protected internal sealed override void SetParameterName(ResultSet res)
  180. {
  181. string name = res.getString("COLUMN_NAME");
  182. if (name != null && name.Length > 0 && name[0] != '@')
  183. name = String.Concat("@", name);
  184. ParameterName = name;
  185. }
  186. protected internal sealed override void SetParameterDbType(ResultSet res)
  187. {
  188. int dataType = res.getInt("DATA_TYPE");
  189. SqlDbType = SqlConvert.JdbcTypeToSqlDbType(dataType);
  190. JdbcType = (DbTypes.JavaSqlTypes) dataType;
  191. }
  192. protected internal sealed override void SetSpecialFeatures(ResultSet res)
  193. {
  194. // do nothing
  195. }
  196. protected internal sealed override DbTypes.JavaSqlTypes JdbcTypeFromProviderType()
  197. {
  198. return (DbTypes.JavaSqlTypes)SqlConvert.SqlDbTypeToJdbcType(SqlDbType);
  199. }
  200. #endregion // Methods
  201. }
  202. }