SqlParameter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. [MonoTODO]
  43. public SqlParameter ()
  44. {
  45. }
  46. [MonoTODO]
  47. public SqlParameter (string parameterName, object value)
  48. {
  49. this.parmName = parameterName;
  50. this.objValue = value;
  51. }
  52. [MonoTODO]
  53. public SqlParameter (string parameterName, SqlDbType dbType)
  54. {
  55. this.parmName = parameterName;
  56. this.dbtype = dbType;
  57. }
  58. [MonoTODO]
  59. public SqlParameter (string parameterName, SqlDbType dbType, int size)
  60. {
  61. this.parmName = parameterName;
  62. this.dbtype = dbType;
  63. this.size = size;
  64. }
  65. [MonoTODO]
  66. public SqlParameter(string parameterName, SqlDbType dbType, int size, string sourceColumn)
  67. {
  68. this.parmName = parameterName;
  69. this.dbtype = dbType;
  70. this.size = size;
  71. this.sourceColumn = sourceColumn;
  72. }
  73. [MonoTODO]
  74. public SqlParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  75. {
  76. this.parmName = parameterName;
  77. this.dbtype = dbType;
  78. this.size = size;
  79. this.sourceColumn = sourceColumn;
  80. this.direction = direction;
  81. this.isNullable = isNullable;
  82. this.precision = precision;
  83. this.scale = scale;
  84. this.sourceVersion = sourceVersion;
  85. this.objValue = value;
  86. }
  87. #endregion // Constructors
  88. #region Properties
  89. [MonoTODO]
  90. public DbType DbType {
  91. get { return theDbType; }
  92. set { theDbType = value; }
  93. }
  94. [MonoTODO]
  95. public ParameterDirection Direction {
  96. get { return direction; }
  97. set { direction = value; }
  98. }
  99. [MonoTODO]
  100. public bool IsNullable {
  101. get { return isNullable; }
  102. }
  103. [MonoTODO]
  104. public int Offset {
  105. get { return offset; }
  106. set { offset = value; }
  107. }
  108. string IDataParameter.ParameterName {
  109. get { return parmName; }
  110. set { parmName = value; }
  111. }
  112. public string ParameterName {
  113. get { return parmName; }
  114. set { parmName = value; }
  115. }
  116. [MonoTODO]
  117. public string SourceColumn {
  118. get { return sourceColumn; }
  119. set { sourceColumn = value; }
  120. }
  121. [MonoTODO]
  122. public DataRowVersion SourceVersion {
  123. get { return sourceVersion; }
  124. set { sourceVersion = value; }
  125. }
  126. [MonoTODO]
  127. public SqlDbType SqlDbType {
  128. get { return dbtype; }
  129. set { dbtype = value; }
  130. }
  131. [MonoTODO]
  132. public object Value {
  133. get { return objValue; }
  134. set { objValue = value; }
  135. }
  136. [MonoTODO]
  137. public byte Precision {
  138. get { return precision; }
  139. set { precision = value; }
  140. }
  141. [MonoTODO]
  142. public byte Scale {
  143. get { return scale; }
  144. set { scale = value; }
  145. }
  146. [MonoTODO]
  147. public int Size {
  148. get { return size; }
  149. set {
  150. sizeSet = true;
  151. size = value;
  152. }
  153. }
  154. #endregion // Properties
  155. #region Methods
  156. [MonoTODO]
  157. object ICloneable.Clone ()
  158. {
  159. throw new NotImplementedException ();
  160. }
  161. internal string Prepare ()
  162. {
  163. StringBuilder result = new StringBuilder ();
  164. result.Append (parmName);
  165. result.Append (" ");
  166. result.Append (dbtype.ToString ());
  167. switch (dbtype) {
  168. case SqlDbType.Image :
  169. case SqlDbType.NVarChar :
  170. case SqlDbType.VarBinary :
  171. case SqlDbType.VarChar :
  172. if (!sizeSet || size == 0)
  173. throw new InvalidOperationException ("All variable length parameters must have an explicitly set non-zero size.");
  174. result.Append ("(");
  175. result.Append (size.ToString ());
  176. result.Append (")");
  177. break;
  178. case SqlDbType.Decimal :
  179. case SqlDbType.Money :
  180. case SqlDbType.SmallMoney :
  181. result.Append ("(");
  182. result.Append (precision.ToString ());
  183. result.Append (",");
  184. result.Append (scale.ToString ());
  185. result.Append (")");
  186. break;
  187. default:
  188. break;
  189. }
  190. return result.ToString ();
  191. }
  192. public override string ToString()
  193. {
  194. return parmName;
  195. }
  196. #endregion // Methods
  197. }
  198. }