SqlParameter.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. internal SqlParameter (object[] dbValues)
  77. {
  78. precision = 0;
  79. scale = 0;
  80. direction = ParameterDirection.Input;
  81. parmName = (string) dbValues[3];
  82. switch ((short) dbValues[5]) {
  83. case 1:
  84. direction = ParameterDirection.Input;
  85. break;
  86. case 2:
  87. direction = ParameterDirection.Output;
  88. break;
  89. case 3:
  90. direction = ParameterDirection.InputOutput;
  91. break;
  92. case 4:
  93. direction = ParameterDirection.ReturnValue;
  94. break;
  95. }
  96. isNullable = (bool) dbValues[8];
  97. if (dbValues[12] != null)
  98. precision = (byte) ((short) dbValues[12]);
  99. if (dbValues[13] != null)
  100. scale = (byte) ((short) dbValues[13]);
  101. dbtype = TypeNameToSqlDbType ((string) dbValues[16]);
  102. }
  103. #endregion // Constructors
  104. #region Properties
  105. [Browsable (false)]
  106. [DataSysDescription ("The parameter generic type.")]
  107. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  108. //[RefreshProperties (RefreshProperties.All)]
  109. public DbType DbType {
  110. get { return theDbType; }
  111. set { theDbType = value; }
  112. }
  113. [DataSysDescription ("Input, output, or bidirectional parameter.")]
  114. [DefaultValue (ParameterDirection.Input)]
  115. public ParameterDirection Direction {
  116. get { return direction; }
  117. set { direction = value; }
  118. }
  119. [Browsable (false)]
  120. [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
  121. [DefaultValue (false)]
  122. [DesignOnly (true)]
  123. [EditorBrowsable (EditorBrowsableState.Advanced)]
  124. public bool IsNullable {
  125. get { return isNullable; }
  126. }
  127. [Browsable (false)]
  128. [DataSysDescription ("Offset in variable length data types.")]
  129. [DefaultValue (0)]
  130. public int Offset {
  131. get { return offset; }
  132. set { offset = value; }
  133. }
  134. string IDataParameter.ParameterName {
  135. get { return parmName; }
  136. set { parmName = value; }
  137. }
  138. [DataSysDescription ("Name of the parameter, like '@p1'")]
  139. [DefaultValue ("")]
  140. public string ParameterName {
  141. get { return parmName; }
  142. set { parmName = value; }
  143. }
  144. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  145. [DefaultValue (0)]
  146. public byte Precision {
  147. get { return precision; }
  148. set { precision = value; }
  149. }
  150. [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.")]
  151. [DefaultValue ("")]
  152. public string SourceColumn {
  153. get { return sourceColumn; }
  154. set { sourceColumn = value; }
  155. }
  156. [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
  157. [DefaultValue (DataRowVersion.Current)]
  158. public DataRowVersion SourceVersion {
  159. get { return sourceVersion; }
  160. set { sourceVersion = value; }
  161. }
  162. [DataSysDescription ("The parameter native type.")]
  163. [DefaultValue (SqlDbType.NVarChar)]
  164. //[RefreshProperties (RefreshProperties.All)]
  165. public SqlDbType SqlDbType {
  166. get { return dbtype; }
  167. set { dbtype = value; }
  168. }
  169. [DataSysDescription ("Value of the parameter.")]
  170. [DefaultValue (null)]
  171. public object Value {
  172. get { return objValue; }
  173. set { objValue = value; }
  174. }
  175. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  176. [DefaultValue (0)]
  177. public byte Scale {
  178. get { return scale; }
  179. set { scale = value; }
  180. }
  181. [DataSysDescription ("Size of variable length datatypes (strings & arrays).")]
  182. [DefaultValue (0)]
  183. public int Size {
  184. get { return size; }
  185. set {
  186. sizeSet = true;
  187. size = value;
  188. }
  189. }
  190. #endregion // Properties
  191. #region Methods
  192. [MonoTODO]
  193. object ICloneable.Clone ()
  194. {
  195. throw new NotImplementedException ();
  196. }
  197. internal string Prepare (string name)
  198. {
  199. StringBuilder result = new StringBuilder ();
  200. result.Append (name);
  201. result.Append (" ");
  202. result.Append (dbtype.ToString ().ToLower ());
  203. switch (dbtype) {
  204. case SqlDbType.Image :
  205. case SqlDbType.NVarChar :
  206. case SqlDbType.VarBinary :
  207. case SqlDbType.VarChar :
  208. if (!sizeSet || size == 0)
  209. throw new InvalidOperationException ("All variable length parameters must have an explicitly set non-zero size.");
  210. result.Append ("(");
  211. result.Append (size.ToString ());
  212. result.Append (")");
  213. break;
  214. case SqlDbType.Decimal :
  215. case SqlDbType.Money :
  216. case SqlDbType.SmallMoney :
  217. result.Append ("(");
  218. result.Append (precision.ToString ());
  219. result.Append (",");
  220. result.Append (scale.ToString ());
  221. result.Append (")");
  222. break;
  223. default:
  224. break;
  225. }
  226. return result.ToString ();
  227. }
  228. internal static SqlDbType TypeNameToSqlDbType (string typeName)
  229. {
  230. switch (typeName) {
  231. case "bigint":
  232. return SqlDbType.BigInt;
  233. case "binary":
  234. return SqlDbType.Binary;
  235. case "bit":
  236. return SqlDbType.Bit;
  237. case "char":
  238. return SqlDbType.Char;
  239. case "datetime":
  240. return SqlDbType.DateTime;
  241. case "decimal":
  242. return SqlDbType.Decimal;
  243. case "float":
  244. return SqlDbType.Float;
  245. case "image":
  246. return SqlDbType.Image;
  247. case "int":
  248. return SqlDbType.Int;
  249. case "money":
  250. return SqlDbType.Money;
  251. case "nchar":
  252. return SqlDbType.NChar;
  253. case "ntext":
  254. return SqlDbType.NText;
  255. case "nvarchar":
  256. return SqlDbType.NVarChar;
  257. case "real":
  258. return SqlDbType.Real;
  259. case "smalldatetime":
  260. return SqlDbType.SmallDateTime;
  261. case "smallint":
  262. return SqlDbType.SmallInt;
  263. case "smallmoney":
  264. return SqlDbType.SmallMoney;
  265. case "text":
  266. return SqlDbType.Text;
  267. case "timestamp":
  268. return SqlDbType.Timestamp;
  269. case "tinyint":
  270. return SqlDbType.TinyInt;
  271. case "uniqueidentifier":
  272. return SqlDbType.UniqueIdentifier;
  273. case "varbinary":
  274. return SqlDbType.VarBinary;
  275. case "varchar":
  276. return SqlDbType.VarChar;
  277. }
  278. return SqlDbType.Variant;
  279. }
  280. public override string ToString()
  281. {
  282. return parmName;
  283. }
  284. #endregion // Methods
  285. }
  286. }