SqlParameter.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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. // Umadevi S ([email protected])
  10. //
  11. // (C) Ximian, Inc. 2002
  12. // Copyright (C) Tim Coleman, 2002
  13. //
  14. //
  15. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  16. //
  17. // Permission is hereby granted, free of charge, to any person obtaining
  18. // a copy of this software and associated documentation files (the
  19. // "Software"), to deal in the Software without restriction, including
  20. // without limitation the rights to use, copy, modify, merge, publish,
  21. // distribute, sublicense, and/or sell copies of the Software, and to
  22. // permit persons to whom the Software is furnished to do so, subject to
  23. // the following conditions:
  24. //
  25. // The above copyright notice and this permission notice shall be
  26. // included in all copies or substantial portions of the Software.
  27. //
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  32. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  33. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  34. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. using Mono.Data.Tds;
  37. using Mono.Data.Tds.Protocol;
  38. using System;
  39. using System.ComponentModel;
  40. using System.Data;
  41. using System.Data.Common;
  42. #if NET_2_0
  43. using System.Data.ProviderBase;
  44. #endif // NET_2_0
  45. using System.Data.SqlTypes;
  46. using System.Runtime.InteropServices;
  47. using System.Text;
  48. namespace System.Data.SqlClient {
  49. [TypeConverterAttribute (typeof (SqlParameterConverter))]
  50. #if NET_2_0
  51. public sealed class SqlParameter : DbParameterBase, IDbDataParameter, IDataParameter, ICloneable
  52. #else
  53. public sealed class SqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
  54. #endif // NET_2_0
  55. {
  56. #region Fields
  57. TdsMetaParameter metaParameter;
  58. SqlParameterCollection container = null;
  59. DbType dbType;
  60. ParameterDirection direction = ParameterDirection.Input;
  61. bool isNullable;
  62. bool isVariableSizeType = false;
  63. bool isTypeSet = false;
  64. int offset;
  65. SqlDbType sqlDbType;
  66. string sourceColumn;
  67. DataRowVersion sourceVersion;
  68. #endregion // Fields
  69. #region Constructors
  70. public SqlParameter ()
  71. : this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  72. {
  73. isTypeSet = false;
  74. }
  75. public SqlParameter (string parameterName, object value)
  76. {
  77. metaParameter = new TdsMetaParameter (parameterName, SqlTypeToFrameworkType (value));
  78. this.sourceVersion = DataRowVersion.Current;
  79. InferSqlType (value);
  80. }
  81. public SqlParameter (string parameterName, SqlDbType dbType)
  82. : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  83. {
  84. }
  85. public SqlParameter (string parameterName, SqlDbType dbType, int size)
  86. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  87. {
  88. }
  89. public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn)
  90. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  91. {
  92. }
  93. [EditorBrowsable (EditorBrowsableState.Advanced)]
  94. public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  95. {
  96. metaParameter = new TdsMetaParameter (parameterName, size,
  97. isNullable, precision,
  98. scale,
  99. SqlTypeToFrameworkType (value));
  100. SqlDbType = dbType;
  101. Direction = direction;
  102. SourceColumn = sourceColumn;
  103. SourceVersion = sourceVersion;
  104. }
  105. // This constructor is used internally to construct a
  106. // SqlParameter. The value array comes from sp_procedure_params_rowset.
  107. // This is in SqlCommand.DeriveParameters.
  108. internal SqlParameter (object[] dbValues)
  109. : this (dbValues [3].ToString (), String.Empty)
  110. {
  111. Precision = 0;
  112. Scale = 0;
  113. Direction = ParameterDirection.Input;
  114. ParameterName = (string) dbValues[3];
  115. switch ((short) dbValues[5]) {
  116. case 1:
  117. Direction = ParameterDirection.Input;
  118. break;
  119. case 2:
  120. Direction = ParameterDirection.Output;
  121. break;
  122. case 3:
  123. Direction = ParameterDirection.InputOutput;
  124. break;
  125. case 4:
  126. Direction = ParameterDirection.ReturnValue;
  127. break;
  128. default:
  129. Direction = ParameterDirection.Input;
  130. break;
  131. }
  132. IsNullable = (dbValues [8] != null &&
  133. dbValues [8] != DBNull.Value) ? (bool) dbValues [8] : false;
  134. if (dbValues [12] != null && dbValues [12] != DBNull.Value)
  135. Precision = (byte) ((short) dbValues [12]);
  136. if (dbValues [13] != null && dbValues [13] != DBNull.Value)
  137. Scale = (byte) ( (short) dbValues [13]);
  138. SetDbTypeName ((string) dbValues [16]);
  139. }
  140. #endregion // Constructors
  141. #region Properties
  142. // Used to ensure that only one collection can contain this
  143. // parameter
  144. internal SqlParameterCollection Container {
  145. get { return container; }
  146. set { container = value; }
  147. }
  148. internal void CheckIfInitialized ()
  149. {
  150. if (!isTypeSet)
  151. throw new Exception ("all parameters to have an explicity set type");
  152. if (isVariableSizeType) {
  153. if (SqlDbType == SqlDbType.Decimal && Precision == 0)
  154. throw new Exception ("Parameter of type 'Decimal' have an explicitly set Precision and Scale");
  155. else if (Size == 0)
  156. throw new Exception ("all variable length parameters to have an explicitly set non-zero Size");
  157. }
  158. }
  159. #if ONLY_1_0 || ONLY_1_1
  160. [Browsable (false)]
  161. [DataSysDescription ("The parameter generic type.")]
  162. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  163. [RefreshProperties (RefreshProperties.All)]
  164. #endif
  165. [DataCategory ("Data")]
  166. public
  167. #if NET_2_0
  168. override
  169. #endif // NET_2_0
  170. DbType DbType {
  171. get { return dbType; }
  172. set {
  173. SetDbType (value);
  174. isTypeSet = true;
  175. }
  176. }
  177. [DataCategory ("Data")]
  178. #if ONLY_1_0 || ONLY_1_1
  179. [DataSysDescription ("Input, output, or bidirectional parameter.")]
  180. [DefaultValue (ParameterDirection.Input)]
  181. #endif
  182. #if NET_2_0
  183. [RefreshProperties (RefreshProperties.All)]
  184. #endif
  185. public
  186. #if NET_2_0
  187. override
  188. #endif // NET_2_0
  189. ParameterDirection Direction {
  190. get { return direction; }
  191. set {
  192. direction = value;
  193. switch( direction ) {
  194. case ParameterDirection.Output:
  195. MetaParameter.Direction = TdsParameterDirection.Output;
  196. break;
  197. case ParameterDirection.InputOutput:
  198. MetaParameter.Direction = TdsParameterDirection.InputOutput;
  199. break;
  200. case ParameterDirection.ReturnValue:
  201. MetaParameter.Direction = TdsParameterDirection.ReturnValue;
  202. break;
  203. }
  204. }
  205. }
  206. internal TdsMetaParameter MetaParameter {
  207. get { return metaParameter; }
  208. }
  209. string IDataParameter.ParameterName {
  210. get { return metaParameter.ParameterName; }
  211. set { metaParameter.ParameterName = value; }
  212. }
  213. #if ONLY_1_0 || ONLY_1_1
  214. [Browsable (false)]
  215. [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
  216. [DefaultValue (false)]
  217. [DesignOnly (true)]
  218. [EditorBrowsable (EditorBrowsableState.Advanced)]
  219. #endif
  220. public
  221. #if NET_2_0
  222. override
  223. #endif // NET_2_0
  224. bool IsNullable {
  225. get { return metaParameter.IsNullable; }
  226. set { metaParameter.IsNullable = value; }
  227. }
  228. [Browsable (false)]
  229. [DataCategory ("Data")]
  230. #if ONLY_1_0 || ONLY_1_1
  231. [DataSysDescription ("Offset in variable length data types.")]
  232. [DefaultValue (0)]
  233. #endif
  234. #if NET_2_0
  235. [EditorBrowsable (EditorBrowsableState.Advanced)]
  236. #endif
  237. public
  238. #if NET_2_0
  239. override
  240. #endif
  241. int Offset {
  242. get { return offset; }
  243. set { offset = value; }
  244. }
  245. #if ONLY_1_0 || ONLY_1_1
  246. [DataSysDescription ("Name of the parameter, like '@p1'")]
  247. [DefaultValue ("")]
  248. #endif
  249. public
  250. #if NET_2_0
  251. override
  252. #endif // NET_2_0
  253. string ParameterName {
  254. get { return metaParameter.ParameterName; }
  255. set { metaParameter.ParameterName = value; }
  256. }
  257. [DataCategory ("Data")]
  258. #if ONLY_1_0 || ONLY_1_1
  259. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  260. [DefaultValue (0)]
  261. #endif
  262. #if NET_2_0
  263. [Browsable (false)]
  264. [EditorBrowsable (EditorBrowsableState.Never)]
  265. #endif
  266. public
  267. #if NET_2_0
  268. override
  269. #endif // NET_2_0
  270. byte Precision {
  271. get { return metaParameter.Precision; }
  272. set { metaParameter.Precision = value; }
  273. }
  274. [DataCategory ("Data")]
  275. #if ONLY_1_0 || ONLY_1_1
  276. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  277. [DefaultValue (0)]
  278. #endif
  279. #if NET_2_0
  280. [Browsable (false)]
  281. [EditorBrowsable (EditorBrowsableState.Never)]
  282. #endif
  283. public
  284. #if NET_2_0
  285. override
  286. #endif // NET_2_0
  287. byte Scale {
  288. get { return metaParameter.Scale; }
  289. set { metaParameter.Scale = value; }
  290. }
  291. [DataCategory ("Data")]
  292. #if ONLY_1_0 || ONLY_1_1
  293. [DataSysDescription ("Size of variable length data types (string & arrays).")]
  294. [DefaultValue (0)]
  295. #endif
  296. public
  297. #if NET_2_0
  298. override
  299. #endif // NET_2_0
  300. int Size {
  301. get { return metaParameter.Size; }
  302. set { metaParameter.Size = value; }
  303. }
  304. [DataCategory ("Data")]
  305. #if ONLY_1_0 || ONLY_1_1
  306. [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.")]
  307. [DefaultValue ("")]
  308. #endif
  309. public
  310. #if NET_2_0
  311. override
  312. #endif // NET_2_0
  313. string SourceColumn {
  314. get { return sourceColumn; }
  315. set { sourceColumn = value; }
  316. }
  317. [DataCategory ("Data")]
  318. #if ONLY_1_0 || ONLY_1_1
  319. [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
  320. [DefaultValue (DataRowVersion.Current)]
  321. #endif
  322. public
  323. #if NET_2_0
  324. override
  325. #endif // NET_2_0
  326. DataRowVersion SourceVersion {
  327. get { return sourceVersion; }
  328. set { sourceVersion = value; }
  329. }
  330. [DataCategory ("Data")]
  331. #if ONLY_1_0 || ONLY_1_1
  332. [DataSysDescription ("The parameter native type.")]
  333. [DefaultValue (SqlDbType.NVarChar)]
  334. #endif
  335. [RefreshProperties (RefreshProperties.All)]
  336. #if NET_2_0
  337. [DbProviderSpecificTypeProperty(true)]
  338. #endif
  339. public SqlDbType SqlDbType {
  340. get { return sqlDbType; }
  341. set {
  342. SetSqlDbType (value);
  343. isTypeSet = true;
  344. }
  345. }
  346. [DataCategory ("Data")]
  347. #if ONLY_1_0 || ONLY_1_1
  348. [DataSysDescription ("Value of the parameter.")]
  349. [DefaultValue (null)]
  350. #endif
  351. [TypeConverterAttribute (typeof (StringConverter))]
  352. #if NET_2_0
  353. [RefreshProperties (RefreshProperties.All)]
  354. #endif
  355. public
  356. #if NET_2_0
  357. override
  358. #endif // NET_2_0
  359. object Value {
  360. get { return metaParameter.Value; }
  361. set {
  362. if (!isTypeSet)
  363. InferSqlType (value);
  364. metaParameter.Value = SqlTypeToFrameworkType (value);
  365. }
  366. }
  367. //#if NET_2_0
  368. // public SqlCompareOptions CompareInfo{
  369. //#endif
  370. #if NET_2_0
  371. public override bool SourceColumnNullMapping {
  372. get { return false ; }
  373. set { }
  374. }
  375. #endif
  376. #endregion // Properties
  377. #region Methods
  378. object ICloneable.Clone ()
  379. {
  380. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  381. }
  382. // If the value is set without the DbType/SqlDbType being set, then we
  383. // infer type information.
  384. private void InferSqlType (object value)
  385. {
  386. Type type = value.GetType ();
  387. string exception = String.Format ("The parameter data type of {0} is invalid.", type.Name);
  388. switch (type.FullName) {
  389. case "System.Int64":
  390. case "System.Data.SqlTypes.SqlInt64":
  391. SetSqlDbType (SqlDbType.BigInt);
  392. break;
  393. case "System.Boolean":
  394. case "System.Data.SqlTypes.SqlBoolean":
  395. SetSqlDbType (SqlDbType.Bit);
  396. break;
  397. case "System.String":
  398. case "System.Data.SqlTypes.SqlString":
  399. SetSqlDbType (SqlDbType.NVarChar);
  400. break;
  401. case "System.DateTime":
  402. case "System.Data.SqlTypes.SqlDateTime":
  403. SetSqlDbType (SqlDbType.DateTime);
  404. break;
  405. case "System.Decimal":
  406. case "System.Data.SqlTypes.SqlDecimal":
  407. SetSqlDbType (SqlDbType.Decimal);
  408. break;
  409. case "System.Double":
  410. case "System.Data.SqlTypes.SqlDouble":
  411. SetSqlDbType (SqlDbType.Float);
  412. break;
  413. case "System.Byte[]":
  414. case "System.Data.SqlTypes.SqlBinary":
  415. SetSqlDbType (SqlDbType.VarBinary);
  416. break;
  417. case "System.Byte":
  418. case "System.Data.SqlTypes.SqlByte":
  419. SetSqlDbType (SqlDbType.TinyInt);
  420. break;
  421. case "System.Int32":
  422. case "System.Data.SqlTypes.SqlInt32":
  423. SetSqlDbType (SqlDbType.Int);
  424. break;
  425. case "System.Single":
  426. case "System.Data.SqlTypes.Single":
  427. SetSqlDbType (SqlDbType.Real);
  428. break;
  429. case "System.Int16":
  430. case "System.Data.SqlTypes.SqlInt16":
  431. SetSqlDbType (SqlDbType.SmallInt);
  432. break;
  433. case "System.Guid":
  434. case "System.Data.SqlTypes.SqlGuid":
  435. SetSqlDbType (SqlDbType.UniqueIdentifier);
  436. break;
  437. case "System.Money":
  438. case "System.SmallMoney":
  439. case "System.Data.SqlTypes.SqlMoney":
  440. SetSqlDbType (SqlDbType.Money);
  441. break;
  442. case "System.Object":
  443. case "System.DBNull":
  444. SetSqlDbType (SqlDbType.Variant); // variant can contain numeric,
  445. //string,binary or data and also nul //values, so we can later resolve // it to correct type.
  446. break;
  447. default:
  448. throw new ArgumentException (exception);
  449. }
  450. }
  451. // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
  452. // string representation of the type name. If the DbType is not convertible
  453. // to an SqlDbType, throw an exception.
  454. private void SetDbType (DbType type)
  455. {
  456. string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
  457. switch (type) {
  458. case DbType.AnsiString:
  459. MetaParameter.TypeName = "varchar";
  460. sqlDbType = SqlDbType.VarChar;
  461. isVariableSizeType = true;
  462. break;
  463. case DbType.AnsiStringFixedLength:
  464. MetaParameter.TypeName = "char";
  465. sqlDbType = SqlDbType.Char;
  466. isVariableSizeType = true;
  467. break;
  468. case DbType.Binary:
  469. MetaParameter.TypeName = "varbinary";
  470. sqlDbType = SqlDbType.VarBinary;
  471. isVariableSizeType = true;
  472. break;
  473. case DbType.Boolean:
  474. MetaParameter.TypeName = "bit";
  475. sqlDbType = SqlDbType.Bit;
  476. break;
  477. case DbType.Byte:
  478. MetaParameter.TypeName = "tinyint";
  479. sqlDbType = SqlDbType.TinyInt;
  480. break;
  481. case DbType.Currency:
  482. sqlDbType = SqlDbType.Money;
  483. MetaParameter.TypeName = "money";
  484. break;
  485. case DbType.Date:
  486. case DbType.DateTime:
  487. MetaParameter.TypeName = "datetime";
  488. sqlDbType = SqlDbType.DateTime;
  489. break;
  490. case DbType.Decimal:
  491. MetaParameter.TypeName = "decimal";
  492. sqlDbType = SqlDbType.Decimal;
  493. break;
  494. case DbType.Double:
  495. MetaParameter.TypeName = "float";
  496. sqlDbType = SqlDbType.Float;
  497. break;
  498. case DbType.Guid:
  499. MetaParameter.TypeName = "uniqueidentifier";
  500. sqlDbType = SqlDbType.UniqueIdentifier;
  501. break;
  502. case DbType.Int16:
  503. MetaParameter.TypeName = "smallint";
  504. sqlDbType = SqlDbType.SmallInt;
  505. break;
  506. case DbType.Int32:
  507. MetaParameter.TypeName = "int";
  508. sqlDbType = SqlDbType.Int;
  509. break;
  510. case DbType.Int64:
  511. MetaParameter.TypeName = "bigint";
  512. sqlDbType = SqlDbType.BigInt;
  513. break;
  514. case DbType.Object:
  515. MetaParameter.TypeName = "sql_variant";
  516. sqlDbType = SqlDbType.Variant;
  517. break;
  518. case DbType.Single:
  519. MetaParameter.TypeName = "real";
  520. sqlDbType = SqlDbType.Real;
  521. break;
  522. case DbType.String:
  523. MetaParameter.TypeName = "nvarchar";
  524. sqlDbType = SqlDbType.NVarChar;
  525. isVariableSizeType = true;
  526. break;
  527. case DbType.StringFixedLength:
  528. MetaParameter.TypeName = "nchar";
  529. sqlDbType = SqlDbType.NChar;
  530. isVariableSizeType = true;
  531. break;
  532. case DbType.Time:
  533. MetaParameter.TypeName = "datetime";
  534. sqlDbType = SqlDbType.DateTime;
  535. break;
  536. default:
  537. throw new ArgumentException (exception);
  538. }
  539. dbType = type;
  540. }
  541. // Used by internal constructor which has a SQL Server typename
  542. private void SetDbTypeName (string dbTypeName)
  543. {
  544. switch (dbTypeName.ToLower ()) {
  545. case "bigint":
  546. SqlDbType = SqlDbType.BigInt;
  547. break;
  548. case "binary":
  549. SqlDbType = SqlDbType.Binary;
  550. break;
  551. case "bit":
  552. SqlDbType = SqlDbType.Bit;
  553. break;
  554. case "char":
  555. SqlDbType = SqlDbType.Char;
  556. break;
  557. case "datetime":
  558. SqlDbType = SqlDbType.DateTime;
  559. break;
  560. case "decimal":
  561. SqlDbType = SqlDbType.Decimal;
  562. break;
  563. case "float":
  564. SqlDbType = SqlDbType.Float;
  565. break;
  566. case "image":
  567. SqlDbType = SqlDbType.Image;
  568. break;
  569. case "int":
  570. SqlDbType = SqlDbType.Int;
  571. break;
  572. case "money":
  573. SqlDbType = SqlDbType.Money;
  574. break;
  575. case "nchar":
  576. SqlDbType = SqlDbType.NChar;
  577. break;
  578. case "ntext":
  579. SqlDbType = SqlDbType.NText;
  580. break;
  581. case "nvarchar":
  582. SqlDbType = SqlDbType.NVarChar;
  583. break;
  584. case "real":
  585. SqlDbType = SqlDbType.Real;
  586. break;
  587. case "smalldatetime":
  588. SqlDbType = SqlDbType.SmallDateTime;
  589. break;
  590. case "smallint":
  591. SqlDbType = SqlDbType.SmallInt;
  592. break;
  593. case "smallmoney":
  594. SqlDbType = SqlDbType.SmallMoney;
  595. break;
  596. case "text":
  597. SqlDbType = SqlDbType.Text;
  598. break;
  599. case "timestamp":
  600. SqlDbType = SqlDbType.Timestamp;
  601. break;
  602. case "tinyint":
  603. SqlDbType = SqlDbType.TinyInt;
  604. break;
  605. case "uniqueidentifier":
  606. SqlDbType = SqlDbType.UniqueIdentifier;
  607. break;
  608. case "varbinary":
  609. SqlDbType = SqlDbType.VarBinary;
  610. break;
  611. case "varchar":
  612. SqlDbType = SqlDbType.VarChar;
  613. break;
  614. default:
  615. SqlDbType = SqlDbType.Variant;
  616. break;
  617. }
  618. }
  619. // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
  620. // string representation of the type name. If the SqlDbType is not convertible
  621. // to a DbType, throw an exception.
  622. private void SetSqlDbType (SqlDbType type)
  623. {
  624. string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
  625. switch (type) {
  626. case SqlDbType.BigInt:
  627. MetaParameter.TypeName = "bigint";
  628. dbType = DbType.Int64;
  629. break;
  630. case SqlDbType.Binary:
  631. MetaParameter.TypeName = "binary";
  632. dbType = DbType.Binary;
  633. isVariableSizeType = true;
  634. break;
  635. case SqlDbType.Timestamp:
  636. MetaParameter.TypeName = "timestamp";
  637. dbType = DbType.Binary;
  638. break;
  639. case SqlDbType.VarBinary:
  640. MetaParameter.TypeName = "varbinary";
  641. dbType = DbType.Binary;
  642. isVariableSizeType = true;
  643. break;
  644. case SqlDbType.Bit:
  645. MetaParameter.TypeName = "bit";
  646. dbType = DbType.Boolean;
  647. break;
  648. case SqlDbType.Char:
  649. MetaParameter.TypeName = "char";
  650. dbType = DbType.AnsiStringFixedLength;
  651. isVariableSizeType = true;
  652. break;
  653. case SqlDbType.DateTime:
  654. MetaParameter.TypeName = "datetime";
  655. dbType = DbType.DateTime;
  656. break;
  657. case SqlDbType.SmallDateTime:
  658. MetaParameter.TypeName = "smalldatetime";
  659. dbType = DbType.DateTime;
  660. break;
  661. case SqlDbType.Decimal:
  662. MetaParameter.TypeName = "decimal";
  663. dbType = DbType.Decimal;
  664. break;
  665. case SqlDbType.Float:
  666. MetaParameter.TypeName = "float";
  667. dbType = DbType.Double;
  668. break;
  669. case SqlDbType.Image:
  670. MetaParameter.TypeName = "image";
  671. dbType = DbType.Binary;
  672. isVariableSizeType = true;
  673. break;
  674. case SqlDbType.Int:
  675. MetaParameter.TypeName = "int";
  676. dbType = DbType.Int32;
  677. break;
  678. case SqlDbType.Money:
  679. MetaParameter.TypeName = "money";
  680. dbType = DbType.Currency;
  681. break;
  682. case SqlDbType.SmallMoney:
  683. MetaParameter.TypeName = "smallmoney";
  684. dbType = DbType.Currency;
  685. break;
  686. case SqlDbType.NChar:
  687. MetaParameter.TypeName = "nchar";
  688. dbType = DbType.StringFixedLength;
  689. isVariableSizeType = true;
  690. break;
  691. case SqlDbType.NText:
  692. MetaParameter.TypeName = "ntext";
  693. dbType = DbType.String;
  694. isVariableSizeType = true;
  695. break;
  696. case SqlDbType.NVarChar:
  697. MetaParameter.TypeName = "nvarchar";
  698. dbType = DbType.String;
  699. isVariableSizeType = true;
  700. break;
  701. case SqlDbType.Real:
  702. MetaParameter.TypeName = "real";
  703. dbType = DbType.Single;
  704. break;
  705. case SqlDbType.SmallInt:
  706. MetaParameter.TypeName = "smallint";
  707. dbType = DbType.Int16;
  708. break;
  709. case SqlDbType.Text:
  710. MetaParameter.TypeName = "text";
  711. dbType = DbType.AnsiString;
  712. isVariableSizeType = true;
  713. break;
  714. case SqlDbType.VarChar:
  715. MetaParameter.TypeName = "varchar";
  716. dbType = DbType.AnsiString;
  717. isVariableSizeType = true;
  718. break;
  719. case SqlDbType.TinyInt:
  720. MetaParameter.TypeName = "tinyint";
  721. dbType = DbType.Byte;
  722. break;
  723. case SqlDbType.UniqueIdentifier:
  724. MetaParameter.TypeName = "uniqueidentifier";
  725. dbType = DbType.Guid;
  726. break;
  727. case SqlDbType.Variant:
  728. MetaParameter.TypeName = "sql_variant";
  729. dbType = DbType.Object;
  730. break;
  731. default:
  732. throw new ArgumentException (exception);
  733. }
  734. sqlDbType = type;
  735. }
  736. public override string ToString()
  737. {
  738. return ParameterName;
  739. }
  740. private object SqlTypeToFrameworkType (object value)
  741. {
  742. if (! (value is INullable)) // if the value is not SqlType
  743. return value;
  744. // Map to .net type, as Mono TDS respects only types from .net
  745. switch (value.GetType ().FullName) {
  746. case "System.Data.SqlTypes.SqlBinary":
  747. return ( (SqlBinary) value).Value;
  748. case "System.Data.SqlTypes.SqlBoolean":
  749. return ( (SqlBoolean) value).Value;
  750. case "System.Data.SqlTypes.SqlByte":
  751. return ( (SqlByte) value).Value;
  752. case "System.Data.SqlTypes.SqlDateTime":
  753. return ( (SqlDateTime) value).Value;
  754. case "System.Data.SqlTypes.SqlDecimal":
  755. return ( (SqlDecimal) value).Value;
  756. case "System.Data.SqlTypes.SqlDouble":
  757. return ( (SqlDouble) value).Value;
  758. case "System.Data.SqlTypes.SqlGuid":
  759. return ( (SqlGuid) value).Value;
  760. case "System.Data.SqlTypes.SqlInt16":
  761. return ( (SqlInt16) value).Value;
  762. case "System.Data.SqlTypes.SqlInt32 ":
  763. return ( (SqlInt32 ) value).Value;
  764. case "System.Data.SqlTypes.SqlInt64":
  765. return ( (SqlInt64) value).Value;
  766. case "System.Data.SqlTypes.SqlMoney":
  767. return ( (SqlMoney) value).Value;
  768. case "System.Data.SqlTypes.SqlSingle":
  769. return ( (SqlSingle) value).Value;
  770. case "System.Data.SqlTypes.SqlString":
  771. return ( (SqlString) value).Value;
  772. }
  773. return value;
  774. }
  775. #if NET_2_0
  776. [MonoTODO]
  777. public override void CopyTo (DbParameter param)
  778. {
  779. throw new NotImplementedException ();
  780. }
  781. [MonoTODO]
  782. public override void ResetDbType ()
  783. {
  784. throw new NotImplementedException ();
  785. }
  786. #endif // NET_2_0
  787. #endregion // Methods
  788. }
  789. }