SqlParameter.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. object ICloneable.Clone ()
  193. {
  194. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  195. }
  196. internal string Prepare (string name)
  197. {
  198. StringBuilder result = new StringBuilder ();
  199. result.Append (name);
  200. result.Append (" ");
  201. result.Append (dbtype.ToString ().ToLower ());
  202. switch (dbtype) {
  203. case SqlDbType.Image :
  204. case SqlDbType.NVarChar :
  205. case SqlDbType.VarBinary :
  206. case SqlDbType.VarChar :
  207. if (!sizeSet || size == 0)
  208. throw new InvalidOperationException ("All variable length parameters must have an explicitly set non-zero size.");
  209. result.Append ("(");
  210. result.Append (size.ToString ());
  211. result.Append (")");
  212. break;
  213. case SqlDbType.Decimal :
  214. case SqlDbType.Money :
  215. case SqlDbType.SmallMoney :
  216. result.Append ("(");
  217. result.Append (precision.ToString ());
  218. result.Append (",");
  219. result.Append (scale.ToString ());
  220. result.Append (")");
  221. break;
  222. default:
  223. break;
  224. }
  225. return result.ToString ();
  226. }
  227. internal static SqlDbType TypeNameToSqlDbType (string typeName)
  228. {
  229. switch (typeName) {
  230. case "bigint":
  231. return SqlDbType.BigInt;
  232. case "binary":
  233. return SqlDbType.Binary;
  234. case "bit":
  235. return SqlDbType.Bit;
  236. case "char":
  237. return SqlDbType.Char;
  238. case "datetime":
  239. return SqlDbType.DateTime;
  240. case "decimal":
  241. return SqlDbType.Decimal;
  242. case "float":
  243. return SqlDbType.Float;
  244. case "image":
  245. return SqlDbType.Image;
  246. case "int":
  247. return SqlDbType.Int;
  248. case "money":
  249. return SqlDbType.Money;
  250. case "nchar":
  251. return SqlDbType.NChar;
  252. case "ntext":
  253. return SqlDbType.NText;
  254. case "nvarchar":
  255. return SqlDbType.NVarChar;
  256. case "real":
  257. return SqlDbType.Real;
  258. case "smalldatetime":
  259. return SqlDbType.SmallDateTime;
  260. case "smallint":
  261. return SqlDbType.SmallInt;
  262. case "smallmoney":
  263. return SqlDbType.SmallMoney;
  264. case "text":
  265. return SqlDbType.Text;
  266. case "timestamp":
  267. return SqlDbType.Timestamp;
  268. case "tinyint":
  269. return SqlDbType.TinyInt;
  270. case "uniqueidentifier":
  271. return SqlDbType.UniqueIdentifier;
  272. case "varbinary":
  273. return SqlDbType.VarBinary;
  274. case "varchar":
  275. return SqlDbType.VarChar;
  276. }
  277. return SqlDbType.Variant;
  278. }
  279. public override string ToString()
  280. {
  281. return parmName;
  282. }
  283. #endregion // Methods
  284. }
  285. }