SqlParameter.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #if ONLY_1_0 || ONLY_1_1
  210. [Browsable (false)]
  211. [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
  212. [DefaultValue (false)]
  213. [DesignOnly (true)]
  214. [EditorBrowsable (EditorBrowsableState.Advanced)]
  215. #endif
  216. public
  217. #if NET_2_0
  218. override
  219. #endif // NET_2_0
  220. bool IsNullable {
  221. get { return metaParameter.IsNullable; }
  222. set { metaParameter.IsNullable = value; }
  223. }
  224. [Browsable (false)]
  225. [DataCategory ("Data")]
  226. #if ONLY_1_0 || ONLY_1_1
  227. [DataSysDescription ("Offset in variable length data types.")]
  228. [DefaultValue (0)]
  229. #endif
  230. #if NET_2_0
  231. [EditorBrowsable (EditorBrowsableState.Advanced)]
  232. #endif
  233. public
  234. #if NET_2_0
  235. override
  236. #endif
  237. int Offset {
  238. get { return offset; }
  239. set { offset = value; }
  240. }
  241. #if ONLY_1_0 || ONLY_1_1
  242. [DataSysDescription ("Name of the parameter, like '@p1'")]
  243. [DefaultValue ("")]
  244. #endif
  245. public
  246. #if NET_2_0
  247. override
  248. #endif // NET_2_0
  249. string ParameterName {
  250. get { return metaParameter.ParameterName; }
  251. set { metaParameter.ParameterName = value; }
  252. }
  253. [DataCategory ("Data")]
  254. #if ONLY_1_0 || ONLY_1_1
  255. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  256. [DefaultValue (0)]
  257. #endif
  258. #if NET_2_0
  259. [Browsable (false)]
  260. [EditorBrowsable (EditorBrowsableState.Never)]
  261. #endif
  262. public
  263. #if NET_2_0
  264. override
  265. #endif // NET_2_0
  266. byte Precision {
  267. get { return metaParameter.Precision; }
  268. set { metaParameter.Precision = value; }
  269. }
  270. [DataCategory ("Data")]
  271. #if ONLY_1_0 || ONLY_1_1
  272. [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
  273. [DefaultValue (0)]
  274. #endif
  275. #if NET_2_0
  276. [Browsable (false)]
  277. [EditorBrowsable (EditorBrowsableState.Never)]
  278. #endif
  279. public
  280. #if NET_2_0
  281. override
  282. #endif // NET_2_0
  283. byte Scale {
  284. get { return metaParameter.Scale; }
  285. set { metaParameter.Scale = value; }
  286. }
  287. [DataCategory ("Data")]
  288. #if ONLY_1_0 || ONLY_1_1
  289. [DataSysDescription ("Size of variable length data types (string & arrays).")]
  290. [DefaultValue (0)]
  291. #endif
  292. public
  293. #if NET_2_0
  294. override
  295. #endif // NET_2_0
  296. int Size {
  297. get { return metaParameter.Size; }
  298. set { metaParameter.Size = value; }
  299. }
  300. [DataCategory ("Data")]
  301. #if ONLY_1_0 || ONLY_1_1
  302. [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.")]
  303. [DefaultValue ("")]
  304. #endif
  305. public
  306. #if NET_2_0
  307. override
  308. #endif // NET_2_0
  309. string SourceColumn {
  310. get { return sourceColumn; }
  311. set { sourceColumn = value; }
  312. }
  313. [DataCategory ("Data")]
  314. #if ONLY_1_0 || ONLY_1_1
  315. [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
  316. [DefaultValue (DataRowVersion.Current)]
  317. #endif
  318. public
  319. #if NET_2_0
  320. override
  321. #endif // NET_2_0
  322. DataRowVersion SourceVersion {
  323. get { return sourceVersion; }
  324. set { sourceVersion = value; }
  325. }
  326. [DataCategory ("Data")]
  327. #if ONLY_1_0 || ONLY_1_1
  328. [DataSysDescription ("The parameter native type.")]
  329. [DefaultValue (SqlDbType.NVarChar)]
  330. #endif
  331. [RefreshProperties (RefreshProperties.All)]
  332. #if NET_2_0
  333. [DbProviderSpecificTypeProperty(true)]
  334. #endif
  335. public SqlDbType SqlDbType {
  336. get { return sqlDbType; }
  337. set {
  338. SetSqlDbType (value);
  339. isTypeSet = true;
  340. }
  341. }
  342. [DataCategory ("Data")]
  343. #if ONLY_1_0 || ONLY_1_1
  344. [DataSysDescription ("Value of the parameter.")]
  345. [DefaultValue (null)]
  346. #endif
  347. [TypeConverterAttribute (typeof (StringConverter))]
  348. #if NET_2_0
  349. [RefreshProperties (RefreshProperties.All)]
  350. #endif
  351. public
  352. #if NET_2_0
  353. override
  354. #endif // NET_2_0
  355. object Value {
  356. get { return metaParameter.Value; }
  357. set {
  358. if (!isTypeSet)
  359. InferSqlType (value);
  360. metaParameter.Value = SqlTypeToFrameworkType (value);
  361. }
  362. }
  363. //#if NET_2_0
  364. // public SqlCompareOptions CompareInfo{
  365. //#endif
  366. #if NET_2_0
  367. public override bool SourceColumnNullMapping {
  368. get { return false ; }
  369. set { }
  370. }
  371. #endif
  372. #endregion // Properties
  373. #region Methods
  374. object ICloneable.Clone ()
  375. {
  376. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  377. }
  378. // If the value is set without the DbType/SqlDbType being set, then we
  379. // infer type information.
  380. private void InferSqlType (object value)
  381. {
  382. if (value == null || value == DBNull.Value)
  383. return;
  384. Type type = value.GetType ();
  385. string exception = String.Format ("The parameter data type of {0} is invalid.", type.Name);
  386. switch (type.FullName) {
  387. case "System.Int64":
  388. case "System.Data.SqlTypes.SqlInt64":
  389. SetSqlDbType (SqlDbType.BigInt);
  390. break;
  391. case "System.Boolean":
  392. case "System.Data.SqlTypes.SqlBoolean":
  393. SetSqlDbType (SqlDbType.Bit);
  394. break;
  395. case "System.String":
  396. case "System.Data.SqlTypes.SqlString":
  397. SetSqlDbType (SqlDbType.NVarChar);
  398. break;
  399. case "System.DateTime":
  400. case "System.Data.SqlTypes.SqlDateTime":
  401. SetSqlDbType (SqlDbType.DateTime);
  402. break;
  403. case "System.Decimal":
  404. case "System.Data.SqlTypes.SqlDecimal":
  405. SetSqlDbType (SqlDbType.Decimal);
  406. break;
  407. case "System.Double":
  408. case "System.Data.SqlTypes.SqlDouble":
  409. SetSqlDbType (SqlDbType.Float);
  410. break;
  411. case "System.Byte[]":
  412. case "System.Data.SqlTypes.SqlBinary":
  413. SetSqlDbType (SqlDbType.VarBinary);
  414. break;
  415. case "System.Byte":
  416. case "System.Data.SqlTypes.SqlByte":
  417. SetSqlDbType (SqlDbType.TinyInt);
  418. break;
  419. case "System.Int32":
  420. case "System.Data.SqlTypes.SqlInt32":
  421. SetSqlDbType (SqlDbType.Int);
  422. break;
  423. case "System.Single":
  424. case "System.Data.SqlTypes.Single":
  425. SetSqlDbType (SqlDbType.Real);
  426. break;
  427. case "System.Int16":
  428. case "System.Data.SqlTypes.SqlInt16":
  429. SetSqlDbType (SqlDbType.SmallInt);
  430. break;
  431. case "System.Guid":
  432. case "System.Data.SqlTypes.SqlGuid":
  433. SetSqlDbType (SqlDbType.UniqueIdentifier);
  434. break;
  435. case "System.Money":
  436. case "System.SmallMoney":
  437. case "System.Data.SqlTypes.SqlMoney":
  438. SetSqlDbType (SqlDbType.Money);
  439. break;
  440. case "System.Object":
  441. SetSqlDbType (SqlDbType.Variant);
  442. break;
  443. default:
  444. throw new ArgumentException (exception);
  445. }
  446. }
  447. // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
  448. // string representation of the type name. If the DbType is not convertible
  449. // to an SqlDbType, throw an exception.
  450. private void SetDbType (DbType type)
  451. {
  452. string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
  453. switch (type) {
  454. case DbType.AnsiString:
  455. MetaParameter.TypeName = "varchar";
  456. sqlDbType = SqlDbType.VarChar;
  457. isVariableSizeType = true;
  458. break;
  459. case DbType.AnsiStringFixedLength:
  460. MetaParameter.TypeName = "char";
  461. sqlDbType = SqlDbType.Char;
  462. isVariableSizeType = true;
  463. break;
  464. case DbType.Binary:
  465. MetaParameter.TypeName = "varbinary";
  466. sqlDbType = SqlDbType.VarBinary;
  467. isVariableSizeType = true;
  468. break;
  469. case DbType.Boolean:
  470. MetaParameter.TypeName = "bit";
  471. sqlDbType = SqlDbType.Bit;
  472. break;
  473. case DbType.Byte:
  474. MetaParameter.TypeName = "tinyint";
  475. sqlDbType = SqlDbType.TinyInt;
  476. break;
  477. case DbType.Currency:
  478. sqlDbType = SqlDbType.Money;
  479. MetaParameter.TypeName = "money";
  480. break;
  481. case DbType.Date:
  482. case DbType.DateTime:
  483. MetaParameter.TypeName = "datetime";
  484. sqlDbType = SqlDbType.DateTime;
  485. break;
  486. case DbType.Decimal:
  487. MetaParameter.TypeName = "decimal";
  488. sqlDbType = SqlDbType.Decimal;
  489. break;
  490. case DbType.Double:
  491. MetaParameter.TypeName = "float";
  492. sqlDbType = SqlDbType.Float;
  493. break;
  494. case DbType.Guid:
  495. MetaParameter.TypeName = "uniqueidentifier";
  496. sqlDbType = SqlDbType.UniqueIdentifier;
  497. break;
  498. case DbType.Int16:
  499. MetaParameter.TypeName = "smallint";
  500. sqlDbType = SqlDbType.SmallInt;
  501. break;
  502. case DbType.Int32:
  503. MetaParameter.TypeName = "int";
  504. sqlDbType = SqlDbType.Int;
  505. break;
  506. case DbType.Int64:
  507. MetaParameter.TypeName = "bigint";
  508. sqlDbType = SqlDbType.BigInt;
  509. break;
  510. case DbType.Object:
  511. MetaParameter.TypeName = "sql_variant";
  512. sqlDbType = SqlDbType.Variant;
  513. break;
  514. case DbType.Single:
  515. MetaParameter.TypeName = "real";
  516. sqlDbType = SqlDbType.Real;
  517. break;
  518. case DbType.String:
  519. MetaParameter.TypeName = "nvarchar";
  520. sqlDbType = SqlDbType.NVarChar;
  521. isVariableSizeType = true;
  522. break;
  523. case DbType.StringFixedLength:
  524. MetaParameter.TypeName = "nchar";
  525. sqlDbType = SqlDbType.NChar;
  526. isVariableSizeType = true;
  527. break;
  528. case DbType.Time:
  529. MetaParameter.TypeName = "datetime";
  530. sqlDbType = SqlDbType.DateTime;
  531. break;
  532. default:
  533. throw new ArgumentException (exception);
  534. }
  535. dbType = type;
  536. }
  537. // Used by internal constructor which has a SQL Server typename
  538. private void SetDbTypeName (string dbTypeName)
  539. {
  540. switch (dbTypeName.ToLower ()) {
  541. case "bigint":
  542. SqlDbType = SqlDbType.BigInt;
  543. break;
  544. case "binary":
  545. SqlDbType = SqlDbType.Binary;
  546. break;
  547. case "bit":
  548. SqlDbType = SqlDbType.Bit;
  549. break;
  550. case "char":
  551. SqlDbType = SqlDbType.Char;
  552. break;
  553. case "datetime":
  554. SqlDbType = SqlDbType.DateTime;
  555. break;
  556. case "decimal":
  557. SqlDbType = SqlDbType.Decimal;
  558. break;
  559. case "float":
  560. SqlDbType = SqlDbType.Float;
  561. break;
  562. case "image":
  563. SqlDbType = SqlDbType.Image;
  564. break;
  565. case "int":
  566. SqlDbType = SqlDbType.Int;
  567. break;
  568. case "money":
  569. SqlDbType = SqlDbType.Money;
  570. break;
  571. case "nchar":
  572. SqlDbType = SqlDbType.NChar;
  573. break;
  574. case "ntext":
  575. SqlDbType = SqlDbType.NText;
  576. break;
  577. case "nvarchar":
  578. SqlDbType = SqlDbType.NVarChar;
  579. break;
  580. case "real":
  581. SqlDbType = SqlDbType.Real;
  582. break;
  583. case "smalldatetime":
  584. SqlDbType = SqlDbType.SmallDateTime;
  585. break;
  586. case "smallint":
  587. SqlDbType = SqlDbType.SmallInt;
  588. break;
  589. case "smallmoney":
  590. SqlDbType = SqlDbType.SmallMoney;
  591. break;
  592. case "text":
  593. SqlDbType = SqlDbType.Text;
  594. break;
  595. case "timestamp":
  596. SqlDbType = SqlDbType.Timestamp;
  597. break;
  598. case "tinyint":
  599. SqlDbType = SqlDbType.TinyInt;
  600. break;
  601. case "uniqueidentifier":
  602. SqlDbType = SqlDbType.UniqueIdentifier;
  603. break;
  604. case "varbinary":
  605. SqlDbType = SqlDbType.VarBinary;
  606. break;
  607. case "varchar":
  608. SqlDbType = SqlDbType.VarChar;
  609. break;
  610. default:
  611. SqlDbType = SqlDbType.Variant;
  612. break;
  613. }
  614. }
  615. // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
  616. // string representation of the type name. If the SqlDbType is not convertible
  617. // to a DbType, throw an exception.
  618. private void SetSqlDbType (SqlDbType type)
  619. {
  620. string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
  621. switch (type) {
  622. case SqlDbType.BigInt:
  623. MetaParameter.TypeName = "bigint";
  624. dbType = DbType.Int64;
  625. break;
  626. case SqlDbType.Binary:
  627. MetaParameter.TypeName = "binary";
  628. dbType = DbType.Binary;
  629. isVariableSizeType = true;
  630. break;
  631. case SqlDbType.Timestamp:
  632. MetaParameter.TypeName = "timestamp";
  633. dbType = DbType.Binary;
  634. break;
  635. case SqlDbType.VarBinary:
  636. MetaParameter.TypeName = "varbinary";
  637. dbType = DbType.Binary;
  638. isVariableSizeType = true;
  639. break;
  640. case SqlDbType.Bit:
  641. MetaParameter.TypeName = "bit";
  642. dbType = DbType.Boolean;
  643. break;
  644. case SqlDbType.Char:
  645. MetaParameter.TypeName = "char";
  646. dbType = DbType.AnsiStringFixedLength;
  647. isVariableSizeType = true;
  648. break;
  649. case SqlDbType.DateTime:
  650. MetaParameter.TypeName = "datetime";
  651. dbType = DbType.DateTime;
  652. break;
  653. case SqlDbType.SmallDateTime:
  654. MetaParameter.TypeName = "smalldatetime";
  655. dbType = DbType.DateTime;
  656. break;
  657. case SqlDbType.Decimal:
  658. MetaParameter.TypeName = "decimal";
  659. dbType = DbType.Decimal;
  660. break;
  661. case SqlDbType.Float:
  662. MetaParameter.TypeName = "float";
  663. dbType = DbType.Double;
  664. break;
  665. case SqlDbType.Image:
  666. MetaParameter.TypeName = "image";
  667. dbType = DbType.Binary;
  668. isVariableSizeType = true;
  669. break;
  670. case SqlDbType.Int:
  671. MetaParameter.TypeName = "int";
  672. dbType = DbType.Int32;
  673. break;
  674. case SqlDbType.Money:
  675. MetaParameter.TypeName = "money";
  676. dbType = DbType.Currency;
  677. break;
  678. case SqlDbType.SmallMoney:
  679. MetaParameter.TypeName = "smallmoney";
  680. dbType = DbType.Currency;
  681. break;
  682. case SqlDbType.NChar:
  683. MetaParameter.TypeName = "nchar";
  684. dbType = DbType.StringFixedLength;
  685. isVariableSizeType = true;
  686. break;
  687. case SqlDbType.NText:
  688. MetaParameter.TypeName = "ntext";
  689. dbType = DbType.String;
  690. isVariableSizeType = true;
  691. break;
  692. case SqlDbType.NVarChar:
  693. MetaParameter.TypeName = "nvarchar";
  694. dbType = DbType.String;
  695. isVariableSizeType = true;
  696. break;
  697. case SqlDbType.Real:
  698. MetaParameter.TypeName = "real";
  699. dbType = DbType.Single;
  700. break;
  701. case SqlDbType.SmallInt:
  702. MetaParameter.TypeName = "smallint";
  703. dbType = DbType.Int16;
  704. break;
  705. case SqlDbType.Text:
  706. MetaParameter.TypeName = "text";
  707. dbType = DbType.AnsiString;
  708. isVariableSizeType = true;
  709. break;
  710. case SqlDbType.VarChar:
  711. MetaParameter.TypeName = "varchar";
  712. dbType = DbType.AnsiString;
  713. isVariableSizeType = true;
  714. break;
  715. case SqlDbType.TinyInt:
  716. MetaParameter.TypeName = "tinyint";
  717. dbType = DbType.Byte;
  718. break;
  719. case SqlDbType.UniqueIdentifier:
  720. MetaParameter.TypeName = "uniqueidentifier";
  721. dbType = DbType.Guid;
  722. break;
  723. case SqlDbType.Variant:
  724. MetaParameter.TypeName = "sql_variant";
  725. dbType = DbType.Object;
  726. break;
  727. default:
  728. throw new ArgumentException (exception);
  729. }
  730. sqlDbType = type;
  731. }
  732. public override string ToString()
  733. {
  734. return ParameterName;
  735. }
  736. private object SqlTypeToFrameworkType (object value)
  737. {
  738. if (! (value is INullable)) // if the value is not SqlType
  739. return value;
  740. // Map to .net type, as Mono TDS respects only types from .net
  741. switch (value.GetType ().FullName) {
  742. case "System.Data.SqlTypes.SqlBinary":
  743. return ( (SqlBinary) value).Value;
  744. case "System.Data.SqlTypes.SqlBoolean":
  745. return ( (SqlBoolean) value).Value;
  746. case "System.Data.SqlTypes.SqlByte":
  747. return ( (SqlByte) value).Value;
  748. case "System.Data.SqlTypes.SqlDateTime":
  749. return ( (SqlDateTime) value).Value;
  750. case "System.Data.SqlTypes.SqlDecimal":
  751. return ( (SqlDecimal) value).Value;
  752. case "System.Data.SqlTypes.SqlDouble":
  753. return ( (SqlDouble) value).Value;
  754. case "System.Data.SqlTypes.SqlGuid":
  755. return ( (SqlGuid) value).Value;
  756. case "System.Data.SqlTypes.SqlInt16":
  757. return ( (SqlInt16) value).Value;
  758. case "System.Data.SqlTypes.SqlInt32 ":
  759. return ( (SqlInt32 ) value).Value;
  760. case "System.Data.SqlTypes.SqlInt64":
  761. return ( (SqlInt64) value).Value;
  762. case "System.Data.SqlTypes.SqlMoney":
  763. return ( (SqlMoney) value).Value;
  764. case "System.Data.SqlTypes.SqlSingle":
  765. return ( (SqlSingle) value).Value;
  766. case "System.Data.SqlTypes.SqlString":
  767. return ( (SqlString) value).Value;
  768. }
  769. return value;
  770. }
  771. #if NET_2_0
  772. [MonoTODO]
  773. public override void CopyTo (DbParameter param)
  774. {
  775. throw new NotImplementedException ();
  776. }
  777. [MonoTODO]
  778. public override void ResetDbType ()
  779. {
  780. throw new NotImplementedException ();
  781. }
  782. #endif // NET_2_0
  783. #endregion // Methods
  784. }
  785. }