SqlParameter.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. // Amit Biswas ([email protected])
  11. // Veerapuram Varadhan ([email protected])
  12. //
  13. // (C) Ximian, Inc. 2002
  14. // Copyright (C) Tim Coleman, 2002
  15. //
  16. //
  17. // Copyright (C) 2004, 2008, 2009 Novell, Inc (http://www.novell.com)
  18. //
  19. // Permission is hereby granted, free of charge, to any person obtaining
  20. // a copy of this software and associated documentation files (the
  21. // "Software"), to deal in the Software without restriction, including
  22. // without limitation the rights to use, copy, modify, merge, publish,
  23. // distribute, sublicense, and/or sell copies of the Software, and to
  24. // permit persons to whom the Software is furnished to do so, subject to
  25. // the following conditions:
  26. //
  27. // The above copyright notice and this permission notice shall be
  28. // included in all copies or substantial portions of the Software.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. using Mono.Data.Tds;
  39. using Mono.Data.Tds.Protocol;
  40. using System;
  41. using System.Collections;
  42. using System.ComponentModel;
  43. using System.Data;
  44. using System.Data.Common;
  45. using System.Data.SqlTypes;
  46. using System.Globalization;
  47. using System.Runtime.InteropServices;
  48. using System.Text;
  49. using System.Xml;
  50. namespace System.Data.SqlClient {
  51. [TypeConverterAttribute ("System.Data.SqlClient.SqlParameter+SqlParameterConverter, " + Consts.AssemblySystem_Data)]
  52. public sealed class SqlParameter : DbParameter, IDbDataParameter, IDataParameter, ICloneable
  53. {
  54. #region Import from old DbParameter
  55. static Hashtable dbTypeMapping = new Hashtable ();
  56. internal static Hashtable DbTypeMapping {
  57. get { return dbTypeMapping;}
  58. set { dbTypeMapping = value;}
  59. }
  60. // LAMESPEC: Implementors should populate the dbTypeMapping accordingly
  61. internal Type SystemType {
  62. get {
  63. return (Type) dbTypeMapping [SqlDbType];
  64. }
  65. }
  66. #endregion
  67. #region Fields
  68. TdsMetaParameter metaParameter;
  69. SqlParameterCollection container;
  70. DbType dbType;
  71. ParameterDirection direction = ParameterDirection.Input;
  72. bool isTypeSet;
  73. int offset;
  74. SqlDbType sqlDbType;
  75. string sourceColumn;
  76. DataRowVersion sourceVersion;
  77. SqlCompareOptions compareInfo;
  78. int localeId;
  79. Type sqlType;
  80. bool typeChanged;
  81. bool sourceColumnNullMapping;
  82. string xmlSchemaCollectionDatabase = String.Empty;
  83. string xmlSchemaCollectionOwningSchema = String.Empty;
  84. string xmlSchemaCollectionName = String.Empty;
  85. static Hashtable type_mapping;
  86. #endregion // Fields
  87. #region Constructors
  88. static SqlParameter ()
  89. {
  90. if (DbTypeMapping == null)
  91. DbTypeMapping = new Hashtable ();
  92. DbTypeMapping.Add (SqlDbType.BigInt, typeof (long));
  93. DbTypeMapping.Add (SqlDbType.Bit, typeof (bool));
  94. DbTypeMapping.Add (SqlDbType.Char, typeof (string));
  95. DbTypeMapping.Add (SqlDbType.NChar, typeof (string));
  96. DbTypeMapping.Add (SqlDbType.Text, typeof (string));
  97. DbTypeMapping.Add (SqlDbType.NText, typeof (string));
  98. DbTypeMapping.Add (SqlDbType.VarChar, typeof (string));
  99. DbTypeMapping.Add (SqlDbType.NVarChar, typeof (string));
  100. DbTypeMapping.Add (SqlDbType.SmallDateTime, typeof (DateTime));
  101. DbTypeMapping.Add (SqlDbType.DateTime, typeof (DateTime));
  102. DbTypeMapping.Add (SqlDbType.DateTime2, typeof (DateTime));
  103. DbTypeMapping.Add (SqlDbType.DateTimeOffset, typeof (DateTimeOffset));
  104. DbTypeMapping.Add (SqlDbType.Decimal, typeof (decimal));
  105. DbTypeMapping.Add (SqlDbType.Float, typeof (double));
  106. DbTypeMapping.Add (SqlDbType.Binary, typeof (byte []));
  107. DbTypeMapping.Add (SqlDbType.Image, typeof (byte []));
  108. DbTypeMapping.Add (SqlDbType.Money, typeof (decimal));
  109. DbTypeMapping.Add (SqlDbType.SmallMoney, typeof (decimal));
  110. DbTypeMapping.Add (SqlDbType.VarBinary, typeof (byte []));
  111. DbTypeMapping.Add (SqlDbType.TinyInt, typeof (byte));
  112. DbTypeMapping.Add (SqlDbType.Int, typeof (int));
  113. DbTypeMapping.Add (SqlDbType.Real, typeof (float));
  114. DbTypeMapping.Add (SqlDbType.SmallInt, typeof (short));
  115. DbTypeMapping.Add (SqlDbType.UniqueIdentifier, typeof (Guid));
  116. DbTypeMapping.Add (SqlDbType.Variant, typeof (object));
  117. DbTypeMapping.Add (SqlDbType.Xml, typeof (string));
  118. type_mapping = new Hashtable ();
  119. type_mapping.Add (typeof (long), SqlDbType.BigInt);
  120. type_mapping.Add (typeof (SqlTypes.SqlInt64), SqlDbType.BigInt);
  121. type_mapping.Add (typeof (bool), SqlDbType.Bit);
  122. type_mapping.Add (typeof (SqlTypes.SqlBoolean), SqlDbType.Bit);
  123. type_mapping.Add (typeof (char), SqlDbType.NVarChar);
  124. type_mapping.Add (typeof (char []), SqlDbType.NVarChar);
  125. type_mapping.Add (typeof (SqlTypes.SqlChars), SqlDbType.NVarChar);
  126. type_mapping.Add (typeof (string), SqlDbType.NVarChar);
  127. type_mapping.Add (typeof (SqlTypes.SqlString), SqlDbType.NVarChar);
  128. type_mapping.Add (typeof (DateTime), SqlDbType.DateTime);
  129. type_mapping.Add (typeof (SqlTypes.SqlDateTime), SqlDbType.DateTime);
  130. type_mapping.Add (typeof (decimal), SqlDbType.Decimal);
  131. type_mapping.Add (typeof (SqlTypes.SqlDecimal), SqlDbType.Decimal);
  132. type_mapping.Add (typeof (double), SqlDbType.Float);
  133. type_mapping.Add (typeof (SqlTypes.SqlDouble), SqlDbType.Float);
  134. type_mapping.Add (typeof (byte []), SqlDbType.VarBinary);
  135. type_mapping.Add (typeof (SqlTypes.SqlBinary), SqlDbType.VarBinary);
  136. type_mapping.Add (typeof (SqlTypes.SqlBytes), SqlDbType.VarBinary);
  137. type_mapping.Add (typeof (byte), SqlDbType.TinyInt);
  138. type_mapping.Add (typeof (SqlTypes.SqlByte), SqlDbType.TinyInt);
  139. type_mapping.Add (typeof (int), SqlDbType.Int);
  140. type_mapping.Add (typeof (SqlTypes.SqlInt32), SqlDbType.Int);
  141. type_mapping.Add (typeof (float), SqlDbType.Real);
  142. type_mapping.Add (typeof (SqlTypes.SqlSingle), SqlDbType.Real);
  143. type_mapping.Add (typeof (short), SqlDbType.SmallInt);
  144. type_mapping.Add (typeof (SqlTypes.SqlInt16), SqlDbType.SmallInt);
  145. type_mapping.Add (typeof (Guid), SqlDbType.UniqueIdentifier);
  146. type_mapping.Add (typeof (SqlTypes.SqlGuid), SqlDbType.UniqueIdentifier);
  147. type_mapping.Add (typeof (SqlTypes.SqlMoney), SqlDbType.Money);
  148. type_mapping.Add (typeof (XmlReader), SqlDbType.Xml);
  149. type_mapping.Add (typeof (SqlTypes.SqlXml), SqlDbType.Xml);
  150. type_mapping.Add (typeof (object), SqlDbType.Variant);
  151. type_mapping.Add (typeof (DateTimeOffset), SqlDbType.DateTimeOffset);
  152. }
  153. public SqlParameter ()
  154. : this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
  155. {
  156. isTypeSet = false;
  157. }
  158. public SqlParameter (string parameterName, object value)
  159. {
  160. if (parameterName == null)
  161. parameterName = string.Empty;
  162. metaParameter = new TdsMetaParameter (parameterName, GetFrameworkValue);
  163. metaParameter.RawValue = value;
  164. InferSqlType (value);
  165. sourceVersion = DataRowVersion.Current;
  166. }
  167. public SqlParameter (string parameterName, SqlDbType dbType)
  168. : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, null)
  169. {
  170. }
  171. public SqlParameter (string parameterName, SqlDbType dbType, int size)
  172. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, null)
  173. {
  174. }
  175. public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn)
  176. : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
  177. {
  178. }
  179. [EditorBrowsable (EditorBrowsableState.Advanced)]
  180. public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value)
  181. {
  182. if (parameterName == null)
  183. parameterName = string.Empty;
  184. metaParameter = new TdsMetaParameter (parameterName, size,
  185. isNullable, precision,
  186. scale,
  187. GetFrameworkValue);
  188. metaParameter.RawValue = value;
  189. SqlDbType = dbType;
  190. Direction = direction;
  191. SourceColumn = sourceColumn;
  192. SourceVersion = sourceVersion;
  193. }
  194. public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, bool sourceColumnNullMapping, Object value, string xmlSchemaCollectionDatabase, string xmlSchemaCollectionOwningSchema, string xmlSchemaCollectionName)
  195. : this (parameterName, dbType, size, direction, false, precision, scale, sourceColumn, sourceVersion, value)
  196. {
  197. XmlSchemaCollectionDatabase = xmlSchemaCollectionDatabase;
  198. XmlSchemaCollectionOwningSchema = xmlSchemaCollectionOwningSchema;
  199. XmlSchemaCollectionName = xmlSchemaCollectionName;
  200. SourceColumnNullMapping = sourceColumnNullMapping;
  201. }
  202. // This constructor is used internally to construct a
  203. // SqlParameter. The value array comes from sp_procedure_params_rowset.
  204. // This is in SqlCommand.DeriveParameters.
  205. //
  206. // http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/900756fd-3980-48e3-ae59-a15d7fc15b4c/
  207. internal SqlParameter (object[] dbValues)
  208. : this (dbValues [3].ToString (), (object) null)
  209. {
  210. ParameterName = (string) dbValues [3];
  211. switch ((short) dbValues [5]) {
  212. case 1:
  213. Direction = ParameterDirection.Input;
  214. break;
  215. case 2:
  216. Direction = ParameterDirection.InputOutput;
  217. break;
  218. case 3:
  219. Direction = ParameterDirection.Output;
  220. break;
  221. case 4:
  222. Direction = ParameterDirection.ReturnValue;
  223. break;
  224. default:
  225. Direction = ParameterDirection.Input;
  226. break;
  227. }
  228. SqlDbType = (SqlDbType) FrameworkDbTypeFromName ((string) dbValues [16]);
  229. if (MetaParameter.IsVariableSizeType) {
  230. if (dbValues [10] != DBNull.Value)
  231. Size = (int) dbValues [10];
  232. }
  233. if (SqlDbType == SqlDbType.Decimal) {
  234. if (dbValues [12] != null && dbValues [12] != DBNull.Value)
  235. Precision = (byte) ((short) dbValues [12]);
  236. if (dbValues [13] != null && dbValues [13] != DBNull.Value)
  237. Scale = (byte) ((short) dbValues [13]);
  238. }
  239. }
  240. #endregion // Constructors
  241. #region Properties
  242. // Used to ensure that only one collection can contain this
  243. // parameter
  244. internal SqlParameterCollection Container {
  245. get { return container; }
  246. set { container = value; }
  247. }
  248. internal void CheckIfInitialized ()
  249. {
  250. if (!isTypeSet)
  251. throw new Exception ("all parameters to have an explicity set type");
  252. if (MetaParameter.IsVariableSizeType) {
  253. if (SqlDbType == SqlDbType.Decimal && Precision == 0)
  254. throw new Exception ("Parameter of type 'Decimal' have an explicitly set Precision and Scale");
  255. else if (Size == 0)
  256. throw new Exception ("all variable length parameters to have an explicitly set non-zero Size");
  257. }
  258. }
  259. public override DbType DbType {
  260. get { return dbType; }
  261. set {
  262. SetDbType (value);
  263. typeChanged = true;
  264. isTypeSet = true;
  265. }
  266. }
  267. [RefreshProperties (RefreshProperties.All)]
  268. public override
  269. ParameterDirection Direction {
  270. get { return direction; }
  271. set {
  272. direction = value;
  273. switch( direction ) {
  274. case ParameterDirection.Output:
  275. MetaParameter.Direction = TdsParameterDirection.Output;
  276. break;
  277. case ParameterDirection.InputOutput:
  278. MetaParameter.Direction = TdsParameterDirection.InputOutput;
  279. break;
  280. case ParameterDirection.ReturnValue:
  281. MetaParameter.Direction = TdsParameterDirection.ReturnValue;
  282. break;
  283. }
  284. }
  285. }
  286. internal TdsMetaParameter MetaParameter {
  287. get { return metaParameter; }
  288. }
  289. public override bool IsNullable {
  290. get { return metaParameter.IsNullable; }
  291. set { metaParameter.IsNullable = value; }
  292. }
  293. [Browsable (false)]
  294. [EditorBrowsable (EditorBrowsableState.Advanced)]
  295. public int Offset {
  296. get { return offset; }
  297. set { offset = value; }
  298. }
  299. public override string ParameterName {
  300. get { return metaParameter.ParameterName; }
  301. set {
  302. if (value == null)
  303. value = string.Empty;
  304. metaParameter.ParameterName = value;
  305. }
  306. }
  307. [DefaultValue (0)]
  308. public byte Precision {
  309. get { return metaParameter.Precision; }
  310. set { metaParameter.Precision = value; }
  311. }
  312. [DefaultValue (0)]
  313. public byte Scale {
  314. get { return metaParameter.Scale; }
  315. set { metaParameter.Scale = value; }
  316. }
  317. public override int Size {
  318. get { return metaParameter.Size; }
  319. set { metaParameter.Size = value; }
  320. }
  321. public override string SourceColumn {
  322. get {
  323. if (sourceColumn == null)
  324. return string.Empty;
  325. return sourceColumn;
  326. }
  327. set { sourceColumn = value; }
  328. }
  329. public override DataRowVersion SourceVersion {
  330. get { return sourceVersion; }
  331. set { sourceVersion = value; }
  332. }
  333. [RefreshProperties (RefreshProperties.All)]
  334. [DbProviderSpecificTypeProperty(true)]
  335. public SqlDbType SqlDbType {
  336. get { return sqlDbType; }
  337. set {
  338. SetSqlDbType (value);
  339. typeChanged = true;
  340. isTypeSet = true;
  341. }
  342. }
  343. [TypeConverterAttribute (typeof (StringConverter))]
  344. [RefreshProperties (RefreshProperties.All)]
  345. public override object Value {
  346. get {
  347. if (sqlType != null)
  348. return GetSqlValue (metaParameter.RawValue);
  349. return metaParameter.RawValue;
  350. }
  351. set {
  352. if (!isTypeSet) {
  353. InferSqlType (value);
  354. }
  355. if (value is INullable) {
  356. sqlType = value.GetType ();
  357. value = SqlTypeToFrameworkType (value);
  358. }
  359. metaParameter.RawValue = value;
  360. }
  361. }
  362. [Browsable (false)]
  363. public SqlCompareOptions CompareInfo{
  364. get{ return compareInfo; }
  365. set{ compareInfo = value; }
  366. }
  367. [Browsable (false)]
  368. public int LocaleId {
  369. get { return localeId; }
  370. set { localeId = value; }
  371. }
  372. [Browsable (false)]
  373. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  374. public Object SqlValue {
  375. get {
  376. return GetSqlValue (metaParameter.RawValue);
  377. }
  378. set {
  379. Value = value;
  380. }
  381. }
  382. public override bool SourceColumnNullMapping {
  383. get { return sourceColumnNullMapping; }
  384. set { sourceColumnNullMapping = value; }
  385. }
  386. public string XmlSchemaCollectionDatabase {
  387. get { return xmlSchemaCollectionDatabase; }
  388. set { xmlSchemaCollectionDatabase = (value == null ? String.Empty : value); }
  389. }
  390. public string XmlSchemaCollectionName {
  391. get { return xmlSchemaCollectionName; }
  392. set {
  393. xmlSchemaCollectionName = (value == null ? String.Empty : value);
  394. }
  395. }
  396. public string XmlSchemaCollectionOwningSchema {
  397. get { return xmlSchemaCollectionOwningSchema; }
  398. set {
  399. xmlSchemaCollectionOwningSchema = (value == null ? String.Empty : value);
  400. }
  401. }
  402. [BrowsableAttribute(false)]
  403. public string UdtTypeName { get; set; }
  404. #endregion // Properties
  405. #region Methods
  406. object ICloneable.Clone ()
  407. {
  408. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  409. }
  410. // If the value is set without the DbType/SqlDbType being set, then we
  411. // infer type information.
  412. void InferSqlType (object value)
  413. {
  414. if (value == null || value == DBNull.Value) {
  415. SetSqlDbType (SqlDbType.NVarChar);
  416. return;
  417. }
  418. Type type = value.GetType ();
  419. if (type.IsEnum)
  420. type = Enum.GetUnderlyingType (type);
  421. object t = type_mapping [type];
  422. if (t == null)
  423. throw new ArgumentException (String.Format ("The parameter data type of {0} is invalid.", type.FullName));
  424. SetSqlDbType ((SqlDbType) t);
  425. }
  426. /*
  427. // Returns System.Type corresponding to the underlying SqlDbType
  428. internal override Type SystemType {
  429. get {
  430. return (Type) DbTypeMapping [sqlDbType];
  431. }
  432. }
  433. internal override object FrameworkDbType {
  434. get {
  435. return sqlDbType;
  436. }
  437. set {
  438. object t;
  439. try {
  440. t = (DbType) DbTypeFromName ((string)value);
  441. SetDbType ((DbType)t);
  442. } catch (ArgumentException) {
  443. t = (SqlDbType)FrameworkDbTypeFromName ((string)value);
  444. SetSqlDbType ((SqlDbType) t);
  445. }
  446. }
  447. }
  448. */
  449. DbType DbTypeFromName (string name)
  450. {
  451. switch (name.ToLower ()) {
  452. case "ansistring":
  453. return DbType.AnsiString;
  454. case "ansistringfixedlength":
  455. return DbType.AnsiStringFixedLength;
  456. case "binary":
  457. return DbType.Binary;
  458. case "boolean":
  459. return DbType.Boolean;
  460. case "byte":
  461. return DbType.Byte;
  462. case "currency":
  463. return DbType.Currency;
  464. case "date":
  465. return DbType.Date;
  466. case "datetime":
  467. return DbType.DateTime;
  468. case "decimal":
  469. return DbType.Decimal;
  470. case "double":
  471. return DbType.Double;
  472. case "guid":
  473. return DbType.Guid;
  474. case "int16":
  475. return DbType.Int16;
  476. case "int32":
  477. return DbType.Int32;
  478. case "int64":
  479. return DbType.Int64;
  480. case "object":
  481. return DbType.Object;
  482. case "single":
  483. return DbType.Single;
  484. case "string":
  485. return DbType.String;
  486. case "stringfixedlength":
  487. return DbType.StringFixedLength;
  488. case "time":
  489. return DbType.Time;
  490. case "xml":
  491. return DbType.Xml;
  492. default:
  493. string exception = String.Format ("No mapping exists from {0} to a known DbType.", name);
  494. throw new ArgumentException (exception);
  495. }
  496. }
  497. // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
  498. // string representation of the type name. If the DbType is not convertible
  499. // to an SqlDbType, throw an exception.
  500. private void SetDbType (DbType type)
  501. {
  502. switch (type) {
  503. case DbType.AnsiString:
  504. MetaParameter.TypeName = "varchar";
  505. sqlDbType = SqlDbType.VarChar;
  506. MetaParameter.IsVariableSizeType = true;
  507. break;
  508. case DbType.AnsiStringFixedLength:
  509. MetaParameter.TypeName = "char";
  510. sqlDbType = SqlDbType.Char;
  511. MetaParameter.IsVariableSizeType = true;
  512. break;
  513. case DbType.Binary:
  514. MetaParameter.TypeName = "varbinary";
  515. sqlDbType = SqlDbType.VarBinary;
  516. MetaParameter.IsVariableSizeType = true;
  517. break;
  518. case DbType.Boolean:
  519. MetaParameter.TypeName = "bit";
  520. sqlDbType = SqlDbType.Bit;
  521. break;
  522. case DbType.Byte:
  523. MetaParameter.TypeName = "tinyint";
  524. sqlDbType = SqlDbType.TinyInt;
  525. break;
  526. case DbType.Currency:
  527. sqlDbType = SqlDbType.Money;
  528. MetaParameter.TypeName = "money";
  529. break;
  530. case DbType.Date:
  531. case DbType.DateTime:
  532. MetaParameter.TypeName = "datetime";
  533. sqlDbType = SqlDbType.DateTime;
  534. break;
  535. case DbType.DateTime2:
  536. MetaParameter.TypeName = "datetime2";
  537. sqlDbType = SqlDbType.DateTime2;
  538. break;
  539. case DbType.Decimal:
  540. MetaParameter.TypeName = "decimal";
  541. sqlDbType = SqlDbType.Decimal;
  542. break;
  543. case DbType.Double:
  544. MetaParameter.TypeName = "float";
  545. sqlDbType = SqlDbType.Float;
  546. break;
  547. case DbType.Guid:
  548. MetaParameter.TypeName = "uniqueidentifier";
  549. sqlDbType = SqlDbType.UniqueIdentifier;
  550. break;
  551. case DbType.Int16:
  552. MetaParameter.TypeName = "smallint";
  553. sqlDbType = SqlDbType.SmallInt;
  554. break;
  555. case DbType.Int32:
  556. MetaParameter.TypeName = "int";
  557. sqlDbType = SqlDbType.Int;
  558. break;
  559. case DbType.Int64:
  560. MetaParameter.TypeName = "bigint";
  561. sqlDbType = SqlDbType.BigInt;
  562. break;
  563. case DbType.Object:
  564. MetaParameter.TypeName = "sql_variant";
  565. sqlDbType = SqlDbType.Variant;
  566. break;
  567. case DbType.Single:
  568. MetaParameter.TypeName = "real";
  569. sqlDbType = SqlDbType.Real;
  570. break;
  571. case DbType.String:
  572. MetaParameter.TypeName = "nvarchar";
  573. sqlDbType = SqlDbType.NVarChar;
  574. MetaParameter.IsVariableSizeType = true;
  575. break;
  576. case DbType.StringFixedLength:
  577. MetaParameter.TypeName = "nchar";
  578. sqlDbType = SqlDbType.NChar;
  579. MetaParameter.IsVariableSizeType = true;
  580. break;
  581. case DbType.Time:
  582. MetaParameter.TypeName = "datetime";
  583. sqlDbType = SqlDbType.DateTime;
  584. break;
  585. // Handle Xml type as string
  586. case DbType.Xml:
  587. MetaParameter.TypeName = "xml";
  588. sqlDbType = SqlDbType.Xml;
  589. MetaParameter.IsVariableSizeType = true;
  590. break;
  591. default:
  592. string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
  593. throw new ArgumentException (exception);
  594. }
  595. dbType = type;
  596. }
  597. // Used by internal constructor which has a SQL Server typename
  598. private SqlDbType FrameworkDbTypeFromName (string dbTypeName)
  599. {
  600. switch (dbTypeName.ToLower ()) {
  601. case "bigint":
  602. return SqlDbType.BigInt;
  603. case "binary":
  604. return SqlDbType.Binary;
  605. case "bit":
  606. return SqlDbType.Bit;
  607. case "char":
  608. return SqlDbType.Char;
  609. case "datetime":
  610. return SqlDbType.DateTime;
  611. case "decimal":
  612. return SqlDbType.Decimal;
  613. case "float":
  614. return SqlDbType.Float;
  615. case "image":
  616. return SqlDbType.Image;
  617. case "int":
  618. return SqlDbType.Int;
  619. case "money":
  620. return SqlDbType.Money;
  621. case "nchar":
  622. return SqlDbType.NChar;
  623. case "ntext":
  624. return SqlDbType.NText;
  625. case "nvarchar":
  626. return SqlDbType.NVarChar;
  627. case "real":
  628. return SqlDbType.Real;
  629. case "smalldatetime":
  630. return SqlDbType.SmallDateTime;
  631. case "smallint":
  632. return SqlDbType.SmallInt;
  633. case "smallmoney":
  634. return SqlDbType.SmallMoney;
  635. case "text":
  636. return SqlDbType.Text;
  637. case "timestamp":
  638. return SqlDbType.Timestamp;
  639. case "tinyint":
  640. return SqlDbType.TinyInt;
  641. case "uniqueidentifier":
  642. return SqlDbType.UniqueIdentifier;
  643. case "varbinary":
  644. return SqlDbType.VarBinary;
  645. case "varchar":
  646. return SqlDbType.VarChar;
  647. case "sql_variant":
  648. return SqlDbType.Variant;
  649. case "xml":
  650. return SqlDbType.Xml;
  651. default:
  652. return SqlDbType.Variant;
  653. }
  654. }
  655. // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
  656. // string representation of the type name. If the SqlDbType is not convertible
  657. // to a DbType, throw an exception.
  658. internal void SetSqlDbType (SqlDbType type)
  659. {
  660. switch (type) {
  661. case SqlDbType.BigInt:
  662. MetaParameter.TypeName = "bigint";
  663. dbType = DbType.Int64;
  664. break;
  665. case SqlDbType.Binary:
  666. MetaParameter.TypeName = "binary";
  667. dbType = DbType.Binary;
  668. MetaParameter.IsVariableSizeType = true;
  669. break;
  670. case SqlDbType.Timestamp:
  671. MetaParameter.TypeName = "timestamp";
  672. dbType = DbType.Binary;
  673. break;
  674. case SqlDbType.VarBinary:
  675. MetaParameter.TypeName = "varbinary";
  676. dbType = DbType.Binary;
  677. MetaParameter.IsVariableSizeType = true;
  678. break;
  679. case SqlDbType.Bit:
  680. MetaParameter.TypeName = "bit";
  681. dbType = DbType.Boolean;
  682. break;
  683. case SqlDbType.Char:
  684. MetaParameter.TypeName = "char";
  685. dbType = DbType.AnsiStringFixedLength;
  686. MetaParameter.IsVariableSizeType = true;
  687. break;
  688. case SqlDbType.DateTime:
  689. MetaParameter.TypeName = "datetime";
  690. dbType = DbType.DateTime;
  691. break;
  692. case SqlDbType.SmallDateTime:
  693. MetaParameter.TypeName = "smalldatetime";
  694. dbType = DbType.DateTime;
  695. break;
  696. case SqlDbType.DateTime2:
  697. MetaParameter.TypeName = "datetime2";
  698. dbType = DbType.DateTime2;
  699. break;
  700. case SqlDbType.DateTimeOffset:
  701. MetaParameter.TypeName = "datetimeoffset";
  702. dbType = DbType.DateTimeOffset;
  703. break;
  704. case SqlDbType.Decimal:
  705. MetaParameter.TypeName = "decimal";
  706. dbType = DbType.Decimal;
  707. break;
  708. case SqlDbType.Float:
  709. MetaParameter.TypeName = "float";
  710. dbType = DbType.Double;
  711. break;
  712. case SqlDbType.Image:
  713. MetaParameter.TypeName = "image";
  714. dbType = DbType.Binary;
  715. MetaParameter.IsVariableSizeType = true;
  716. break;
  717. case SqlDbType.Int:
  718. MetaParameter.TypeName = "int";
  719. dbType = DbType.Int32;
  720. break;
  721. case SqlDbType.Money:
  722. MetaParameter.TypeName = "money";
  723. dbType = DbType.Currency;
  724. break;
  725. case SqlDbType.SmallMoney:
  726. MetaParameter.TypeName = "smallmoney";
  727. dbType = DbType.Currency;
  728. break;
  729. case SqlDbType.NChar:
  730. MetaParameter.TypeName = "nchar";
  731. dbType = DbType.StringFixedLength;
  732. MetaParameter.IsVariableSizeType = true;
  733. break;
  734. case SqlDbType.NText:
  735. MetaParameter.TypeName = "ntext";
  736. dbType = DbType.String;
  737. MetaParameter.IsVariableSizeType = true;
  738. break;
  739. case SqlDbType.NVarChar:
  740. MetaParameter.TypeName = "nvarchar";
  741. dbType = DbType.String;
  742. MetaParameter.IsVariableSizeType = true;
  743. break;
  744. case SqlDbType.Real:
  745. MetaParameter.TypeName = "real";
  746. dbType = DbType.Single;
  747. break;
  748. case SqlDbType.SmallInt:
  749. MetaParameter.TypeName = "smallint";
  750. dbType = DbType.Int16;
  751. break;
  752. case SqlDbType.Text:
  753. MetaParameter.TypeName = "text";
  754. dbType = DbType.AnsiString;
  755. MetaParameter.IsVariableSizeType = true;
  756. break;
  757. case SqlDbType.VarChar:
  758. MetaParameter.TypeName = "varchar";
  759. dbType = DbType.AnsiString;
  760. MetaParameter.IsVariableSizeType = true;
  761. break;
  762. case SqlDbType.TinyInt:
  763. MetaParameter.TypeName = "tinyint";
  764. dbType = DbType.Byte;
  765. break;
  766. case SqlDbType.UniqueIdentifier:
  767. MetaParameter.TypeName = "uniqueidentifier";
  768. dbType = DbType.Guid;
  769. break;
  770. case SqlDbType.Variant:
  771. MetaParameter.TypeName = "sql_variant";
  772. dbType = DbType.Object;
  773. break;
  774. case SqlDbType.Xml:
  775. MetaParameter.TypeName = "xml";
  776. dbType = DbType.Xml;
  777. MetaParameter.IsVariableSizeType = true;
  778. break;
  779. default:
  780. string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
  781. throw new ArgumentOutOfRangeException ("SqlDbType", exception);
  782. }
  783. sqlDbType = type;
  784. }
  785. public override string ToString()
  786. {
  787. return ParameterName;
  788. }
  789. object GetFrameworkValue (object rawValue, ref bool updated)
  790. {
  791. object tdsValue;
  792. updated = typeChanged || updated;
  793. if (updated) {
  794. tdsValue = SqlTypeToFrameworkType (rawValue);
  795. typeChanged = false;
  796. } else
  797. tdsValue = null;
  798. return tdsValue;
  799. }
  800. // TODO: Code copied from SqlDataReader, need a better approach
  801. object GetSqlValue (object value)
  802. {
  803. if (value == null)
  804. return value;
  805. switch (sqlDbType) {
  806. case SqlDbType.BigInt:
  807. if (value == DBNull.Value)
  808. return SqlInt64.Null;
  809. return (SqlInt64) ((long) value);
  810. case SqlDbType.Binary:
  811. case SqlDbType.Image:
  812. case SqlDbType.VarBinary:
  813. case SqlDbType.Timestamp:
  814. if (value == DBNull.Value)
  815. return SqlBinary.Null;
  816. return (SqlBinary) (byte[]) value;
  817. case SqlDbType.Bit:
  818. if (value == DBNull.Value)
  819. return SqlBoolean.Null;
  820. return (SqlBoolean) ((bool) value);
  821. case SqlDbType.Char:
  822. case SqlDbType.NChar:
  823. case SqlDbType.NText:
  824. case SqlDbType.NVarChar:
  825. case SqlDbType.Text:
  826. case SqlDbType.VarChar:
  827. if (value == DBNull.Value)
  828. return SqlString.Null;
  829. string str;
  830. Type type = value.GetType ();
  831. if (type == typeof (char))
  832. str = value.ToString ();
  833. else if (type == typeof (char[]))
  834. str = new String ((char[])value);
  835. else
  836. str = ((string)value);
  837. return (SqlString) str;
  838. case SqlDbType.DateTime:
  839. case SqlDbType.SmallDateTime:
  840. if (value == DBNull.Value)
  841. return SqlDateTime.Null;
  842. return (SqlDateTime) ((DateTime) value);
  843. case SqlDbType.Decimal:
  844. if (value == DBNull.Value)
  845. return SqlDecimal.Null;
  846. if (value is TdsBigDecimal)
  847. return SqlDecimalExtensions.FromTdsBigDecimal ((TdsBigDecimal) value);
  848. return (SqlDecimal) ((decimal) value);
  849. case SqlDbType.Float:
  850. if (value == DBNull.Value)
  851. return SqlDouble.Null;
  852. return (SqlDouble) ((double) value);
  853. case SqlDbType.Int:
  854. if (value == DBNull.Value)
  855. return SqlInt32.Null;
  856. return (SqlInt32) ((int) value);
  857. case SqlDbType.Money:
  858. case SqlDbType.SmallMoney:
  859. if (value == DBNull.Value)
  860. return SqlMoney.Null;
  861. return (SqlMoney) ((decimal) value);
  862. case SqlDbType.Real:
  863. if (value == DBNull.Value)
  864. return SqlSingle.Null;
  865. return (SqlSingle) ((float) value);
  866. case SqlDbType.UniqueIdentifier:
  867. if (value == DBNull.Value)
  868. return SqlGuid.Null;
  869. return (SqlGuid) ((Guid) value);
  870. case SqlDbType.SmallInt:
  871. if (value == DBNull.Value)
  872. return SqlInt16.Null;
  873. return (SqlInt16) ((short) value);
  874. case SqlDbType.TinyInt:
  875. if (value == DBNull.Value)
  876. return SqlByte.Null;
  877. return (SqlByte) ((byte) value);
  878. case SqlDbType.Xml:
  879. if (value == DBNull.Value)
  880. return SqlXml.Null;
  881. return (SqlXml) value;
  882. default:
  883. throw new NotImplementedException ("Type '" + sqlDbType + "' not implemented.");
  884. }
  885. }
  886. object SqlTypeToFrameworkType (object value)
  887. {
  888. INullable nullable = value as INullable;
  889. if (nullable == null)
  890. return ConvertToFrameworkType (value);
  891. if (nullable.IsNull)
  892. return DBNull.Value;
  893. Type type = value.GetType ();
  894. // Map to .net type, as Mono TDS respects only types from .net
  895. if (typeof (SqlString) == type) {
  896. return ((SqlString) value).Value;
  897. }
  898. if (typeof (SqlInt16) == type) {
  899. return ((SqlInt16) value).Value;
  900. }
  901. if (typeof (SqlInt32) == type) {
  902. return ((SqlInt32) value).Value;
  903. }
  904. if (typeof (SqlDateTime) == type) {
  905. return ((SqlDateTime) value).Value;
  906. }
  907. if (typeof (SqlInt64) == type) {
  908. return ((SqlInt64) value).Value;
  909. }
  910. if (typeof (SqlBinary) == type) {
  911. return ((SqlBinary) value).Value;
  912. }
  913. if (typeof (SqlBytes) == type) {
  914. return ((SqlBytes) value).Value;
  915. }
  916. if (typeof (SqlChars) == type) {
  917. return ((SqlChars) value).Value;
  918. }
  919. if (typeof (SqlBoolean) == type) {
  920. return ((SqlBoolean) value).Value;
  921. }
  922. if (typeof (SqlByte) == type) {
  923. return ((SqlByte) value).Value;
  924. }
  925. if (typeof (SqlDecimal) == type) {
  926. return ((SqlDecimal) value).Value;
  927. }
  928. if (typeof (SqlDouble) == type) {
  929. return ((SqlDouble) value).Value;
  930. }
  931. if (typeof (SqlGuid) == type) {
  932. return ((SqlGuid) value).Value;
  933. }
  934. if (typeof (SqlMoney) == type) {
  935. return ((SqlMoney) value).Value;
  936. }
  937. if (typeof (SqlSingle) == type) {
  938. return ((SqlSingle) value).Value;
  939. }
  940. return value;
  941. }
  942. internal object ConvertToFrameworkType (object value)
  943. {
  944. if (value == null || value == DBNull.Value)
  945. return value;
  946. if (sqlDbType == SqlDbType.Variant)
  947. return metaParameter.Value;
  948. Type frameworkType = SystemType;
  949. if (frameworkType == null)
  950. throw new NotImplementedException ("Type Not Supported : " + sqlDbType.ToString());
  951. Type valueType = value.GetType ();
  952. if (valueType == frameworkType)
  953. return value;
  954. object sqlvalue = null;
  955. try {
  956. sqlvalue = ConvertToFrameworkType (value, frameworkType);
  957. } catch (FormatException ex) {
  958. throw new FormatException (string.Format (CultureInfo.InvariantCulture,
  959. "Parameter value could not be converted from {0} to {1}.",
  960. valueType.Name, frameworkType.Name), ex);
  961. }
  962. return sqlvalue;
  963. }
  964. object ConvertToFrameworkType (object value, Type frameworkType)
  965. {
  966. if (frameworkType == typeof (string)) {
  967. if (value is DateTime)
  968. return ((DateTime) value).ToString ("yyyy-MM-dd'T'HH':'mm':'ss.fffffff");
  969. if (value is DateTimeOffset)
  970. return ((DateTimeOffset) value).ToString ("yyyy-MM-dd'T'HH':'mm':'ss.fffffffzzz");
  971. }
  972. object sqlvalue = Convert.ChangeType (value, frameworkType);
  973. switch (sqlDbType) {
  974. case SqlDbType.Money:
  975. case SqlDbType.SmallMoney:
  976. sqlvalue = Decimal.Round ((decimal) sqlvalue, 4);
  977. break;
  978. }
  979. return sqlvalue;
  980. }
  981. public override void ResetDbType ()
  982. {
  983. InferSqlType (Value);
  984. }
  985. public void ResetSqlDbType ()
  986. {
  987. InferSqlType (Value);
  988. }
  989. #endregion // Methods
  990. }
  991. }