SqlParameter.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //
  2. // System.Data.SqlClient.SqlParameter.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc. 2002
  10. // Copyright (C) Tim Coleman, 2002
  11. //
  12. using System;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Data.Common;
  16. using System.Runtime.InteropServices;
  17. using System.Text;
  18. namespace System.Data.SqlClient {
  19. /// <summary>
  20. /// Represents a parameter to a Command object, and optionally,
  21. /// its mapping to DataSet columns; and is implemented by .NET
  22. /// data providers that access data sources.
  23. /// </summary>
  24. public sealed class SqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  25. {
  26. #region Fields
  27. string parmName;
  28. SqlDbType dbtype;
  29. DbType theDbType;
  30. object objValue;
  31. int size;
  32. string sourceColumn;
  33. ParameterDirection direction = ParameterDirection.Input;
  34. bool isNullable;
  35. byte precision;
  36. byte scale;
  37. DataRowVersion sourceVersion;
  38. int offset;
  39. bool sizeSet = false;
  40. #endregion // Fields
  41. #region Constructors
  42. public SqlParameter ()
  43. : this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  44. {
  45. }
  46. public SqlParameter (string parameterName, object value)
  47. : this (parameterName, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, value)
  48. {
  49. }
  50. public SqlParameter (string parameterName, SqlDbType dbType)
  51. : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  52. {
  53. }
  54. public SqlParameter (string parameterName, SqlDbType dbType, int size)
  55. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  56. {
  57. }
  58. public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn)
  59. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  60. {
  61. }
  62. [EditorBrowsable (EditorBrowsableState.Advanced)]
  63. public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  64. {
  65. this.parmName = parameterName;
  66. this.dbtype = dbType;
  67. this.size = size;
  68. this.sourceColumn = sourceColumn;
  69. this.direction = direction;
  70. this.isNullable = isNullable;
  71. this.precision = precision;
  72. this.scale = scale;
  73. this.sourceVersion = sourceVersion;
  74. this.objValue = value;
  75. }
  76. #endregion // Constructors
  77. #region Properties
  78. [Browsable (false)]
  79. [DataSysDescription ("The parameter generic type.")]
  80. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  81. //[RefreshProperties (RefreshProperties.All)]
  82. public DbType DbType {
  83. get { return theDbType; }
  84. set { theDbType = value; }
  85. }
  86. [DataSysDescription ("Input, output, or bidirectional parameter.")]
  87. [DefaultValue (ParameterDirection.Input)]
  88. public ParameterDirection Direction {
  89. get { return direction; }
  90. set { direction = value; }
  91. }
  92. [Browsable (false)]
  93. [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
  94. [DefaultValue (false)]
  95. [DesignOnly (true)]
  96. [EditorBrowsable (EditorBrowsableState.Advanced)]
  97. public bool IsNullable {
  98. get { return isNullable; }
  99. }
  100. [Browsable (false)]
  101. [DataSysDescription ("Offset in variable length data types.")]
  102. [DefaultValue (0)]
  103. public int Offset {
  104. get { return offset; }
  105. set { offset = value; }
  106. }
  107. string IDataParameter.ParameterName {
  108. get { return parmName; }
  109. set { parmName = value; }
  110. }
  111. [DataSysDescription ("Name of the parameter, like '@p1'")]
  112. [DefaultValue ("")]
  113. public string ParameterName {
  114. get { return parmName; }
  115. set { parmName = value; }
  116. }
  117. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  118. [DefaultValue (0)]
  119. public byte Precision {
  120. get { return precision; }
  121. set { precision = value; }
  122. }
  123. [DataSysDescription ("When used by a DataAdapter.Update, the source column name that is used to find the DataSetColumn name in the ColumnMappings. This is to copy a value between the parameter and a datarow.")]
  124. [DefaultValue ("")]
  125. public string SourceColumn {
  126. get { return sourceColumn; }
  127. set { sourceColumn = value; }
  128. }
  129. [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
  130. [DefaultValue (DataRowVersion.Current)]
  131. public DataRowVersion SourceVersion {
  132. get { return sourceVersion; }
  133. set { sourceVersion = value; }
  134. }
  135. [DataSysDescription ("The parameter native type.")]
  136. [DefaultValue (SqlDbType.NVarChar)]
  137. //[RefreshProperties (RefreshProperties.All)]
  138. public SqlDbType SqlDbType {
  139. get { return dbtype; }
  140. set { dbtype = value; }
  141. }
  142. [DataSysDescription ("Value of the parameter.")]
  143. [DefaultValue (null)]
  144. public object Value {
  145. get { return objValue; }
  146. set { objValue = value; }
  147. }
  148. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  149. [DefaultValue (0)]
  150. public byte Scale {
  151. get { return scale; }
  152. set { scale = value; }
  153. }
  154. [DataSysDescription ("Size of variable length datatypes (strings & arrays).")]
  155. [DefaultValue (0)]
  156. public int Size {
  157. get { return size; }
  158. set {
  159. sizeSet = true;
  160. size = value;
  161. }
  162. }
  163. #endregion // Properties
  164. #region Methods
  165. [MonoTODO]
  166. object ICloneable.Clone ()
  167. {
  168. throw new NotImplementedException ();
  169. }
  170. internal string Prepare (string name)
  171. {
  172. StringBuilder result = new StringBuilder ();
  173. result.Append (name);
  174. result.Append (" ");
  175. result.Append (dbtype.ToString ().ToLower ());
  176. switch (dbtype) {
  177. case SqlDbType.Image :
  178. case SqlDbType.NVarChar :
  179. case SqlDbType.VarBinary :
  180. case SqlDbType.VarChar :
  181. if (!sizeSet || size == 0)
  182. throw new InvalidOperationException ("All variable length parameters must have an explicitly set non-zero size.");
  183. result.Append ("(");
  184. result.Append (size.ToString ());
  185. result.Append (")");
  186. break;
  187. case SqlDbType.Decimal :
  188. case SqlDbType.Money :
  189. case SqlDbType.SmallMoney :
  190. result.Append ("(");
  191. result.Append (precision.ToString ());
  192. result.Append (",");
  193. result.Append (scale.ToString ());
  194. result.Append (")");
  195. break;
  196. default:
  197. break;
  198. }
  199. return result.ToString ();
  200. }
  201. public override string ToString()
  202. {
  203. return parmName;
  204. }
  205. #endregion // Methods
  206. }
  207. }