SqlParameter.cs 22 KB

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