SqlParameter.cs 22 KB

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