SqlParameter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. // Diego Caravana ([email protected])
  9. //
  10. // (C) Ximian, Inc. 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using Mono.Data.Tds;
  36. using Mono.Data.Tds.Protocol;
  37. using System;
  38. using System.ComponentModel;
  39. using System.Data;
  40. using System.Data.Common;
  41. using System.Runtime.InteropServices;
  42. using System.Text;
  43. namespace System.Data.SqlClient {
  44. [TypeConverterAttribute (typeof (SqlParameterConverter))]
  45. public sealed class SqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  46. {
  47. #region Fields
  48. TdsMetaParameter metaParameter;
  49. SqlParameterCollection container = null;
  50. DbType dbType;
  51. ParameterDirection direction = ParameterDirection.Input;
  52. bool isNullable;
  53. bool isSizeSet = false;
  54. bool isTypeSet = false;
  55. int offset;
  56. SqlDbType sqlDbType;
  57. string sourceColumn;
  58. DataRowVersion sourceVersion;
  59. #endregion // Fields
  60. #region Constructors
  61. public SqlParameter ()
  62. : this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  63. {
  64. }
  65. public SqlParameter (string parameterName, object value)
  66. {
  67. metaParameter = new TdsMetaParameter (parameterName, value);
  68. this.sourceVersion = DataRowVersion.Current;
  69. InferSqlType (value);
  70. }
  71. public SqlParameter (string parameterName, SqlDbType dbType)
  72. : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  73. {
  74. }
  75. public SqlParameter (string parameterName, SqlDbType dbType, int size)
  76. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  77. {
  78. }
  79. public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn)
  80. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  81. {
  82. }
  83. [EditorBrowsable (EditorBrowsableState.Advanced)]
  84. public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  85. {
  86. metaParameter = new TdsMetaParameter (parameterName, size, isNullable, precision, scale, value);
  87. SqlDbType = dbType;
  88. Direction = direction;
  89. SourceColumn = sourceColumn;
  90. SourceVersion = sourceVersion;
  91. }
  92. // This constructor is used internally to construct a
  93. // SqlParameter. The value array comes from sp_procedure_params_rowset.
  94. // This is in SqlCommand.DeriveParameters.
  95. internal SqlParameter (object[] dbValues)
  96. {
  97. Precision = 0;
  98. Scale = 0;
  99. Direction = ParameterDirection.Input;
  100. ParameterName = (string) dbValues[3];
  101. switch ((short) dbValues[5]) {
  102. case 1:
  103. Direction = ParameterDirection.Input;
  104. break;
  105. case 2:
  106. Direction = ParameterDirection.Output;
  107. break;
  108. case 3:
  109. Direction = ParameterDirection.InputOutput;
  110. break;
  111. case 4:
  112. Direction = ParameterDirection.ReturnValue;
  113. break;
  114. }
  115. IsNullable = (bool) dbValues[8];
  116. if (dbValues[12] != null)
  117. Precision = (byte) ((short) dbValues[12]);
  118. if (dbValues[13] != null)
  119. Scale = (byte) ((short) dbValues[13]);
  120. SetDbTypeName ((string) dbValues[16]);
  121. }
  122. #endregion // Constructors
  123. #region Properties
  124. // Used to ensure that only one collection can contain this
  125. // parameter
  126. internal SqlParameterCollection Container {
  127. get { return container; }
  128. set { container = value; }
  129. }
  130. [Browsable (false)]
  131. [DataCategory ("Data")]
  132. [DataSysDescription ("The parameter generic type.")]
  133. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  134. [RefreshProperties (RefreshProperties.All)]
  135. public DbType DbType {
  136. get { return dbType; }
  137. set {
  138. SetDbType (value);
  139. isTypeSet = true;
  140. }
  141. }
  142. [DataCategory ("Data")]
  143. [DataSysDescription ("Input, output, or bidirectional parameter.")]
  144. [DefaultValue (ParameterDirection.Input)]
  145. public ParameterDirection Direction {
  146. get { return direction; }
  147. set {
  148. direction = value;
  149. switch( direction ) {
  150. case ParameterDirection.Output:
  151. MetaParameter.Direction = TdsParameterDirection.Output;
  152. break;
  153. case ParameterDirection.InputOutput:
  154. MetaParameter.Direction = TdsParameterDirection.InputOutput;
  155. break;
  156. case ParameterDirection.ReturnValue:
  157. MetaParameter.Direction = TdsParameterDirection.ReturnValue;
  158. break;
  159. }
  160. }
  161. }
  162. internal TdsMetaParameter MetaParameter {
  163. get { return metaParameter; }
  164. }
  165. string IDataParameter.ParameterName {
  166. get { return metaParameter.ParameterName; }
  167. set { metaParameter.ParameterName = value; }
  168. }
  169. [Browsable (false)]
  170. [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
  171. [DefaultValue (false)]
  172. [DesignOnly (true)]
  173. [EditorBrowsable (EditorBrowsableState.Advanced)]
  174. public bool IsNullable {
  175. get { return metaParameter.IsNullable; }
  176. set { metaParameter.IsNullable = value; }
  177. }
  178. [Browsable (false)]
  179. [DataCategory ("Data")]
  180. [DataSysDescription ("Offset in variable length data types.")]
  181. [DefaultValue (0)]
  182. public int Offset {
  183. get { return offset; }
  184. set { offset = value; }
  185. }
  186. [DataSysDescription ("Name of the parameter, like '@p1'")]
  187. [DefaultValue ("")]
  188. public string ParameterName {
  189. get { return metaParameter.ParameterName; }
  190. set { metaParameter.ParameterName = value; }
  191. }
  192. [DataCategory ("Data")]
  193. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  194. [DefaultValue (0)]
  195. public byte Precision {
  196. get { return metaParameter.Precision; }
  197. set { metaParameter.Precision = value; }
  198. }
  199. [DataCategory ("Data")]
  200. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  201. [DefaultValue (0)]
  202. public byte Scale {
  203. get { return metaParameter.Scale; }
  204. set { metaParameter.Scale = value; }
  205. }
  206. [DataCategory ("Data")]
  207. [DataSysDescription ("Size of variable length datatypes (strings & arrays).")]
  208. [DefaultValue (0)]
  209. public int Size {
  210. get { return metaParameter.Size; }
  211. set { metaParameter.Size = value; }
  212. }
  213. [DataCategory ("Data")]
  214. [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.")]
  215. [DefaultValue ("")]
  216. public string SourceColumn {
  217. get { return sourceColumn; }
  218. set { sourceColumn = value; }
  219. }
  220. [DataCategory ("Data")]
  221. [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
  222. [DefaultValue (DataRowVersion.Current)]
  223. public DataRowVersion SourceVersion {
  224. get { return sourceVersion; }
  225. set { sourceVersion = value; }
  226. }
  227. [DataCategory ("Data")]
  228. [DataSysDescription ("The parameter native type.")]
  229. [DefaultValue (SqlDbType.NVarChar)]
  230. [RefreshProperties (RefreshProperties.All)]
  231. public SqlDbType SqlDbType {
  232. get { return sqlDbType; }
  233. set {
  234. SetSqlDbType (value);
  235. isTypeSet = true;
  236. }
  237. }
  238. [DataCategory ("Data")]
  239. [DataSysDescription ("Value of the parameter.")]
  240. [DefaultValue (null)]
  241. [TypeConverterAttribute (typeof (StringConverter))]
  242. public object Value {
  243. get { return metaParameter.Value; }
  244. set {
  245. if (!isTypeSet)
  246. InferSqlType (value);
  247. metaParameter.Value = value;
  248. }
  249. }
  250. #endregion // Properties
  251. #region Methods
  252. object ICloneable.Clone ()
  253. {
  254. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  255. }
  256. // If the value is set without the DbType/SqlDbType being set, then we
  257. // infer type information.
  258. private void InferSqlType (object value)
  259. {
  260. Type type = value.GetType ();
  261. string exception = String.Format ("The parameter data type of {0} is invalid.", type.Name);
  262. switch (type.FullName) {
  263. case "System.Int64":
  264. SetSqlDbType (SqlDbType.BigInt);
  265. break;
  266. case "System.Boolean":
  267. SetSqlDbType (SqlDbType.Bit);
  268. break;
  269. case "System.String":
  270. SetSqlDbType (SqlDbType.NVarChar);
  271. break;
  272. case "System.DateTime":
  273. SetSqlDbType (SqlDbType.DateTime);
  274. break;
  275. case "System.Decimal":
  276. SetSqlDbType (SqlDbType.Decimal);
  277. break;
  278. case "System.Double":
  279. SetSqlDbType (SqlDbType.Float);
  280. break;
  281. case "System.Byte[]":
  282. SetSqlDbType (SqlDbType.VarBinary);
  283. break;
  284. case "System.Byte":
  285. SetSqlDbType (SqlDbType.TinyInt);
  286. break;
  287. case "System.Int32":
  288. SetSqlDbType (SqlDbType.Int);
  289. break;
  290. case "System.Single":
  291. SetSqlDbType (SqlDbType.Real);
  292. break;
  293. case "System.Int16":
  294. SetSqlDbType (SqlDbType.SmallInt);
  295. break;
  296. case "System.Guid":
  297. SetSqlDbType (SqlDbType.UniqueIdentifier);
  298. break;
  299. case "System.Object":
  300. SetSqlDbType (SqlDbType.Variant);
  301. break;
  302. default:
  303. throw new ArgumentException (exception);
  304. }
  305. }
  306. // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
  307. // string representation of the type name. If the DbType is not convertible
  308. // to an SqlDbType, throw an exception.
  309. private void SetDbType (DbType type)
  310. {
  311. string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
  312. switch (type) {
  313. case DbType.AnsiString:
  314. MetaParameter.TypeName = "varchar";
  315. sqlDbType = SqlDbType.VarChar;
  316. break;
  317. case DbType.AnsiStringFixedLength:
  318. MetaParameter.TypeName = "char";
  319. sqlDbType = SqlDbType.Char;
  320. break;
  321. case DbType.Binary:
  322. MetaParameter.TypeName = "varbinary";
  323. sqlDbType = SqlDbType.VarBinary;
  324. break;
  325. case DbType.Boolean:
  326. MetaParameter.TypeName = "bit";
  327. sqlDbType = SqlDbType.Bit;
  328. break;
  329. case DbType.Byte:
  330. MetaParameter.TypeName = "tinyint";
  331. sqlDbType = SqlDbType.TinyInt;
  332. break;
  333. case DbType.Currency:
  334. sqlDbType = SqlDbType.Money;
  335. MetaParameter.TypeName = "money";
  336. break;
  337. case DbType.Date:
  338. case DbType.DateTime:
  339. MetaParameter.TypeName = "datetime";
  340. sqlDbType = SqlDbType.DateTime;
  341. break;
  342. case DbType.Decimal:
  343. MetaParameter.TypeName = "decimal";
  344. sqlDbType = SqlDbType.Decimal;
  345. break;
  346. case DbType.Double:
  347. MetaParameter.TypeName = "float";
  348. sqlDbType = SqlDbType.Float;
  349. break;
  350. case DbType.Guid:
  351. MetaParameter.TypeName = "uniqueidentifier";
  352. sqlDbType = SqlDbType.UniqueIdentifier;
  353. break;
  354. case DbType.Int16:
  355. MetaParameter.TypeName = "smallint";
  356. sqlDbType = SqlDbType.SmallInt;
  357. break;
  358. case DbType.Int32:
  359. MetaParameter.TypeName = "int";
  360. sqlDbType = SqlDbType.Int;
  361. break;
  362. case DbType.Int64:
  363. MetaParameter.TypeName = "bigint";
  364. sqlDbType = SqlDbType.BigInt;
  365. break;
  366. case DbType.Object:
  367. MetaParameter.TypeName = "sql_variant";
  368. sqlDbType = SqlDbType.Variant;
  369. break;
  370. case DbType.Single:
  371. MetaParameter.TypeName = "real";
  372. sqlDbType = SqlDbType.Real;
  373. break;
  374. case DbType.String:
  375. MetaParameter.TypeName = "nvarchar";
  376. sqlDbType = SqlDbType.NVarChar;
  377. break;
  378. case DbType.StringFixedLength:
  379. MetaParameter.TypeName = "nchar";
  380. sqlDbType = SqlDbType.NChar;
  381. break;
  382. case DbType.Time:
  383. MetaParameter.TypeName = "datetime";
  384. sqlDbType = SqlDbType.DateTime;
  385. break;
  386. default:
  387. throw new ArgumentException (exception);
  388. }
  389. dbType = type;
  390. }
  391. // Used by internal constructor which has a SQL Server typename
  392. private void SetDbTypeName (string dbTypeName)
  393. {
  394. switch (dbTypeName.ToLower ()) {
  395. case "bigint":
  396. SqlDbType = SqlDbType.BigInt;
  397. break;
  398. case "binary":
  399. SqlDbType = SqlDbType.Binary;
  400. break;
  401. case "bit":
  402. SqlDbType = SqlDbType.Bit;
  403. break;
  404. case "char":
  405. SqlDbType = SqlDbType.Char;
  406. break;
  407. case "datetime":
  408. SqlDbType = SqlDbType.DateTime;
  409. break;
  410. case "decimal":
  411. SqlDbType = SqlDbType.Decimal;
  412. break;
  413. case "float":
  414. SqlDbType = SqlDbType.Float;
  415. break;
  416. case "image":
  417. SqlDbType = SqlDbType.Image;
  418. break;
  419. case "int":
  420. SqlDbType = SqlDbType.Int;
  421. break;
  422. case "money":
  423. SqlDbType = SqlDbType.Money;
  424. break;
  425. case "nchar":
  426. SqlDbType = SqlDbType.NChar;
  427. break;
  428. case "ntext":
  429. SqlDbType = SqlDbType.NText;
  430. break;
  431. case "nvarchar":
  432. SqlDbType = SqlDbType.NVarChar;
  433. break;
  434. case "real":
  435. SqlDbType = SqlDbType.Real;
  436. break;
  437. case "smalldatetime":
  438. SqlDbType = SqlDbType.SmallDateTime;
  439. break;
  440. case "smallint":
  441. SqlDbType = SqlDbType.SmallInt;
  442. break;
  443. case "smallmoney":
  444. SqlDbType = SqlDbType.SmallMoney;
  445. break;
  446. case "text":
  447. SqlDbType = SqlDbType.Text;
  448. break;
  449. case "timestamp":
  450. SqlDbType = SqlDbType.Timestamp;
  451. break;
  452. case "tinyint":
  453. SqlDbType = SqlDbType.TinyInt;
  454. break;
  455. case "uniqueidentifier":
  456. SqlDbType = SqlDbType.UniqueIdentifier;
  457. break;
  458. case "varbinary":
  459. SqlDbType = SqlDbType.VarBinary;
  460. break;
  461. case "varchar":
  462. SqlDbType = SqlDbType.VarChar;
  463. break;
  464. default:
  465. SqlDbType = SqlDbType.Variant;
  466. break;
  467. }
  468. }
  469. // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
  470. // string representation of the type name. If the SqlDbType is not convertible
  471. // to a DbType, throw an exception.
  472. private void SetSqlDbType (SqlDbType type)
  473. {
  474. string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
  475. switch (type) {
  476. case SqlDbType.BigInt:
  477. MetaParameter.TypeName = "bigint";
  478. dbType = DbType.Int64;
  479. break;
  480. case SqlDbType.Binary:
  481. MetaParameter.TypeName = "binary";
  482. dbType = DbType.Binary;
  483. break;
  484. case SqlDbType.Timestamp:
  485. MetaParameter.TypeName = "timestamp";
  486. dbType = DbType.Binary;
  487. break;
  488. case SqlDbType.VarBinary:
  489. MetaParameter.TypeName = "varbinary";
  490. dbType = DbType.Binary;
  491. break;
  492. case SqlDbType.Bit:
  493. MetaParameter.TypeName = "bit";
  494. dbType = DbType.Boolean;
  495. break;
  496. case SqlDbType.Char:
  497. MetaParameter.TypeName = "char";
  498. dbType = DbType.AnsiStringFixedLength;
  499. break;
  500. case SqlDbType.DateTime:
  501. MetaParameter.TypeName = "datetime";
  502. dbType = DbType.DateTime;
  503. break;
  504. case SqlDbType.SmallDateTime:
  505. MetaParameter.TypeName = "smalldatetime";
  506. dbType = DbType.DateTime;
  507. break;
  508. case SqlDbType.Decimal:
  509. MetaParameter.TypeName = "decimal";
  510. dbType = DbType.Decimal;
  511. break;
  512. case SqlDbType.Float:
  513. MetaParameter.TypeName = "float";
  514. dbType = DbType.Double;
  515. break;
  516. case SqlDbType.Image:
  517. MetaParameter.TypeName = "image";
  518. dbType = DbType.Binary;
  519. break;
  520. case SqlDbType.Int:
  521. MetaParameter.TypeName = "int";
  522. dbType = DbType.Int32;
  523. break;
  524. case SqlDbType.Money:
  525. MetaParameter.TypeName = "money";
  526. dbType = DbType.Currency;
  527. break;
  528. case SqlDbType.SmallMoney:
  529. MetaParameter.TypeName = "smallmoney";
  530. dbType = DbType.Currency;
  531. break;
  532. case SqlDbType.NChar:
  533. MetaParameter.TypeName = "nchar";
  534. dbType = DbType.StringFixedLength;
  535. break;
  536. case SqlDbType.NText:
  537. MetaParameter.TypeName = "ntext";
  538. dbType = DbType.String;
  539. break;
  540. case SqlDbType.NVarChar:
  541. MetaParameter.TypeName = "nvarchar";
  542. dbType = DbType.String;
  543. break;
  544. case SqlDbType.Real:
  545. MetaParameter.TypeName = "real";
  546. dbType = DbType.Single;
  547. break;
  548. case SqlDbType.SmallInt:
  549. MetaParameter.TypeName = "smallint";
  550. dbType = DbType.Int16;
  551. break;
  552. case SqlDbType.Text:
  553. MetaParameter.TypeName = "text";
  554. dbType = DbType.AnsiString;
  555. break;
  556. case SqlDbType.VarChar:
  557. MetaParameter.TypeName = "varchar";
  558. dbType = DbType.AnsiString;
  559. break;
  560. case SqlDbType.TinyInt:
  561. MetaParameter.TypeName = "tinyint";
  562. dbType = DbType.Byte;
  563. break;
  564. case SqlDbType.UniqueIdentifier:
  565. MetaParameter.TypeName = "uniqueidentifier";
  566. dbType = DbType.Guid;
  567. break;
  568. case SqlDbType.Variant:
  569. MetaParameter.TypeName = "sql_variant";
  570. dbType = DbType.Object;
  571. break;
  572. default:
  573. throw new ArgumentException (exception);
  574. }
  575. sqlDbType = type;
  576. }
  577. public override string ToString()
  578. {
  579. return ParameterName;
  580. }
  581. #endregion // Methods
  582. }
  583. }