SqlParameter.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. [BrowsableAttribute(false)]
  405. public string TypeName { get; set; }
  406. #endregion // Properties
  407. #region Methods
  408. object ICloneable.Clone ()
  409. {
  410. return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
  411. }
  412. // If the value is set without the DbType/SqlDbType being set, then we
  413. // infer type information.
  414. void InferSqlType (object value)
  415. {
  416. if (value == null || value == DBNull.Value) {
  417. SetSqlDbType (SqlDbType.NVarChar);
  418. return;
  419. }
  420. Type type = value.GetType ();
  421. if (type.IsEnum)
  422. type = Enum.GetUnderlyingType (type);
  423. object t = type_mapping [type];
  424. if (t == null)
  425. throw new ArgumentException (String.Format ("The parameter data type of {0} is invalid.", type.FullName));
  426. SetSqlDbType ((SqlDbType) t);
  427. }
  428. /*
  429. // Returns System.Type corresponding to the underlying SqlDbType
  430. internal override Type SystemType {
  431. get {
  432. return (Type) DbTypeMapping [sqlDbType];
  433. }
  434. }
  435. internal override object FrameworkDbType {
  436. get {
  437. return sqlDbType;
  438. }
  439. set {
  440. object t;
  441. try {
  442. t = (DbType) DbTypeFromName ((string)value);
  443. SetDbType ((DbType)t);
  444. } catch (ArgumentException) {
  445. t = (SqlDbType)FrameworkDbTypeFromName ((string)value);
  446. SetSqlDbType ((SqlDbType) t);
  447. }
  448. }
  449. }
  450. */
  451. DbType DbTypeFromName (string name)
  452. {
  453. switch (name.ToLower ()) {
  454. case "ansistring":
  455. return DbType.AnsiString;
  456. case "ansistringfixedlength":
  457. return DbType.AnsiStringFixedLength;
  458. case "binary":
  459. return DbType.Binary;
  460. case "boolean":
  461. return DbType.Boolean;
  462. case "byte":
  463. return DbType.Byte;
  464. case "currency":
  465. return DbType.Currency;
  466. case "date":
  467. return DbType.Date;
  468. case "datetime":
  469. return DbType.DateTime;
  470. case "decimal":
  471. return DbType.Decimal;
  472. case "double":
  473. return DbType.Double;
  474. case "guid":
  475. return DbType.Guid;
  476. case "int16":
  477. return DbType.Int16;
  478. case "int32":
  479. return DbType.Int32;
  480. case "int64":
  481. return DbType.Int64;
  482. case "object":
  483. return DbType.Object;
  484. case "single":
  485. return DbType.Single;
  486. case "string":
  487. return DbType.String;
  488. case "stringfixedlength":
  489. return DbType.StringFixedLength;
  490. case "time":
  491. return DbType.Time;
  492. case "xml":
  493. return DbType.Xml;
  494. default:
  495. string exception = String.Format ("No mapping exists from {0} to a known DbType.", name);
  496. throw new ArgumentException (exception);
  497. }
  498. }
  499. // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
  500. // string representation of the type name. If the DbType is not convertible
  501. // to an SqlDbType, throw an exception.
  502. private void SetDbType (DbType type)
  503. {
  504. switch (type) {
  505. case DbType.AnsiString:
  506. MetaParameter.TypeName = "varchar";
  507. sqlDbType = SqlDbType.VarChar;
  508. MetaParameter.IsVariableSizeType = true;
  509. break;
  510. case DbType.AnsiStringFixedLength:
  511. MetaParameter.TypeName = "char";
  512. sqlDbType = SqlDbType.Char;
  513. MetaParameter.IsVariableSizeType = true;
  514. break;
  515. case DbType.Binary:
  516. MetaParameter.TypeName = "varbinary";
  517. sqlDbType = SqlDbType.VarBinary;
  518. MetaParameter.IsVariableSizeType = true;
  519. break;
  520. case DbType.Boolean:
  521. MetaParameter.TypeName = "bit";
  522. sqlDbType = SqlDbType.Bit;
  523. break;
  524. case DbType.Byte:
  525. MetaParameter.TypeName = "tinyint";
  526. sqlDbType = SqlDbType.TinyInt;
  527. break;
  528. case DbType.Currency:
  529. sqlDbType = SqlDbType.Money;
  530. MetaParameter.TypeName = "money";
  531. break;
  532. case DbType.Date:
  533. case DbType.DateTime:
  534. MetaParameter.TypeName = "datetime";
  535. sqlDbType = SqlDbType.DateTime;
  536. break;
  537. case DbType.DateTime2:
  538. MetaParameter.TypeName = "datetime2";
  539. sqlDbType = SqlDbType.DateTime2;
  540. break;
  541. case DbType.Decimal:
  542. MetaParameter.TypeName = "decimal";
  543. sqlDbType = SqlDbType.Decimal;
  544. break;
  545. case DbType.Double:
  546. MetaParameter.TypeName = "float";
  547. sqlDbType = SqlDbType.Float;
  548. break;
  549. case DbType.Guid:
  550. MetaParameter.TypeName = "uniqueidentifier";
  551. sqlDbType = SqlDbType.UniqueIdentifier;
  552. break;
  553. case DbType.Int16:
  554. MetaParameter.TypeName = "smallint";
  555. sqlDbType = SqlDbType.SmallInt;
  556. break;
  557. case DbType.Int32:
  558. MetaParameter.TypeName = "int";
  559. sqlDbType = SqlDbType.Int;
  560. break;
  561. case DbType.Int64:
  562. MetaParameter.TypeName = "bigint";
  563. sqlDbType = SqlDbType.BigInt;
  564. break;
  565. case DbType.Object:
  566. MetaParameter.TypeName = "sql_variant";
  567. sqlDbType = SqlDbType.Variant;
  568. break;
  569. case DbType.Single:
  570. MetaParameter.TypeName = "real";
  571. sqlDbType = SqlDbType.Real;
  572. break;
  573. case DbType.String:
  574. MetaParameter.TypeName = "nvarchar";
  575. sqlDbType = SqlDbType.NVarChar;
  576. MetaParameter.IsVariableSizeType = true;
  577. break;
  578. case DbType.StringFixedLength:
  579. MetaParameter.TypeName = "nchar";
  580. sqlDbType = SqlDbType.NChar;
  581. MetaParameter.IsVariableSizeType = true;
  582. break;
  583. case DbType.Time:
  584. MetaParameter.TypeName = "datetime";
  585. sqlDbType = SqlDbType.DateTime;
  586. break;
  587. // Handle Xml type as string
  588. case DbType.Xml:
  589. MetaParameter.TypeName = "xml";
  590. sqlDbType = SqlDbType.Xml;
  591. MetaParameter.IsVariableSizeType = true;
  592. break;
  593. default:
  594. string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
  595. throw new ArgumentException (exception);
  596. }
  597. dbType = type;
  598. }
  599. // Used by internal constructor which has a SQL Server typename
  600. private SqlDbType FrameworkDbTypeFromName (string dbTypeName)
  601. {
  602. switch (dbTypeName.ToLower ()) {
  603. case "bigint":
  604. return SqlDbType.BigInt;
  605. case "binary":
  606. return SqlDbType.Binary;
  607. case "bit":
  608. return SqlDbType.Bit;
  609. case "char":
  610. return SqlDbType.Char;
  611. case "datetime":
  612. return SqlDbType.DateTime;
  613. case "decimal":
  614. return SqlDbType.Decimal;
  615. case "float":
  616. return SqlDbType.Float;
  617. case "image":
  618. return SqlDbType.Image;
  619. case "int":
  620. return SqlDbType.Int;
  621. case "money":
  622. return SqlDbType.Money;
  623. case "nchar":
  624. return SqlDbType.NChar;
  625. case "ntext":
  626. return SqlDbType.NText;
  627. case "nvarchar":
  628. return SqlDbType.NVarChar;
  629. case "real":
  630. return SqlDbType.Real;
  631. case "smalldatetime":
  632. return SqlDbType.SmallDateTime;
  633. case "smallint":
  634. return SqlDbType.SmallInt;
  635. case "smallmoney":
  636. return SqlDbType.SmallMoney;
  637. case "text":
  638. return SqlDbType.Text;
  639. case "timestamp":
  640. return SqlDbType.Timestamp;
  641. case "tinyint":
  642. return SqlDbType.TinyInt;
  643. case "uniqueidentifier":
  644. return SqlDbType.UniqueIdentifier;
  645. case "varbinary":
  646. return SqlDbType.VarBinary;
  647. case "varchar":
  648. return SqlDbType.VarChar;
  649. case "sql_variant":
  650. return SqlDbType.Variant;
  651. case "xml":
  652. return SqlDbType.Xml;
  653. default:
  654. return SqlDbType.Variant;
  655. }
  656. }
  657. // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
  658. // string representation of the type name. If the SqlDbType is not convertible
  659. // to a DbType, throw an exception.
  660. internal void SetSqlDbType (SqlDbType type)
  661. {
  662. switch (type) {
  663. case SqlDbType.BigInt:
  664. MetaParameter.TypeName = "bigint";
  665. dbType = DbType.Int64;
  666. break;
  667. case SqlDbType.Binary:
  668. MetaParameter.TypeName = "binary";
  669. dbType = DbType.Binary;
  670. MetaParameter.IsVariableSizeType = true;
  671. break;
  672. case SqlDbType.Timestamp:
  673. MetaParameter.TypeName = "timestamp";
  674. dbType = DbType.Binary;
  675. break;
  676. case SqlDbType.VarBinary:
  677. MetaParameter.TypeName = "varbinary";
  678. dbType = DbType.Binary;
  679. MetaParameter.IsVariableSizeType = true;
  680. break;
  681. case SqlDbType.Bit:
  682. MetaParameter.TypeName = "bit";
  683. dbType = DbType.Boolean;
  684. break;
  685. case SqlDbType.Char:
  686. MetaParameter.TypeName = "char";
  687. dbType = DbType.AnsiStringFixedLength;
  688. MetaParameter.IsVariableSizeType = true;
  689. break;
  690. case SqlDbType.DateTime:
  691. MetaParameter.TypeName = "datetime";
  692. dbType = DbType.DateTime;
  693. break;
  694. case SqlDbType.SmallDateTime:
  695. MetaParameter.TypeName = "smalldatetime";
  696. dbType = DbType.DateTime;
  697. break;
  698. case SqlDbType.DateTime2:
  699. MetaParameter.TypeName = "datetime2";
  700. dbType = DbType.DateTime2;
  701. break;
  702. case SqlDbType.DateTimeOffset:
  703. MetaParameter.TypeName = "datetimeoffset";
  704. dbType = DbType.DateTimeOffset;
  705. break;
  706. case SqlDbType.Decimal:
  707. MetaParameter.TypeName = "decimal";
  708. dbType = DbType.Decimal;
  709. break;
  710. case SqlDbType.Float:
  711. MetaParameter.TypeName = "float";
  712. dbType = DbType.Double;
  713. break;
  714. case SqlDbType.Image:
  715. MetaParameter.TypeName = "image";
  716. dbType = DbType.Binary;
  717. MetaParameter.IsVariableSizeType = true;
  718. break;
  719. case SqlDbType.Int:
  720. MetaParameter.TypeName = "int";
  721. dbType = DbType.Int32;
  722. break;
  723. case SqlDbType.Money:
  724. MetaParameter.TypeName = "money";
  725. dbType = DbType.Currency;
  726. break;
  727. case SqlDbType.SmallMoney:
  728. MetaParameter.TypeName = "smallmoney";
  729. dbType = DbType.Currency;
  730. break;
  731. case SqlDbType.NChar:
  732. MetaParameter.TypeName = "nchar";
  733. dbType = DbType.StringFixedLength;
  734. MetaParameter.IsVariableSizeType = true;
  735. break;
  736. case SqlDbType.NText:
  737. MetaParameter.TypeName = "ntext";
  738. dbType = DbType.String;
  739. MetaParameter.IsVariableSizeType = true;
  740. break;
  741. case SqlDbType.NVarChar:
  742. MetaParameter.TypeName = "nvarchar";
  743. dbType = DbType.String;
  744. MetaParameter.IsVariableSizeType = true;
  745. break;
  746. case SqlDbType.Real:
  747. MetaParameter.TypeName = "real";
  748. dbType = DbType.Single;
  749. break;
  750. case SqlDbType.SmallInt:
  751. MetaParameter.TypeName = "smallint";
  752. dbType = DbType.Int16;
  753. break;
  754. case SqlDbType.Text:
  755. MetaParameter.TypeName = "text";
  756. dbType = DbType.AnsiString;
  757. MetaParameter.IsVariableSizeType = true;
  758. break;
  759. case SqlDbType.VarChar:
  760. MetaParameter.TypeName = "varchar";
  761. dbType = DbType.AnsiString;
  762. MetaParameter.IsVariableSizeType = true;
  763. break;
  764. case SqlDbType.TinyInt:
  765. MetaParameter.TypeName = "tinyint";
  766. dbType = DbType.Byte;
  767. break;
  768. case SqlDbType.UniqueIdentifier:
  769. MetaParameter.TypeName = "uniqueidentifier";
  770. dbType = DbType.Guid;
  771. break;
  772. case SqlDbType.Variant:
  773. MetaParameter.TypeName = "sql_variant";
  774. dbType = DbType.Object;
  775. break;
  776. case SqlDbType.Xml:
  777. MetaParameter.TypeName = "xml";
  778. dbType = DbType.Xml;
  779. MetaParameter.IsVariableSizeType = true;
  780. break;
  781. default:
  782. string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
  783. throw new ArgumentOutOfRangeException ("SqlDbType", exception);
  784. }
  785. sqlDbType = type;
  786. }
  787. public override string ToString()
  788. {
  789. return ParameterName;
  790. }
  791. object GetFrameworkValue (object rawValue, ref bool updated)
  792. {
  793. object tdsValue;
  794. updated = typeChanged || updated;
  795. if (updated) {
  796. tdsValue = SqlTypeToFrameworkType (rawValue);
  797. typeChanged = false;
  798. } else
  799. tdsValue = null;
  800. return tdsValue;
  801. }
  802. // TODO: Code copied from SqlDataReader, need a better approach
  803. object GetSqlValue (object value)
  804. {
  805. if (value == null)
  806. return value;
  807. switch (sqlDbType) {
  808. case SqlDbType.BigInt:
  809. if (value == DBNull.Value)
  810. return SqlInt64.Null;
  811. return (SqlInt64) ((long) value);
  812. case SqlDbType.Binary:
  813. case SqlDbType.Image:
  814. case SqlDbType.VarBinary:
  815. case SqlDbType.Timestamp:
  816. if (value == DBNull.Value)
  817. return SqlBinary.Null;
  818. return (SqlBinary) (byte[]) value;
  819. case SqlDbType.Bit:
  820. if (value == DBNull.Value)
  821. return SqlBoolean.Null;
  822. return (SqlBoolean) ((bool) value);
  823. case SqlDbType.Char:
  824. case SqlDbType.NChar:
  825. case SqlDbType.NText:
  826. case SqlDbType.NVarChar:
  827. case SqlDbType.Text:
  828. case SqlDbType.VarChar:
  829. if (value == DBNull.Value)
  830. return SqlString.Null;
  831. string str;
  832. Type type = value.GetType ();
  833. if (type == typeof (char))
  834. str = value.ToString ();
  835. else if (type == typeof (char[]))
  836. str = new String ((char[])value);
  837. else
  838. str = ((string)value);
  839. return (SqlString) str;
  840. case SqlDbType.DateTime:
  841. case SqlDbType.SmallDateTime:
  842. if (value == DBNull.Value)
  843. return SqlDateTime.Null;
  844. return (SqlDateTime) ((DateTime) value);
  845. case SqlDbType.Decimal:
  846. if (value == DBNull.Value)
  847. return SqlDecimal.Null;
  848. if (value is TdsBigDecimal)
  849. return SqlDecimalExtensions.FromTdsBigDecimal ((TdsBigDecimal) value);
  850. return (SqlDecimal) ((decimal) value);
  851. case SqlDbType.Float:
  852. if (value == DBNull.Value)
  853. return SqlDouble.Null;
  854. return (SqlDouble) ((double) value);
  855. case SqlDbType.Int:
  856. if (value == DBNull.Value)
  857. return SqlInt32.Null;
  858. return (SqlInt32) ((int) value);
  859. case SqlDbType.Money:
  860. case SqlDbType.SmallMoney:
  861. if (value == DBNull.Value)
  862. return SqlMoney.Null;
  863. return (SqlMoney) ((decimal) value);
  864. case SqlDbType.Real:
  865. if (value == DBNull.Value)
  866. return SqlSingle.Null;
  867. return (SqlSingle) ((float) value);
  868. case SqlDbType.UniqueIdentifier:
  869. if (value == DBNull.Value)
  870. return SqlGuid.Null;
  871. return (SqlGuid) ((Guid) value);
  872. case SqlDbType.SmallInt:
  873. if (value == DBNull.Value)
  874. return SqlInt16.Null;
  875. return (SqlInt16) ((short) value);
  876. case SqlDbType.TinyInt:
  877. if (value == DBNull.Value)
  878. return SqlByte.Null;
  879. return (SqlByte) ((byte) value);
  880. case SqlDbType.Xml:
  881. if (value == DBNull.Value)
  882. return SqlXml.Null;
  883. return (SqlXml) value;
  884. default:
  885. throw new NotImplementedException ("Type '" + sqlDbType + "' not implemented.");
  886. }
  887. }
  888. object SqlTypeToFrameworkType (object value)
  889. {
  890. INullable nullable = value as INullable;
  891. if (nullable == null)
  892. return ConvertToFrameworkType (value);
  893. if (nullable.IsNull)
  894. return DBNull.Value;
  895. Type type = value.GetType ();
  896. // Map to .net type, as Mono TDS respects only types from .net
  897. if (typeof (SqlString) == type) {
  898. return ((SqlString) value).Value;
  899. }
  900. if (typeof (SqlInt16) == type) {
  901. return ((SqlInt16) value).Value;
  902. }
  903. if (typeof (SqlInt32) == type) {
  904. return ((SqlInt32) value).Value;
  905. }
  906. if (typeof (SqlDateTime) == type) {
  907. return ((SqlDateTime) value).Value;
  908. }
  909. if (typeof (SqlInt64) == type) {
  910. return ((SqlInt64) value).Value;
  911. }
  912. if (typeof (SqlBinary) == type) {
  913. return ((SqlBinary) value).Value;
  914. }
  915. if (typeof (SqlBytes) == type) {
  916. return ((SqlBytes) value).Value;
  917. }
  918. if (typeof (SqlChars) == type) {
  919. return ((SqlChars) value).Value;
  920. }
  921. if (typeof (SqlBoolean) == type) {
  922. return ((SqlBoolean) value).Value;
  923. }
  924. if (typeof (SqlByte) == type) {
  925. return ((SqlByte) value).Value;
  926. }
  927. if (typeof (SqlDecimal) == type) {
  928. return ((SqlDecimal) value).Value;
  929. }
  930. if (typeof (SqlDouble) == type) {
  931. return ((SqlDouble) value).Value;
  932. }
  933. if (typeof (SqlGuid) == type) {
  934. return ((SqlGuid) value).Value;
  935. }
  936. if (typeof (SqlMoney) == type) {
  937. return ((SqlMoney) value).Value;
  938. }
  939. if (typeof (SqlSingle) == type) {
  940. return ((SqlSingle) value).Value;
  941. }
  942. return value;
  943. }
  944. internal object ConvertToFrameworkType (object value)
  945. {
  946. if (value == null || value == DBNull.Value)
  947. return value;
  948. if (sqlDbType == SqlDbType.Variant)
  949. return metaParameter.Value;
  950. Type frameworkType = SystemType;
  951. if (frameworkType == null)
  952. throw new NotImplementedException ("Type Not Supported : " + sqlDbType.ToString());
  953. Type valueType = value.GetType ();
  954. if (valueType == frameworkType)
  955. return value;
  956. object sqlvalue = null;
  957. try {
  958. sqlvalue = ConvertToFrameworkType (value, frameworkType);
  959. } catch (FormatException ex) {
  960. throw new FormatException (string.Format (CultureInfo.InvariantCulture,
  961. "Parameter value could not be converted from {0} to {1}.",
  962. valueType.Name, frameworkType.Name), ex);
  963. }
  964. return sqlvalue;
  965. }
  966. object ConvertToFrameworkType (object value, Type frameworkType)
  967. {
  968. if (frameworkType == typeof (string)) {
  969. if (value is DateTime)
  970. return ((DateTime) value).ToString ("yyyy-MM-dd'T'HH':'mm':'ss.fffffff");
  971. if (value is DateTimeOffset)
  972. return ((DateTimeOffset) value).ToString ("yyyy-MM-dd'T'HH':'mm':'ss.fffffffzzz");
  973. }
  974. object sqlvalue = Convert.ChangeType (value, frameworkType);
  975. switch (sqlDbType) {
  976. case SqlDbType.Money:
  977. case SqlDbType.SmallMoney:
  978. sqlvalue = Decimal.Round ((decimal) sqlvalue, 4);
  979. break;
  980. }
  981. return sqlvalue;
  982. }
  983. public override void ResetDbType ()
  984. {
  985. InferSqlType (Value);
  986. }
  987. public void ResetSqlDbType ()
  988. {
  989. InferSqlType (Value);
  990. }
  991. #endregion // Methods
  992. }
  993. }