2 // System.Data.SqlClient.SqlParameter.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 // Diego Caravana (diego@toth.it)
9 // Umadevi S (sumadevi@novell.com)
10 // Amit Biswas (amit@amitbiswas.com)
11 // Veerapuram Varadhan (vvaradhan@novell.com)
13 // (C) Ximian, Inc. 2002
14 // Copyright (C) Tim Coleman, 2002
18 // Copyright (C) 2004, 2008, 2009 Novell, Inc (http://www.novell.com)
20 // Permission is hereby granted, free of charge, to any person obtaining
21 // a copy of this software and associated documentation files (the
22 // "Software"), to deal in the Software without restriction, including
23 // without limitation the rights to use, copy, modify, merge, publish,
24 // distribute, sublicense, and/or sell copies of the Software, and to
25 // permit persons to whom the Software is furnished to do so, subject to
26 // the following conditions:
28 // The above copyright notice and this permission notice shall be
29 // included in all copies or substantial portions of the Software.
31 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 using Mono
.Data
.Tds
.Protocol
;
43 using System
.Collections
;
44 using System
.ComponentModel
;
46 using System
.Data
.Common
;
47 using System
.Data
.SqlTypes
;
48 using System
.Globalization
;
49 using System
.Runtime
.InteropServices
;
53 namespace System
.Data
.SqlClient
{
54 [TypeConverterAttribute ("System.Data.SqlClient.SqlParameter+SqlParameterConverter, " + Consts
.AssemblySystem_Data
)]
55 public sealed class SqlParameter
: DbParameter
, IDbDataParameter
, IDataParameter
, ICloneable
57 #region Import from old DbParameter
58 static Hashtable dbTypeMapping
= new Hashtable ();
59 internal static Hashtable DbTypeMapping
{
60 get { return dbTypeMapping;}
61 set { dbTypeMapping = value;}
64 // LAMESPEC: Implementors should populate the dbTypeMapping accordingly
65 internal Type SystemType
{
67 return (Type
) dbTypeMapping
[DbType
];
74 TdsMetaParameter metaParameter
;
76 SqlParameterCollection container
;
78 ParameterDirection direction
= ParameterDirection
.Input
;
83 DataRowVersion sourceVersion
;
84 SqlCompareOptions compareInfo
;
88 bool sourceColumnNullMapping
;
89 string xmlSchemaCollectionDatabase
= String
.Empty
;
90 string xmlSchemaCollectionOwningSchema
= String
.Empty
;
91 string xmlSchemaCollectionName
= String
.Empty
;
93 static Hashtable type_mapping
;
100 static SqlParameter ()
102 if (DbTypeMapping
== null)
103 DbTypeMapping
= new Hashtable ();
105 DbTypeMapping
.Add (SqlDbType
.BigInt
, typeof (long));
106 DbTypeMapping
.Add (SqlDbType
.Bit
, typeof (bool));
107 DbTypeMapping
.Add (SqlDbType
.Char
, typeof (string));
108 DbTypeMapping
.Add (SqlDbType
.NChar
, typeof (string));
109 DbTypeMapping
.Add (SqlDbType
.Text
, typeof (string));
110 DbTypeMapping
.Add (SqlDbType
.NText
, typeof (string));
111 DbTypeMapping
.Add (SqlDbType
.VarChar
, typeof (string));
112 DbTypeMapping
.Add (SqlDbType
.NVarChar
, typeof (string));
113 DbTypeMapping
.Add (SqlDbType
.SmallDateTime
, typeof (DateTime
));
114 DbTypeMapping
.Add (SqlDbType
.DateTime
, typeof (DateTime
));
115 DbTypeMapping
.Add (SqlDbType
.Decimal
, typeof (decimal));
116 DbTypeMapping
.Add (SqlDbType
.Float
, typeof (double));
117 DbTypeMapping
.Add (SqlDbType
.Binary
, typeof (byte []));
118 DbTypeMapping
.Add (SqlDbType
.Image
, typeof (byte []));
119 DbTypeMapping
.Add (SqlDbType
.Money
, typeof (decimal));
120 DbTypeMapping
.Add (SqlDbType
.SmallMoney
, typeof (decimal));
121 DbTypeMapping
.Add (SqlDbType
.VarBinary
, typeof (byte []));
122 DbTypeMapping
.Add (SqlDbType
.TinyInt
, typeof (byte));
123 DbTypeMapping
.Add (SqlDbType
.Int
, typeof (int));
124 DbTypeMapping
.Add (SqlDbType
.Real
, typeof (float));
125 DbTypeMapping
.Add (SqlDbType
.SmallInt
, typeof (short));
126 DbTypeMapping
.Add (SqlDbType
.UniqueIdentifier
, typeof (Guid
));
127 DbTypeMapping
.Add (SqlDbType
.Variant
, typeof (object));
128 DbTypeMapping
.Add (SqlDbType
.Xml
, typeof (string));
130 type_mapping
= new Hashtable ();
132 type_mapping
.Add (typeof (long), SqlDbType
.BigInt
);
133 type_mapping
.Add (typeof (SqlTypes
.SqlInt64
), SqlDbType
.BigInt
);
135 type_mapping
.Add (typeof (bool), SqlDbType
.Bit
);
136 type_mapping
.Add (typeof (SqlTypes
.SqlBoolean
), SqlDbType
.Bit
);
138 type_mapping
.Add (typeof (char), SqlDbType
.NVarChar
);
139 type_mapping
.Add (typeof (char []), SqlDbType
.NVarChar
);
140 type_mapping
.Add (typeof (SqlTypes
.SqlChars
), SqlDbType
.NVarChar
);
142 type_mapping
.Add (typeof (string), SqlDbType
.NVarChar
);
143 type_mapping
.Add (typeof (SqlTypes
.SqlString
), SqlDbType
.NVarChar
);
145 type_mapping
.Add (typeof (DateTime
), SqlDbType
.DateTime
);
146 type_mapping
.Add (typeof (SqlTypes
.SqlDateTime
), SqlDbType
.DateTime
);
148 type_mapping
.Add (typeof (decimal), SqlDbType
.Decimal
);
149 type_mapping
.Add (typeof (SqlTypes
.SqlDecimal
), SqlDbType
.Decimal
);
151 type_mapping
.Add (typeof (double), SqlDbType
.Float
);
152 type_mapping
.Add (typeof (SqlTypes
.SqlDouble
), SqlDbType
.Float
);
154 type_mapping
.Add (typeof (byte []), SqlDbType
.VarBinary
);
155 type_mapping
.Add (typeof (SqlTypes
.SqlBinary
), SqlDbType
.VarBinary
);
157 type_mapping
.Add (typeof (SqlTypes
.SqlBytes
), SqlDbType
.VarBinary
);
159 type_mapping
.Add (typeof (byte), SqlDbType
.TinyInt
);
160 type_mapping
.Add (typeof (SqlTypes
.SqlByte
), SqlDbType
.TinyInt
);
162 type_mapping
.Add (typeof (int), SqlDbType
.Int
);
163 type_mapping
.Add (typeof (SqlTypes
.SqlInt32
), SqlDbType
.Int
);
165 type_mapping
.Add (typeof (float), SqlDbType
.Real
);
166 type_mapping
.Add (typeof (SqlTypes
.SqlSingle
), SqlDbType
.Real
);
168 type_mapping
.Add (typeof (short), SqlDbType
.SmallInt
);
169 type_mapping
.Add (typeof (SqlTypes
.SqlInt16
), SqlDbType
.SmallInt
);
171 type_mapping
.Add (typeof (Guid
), SqlDbType
.UniqueIdentifier
);
172 type_mapping
.Add (typeof (SqlTypes
.SqlGuid
), SqlDbType
.UniqueIdentifier
);
174 type_mapping
.Add (typeof (SqlTypes
.SqlMoney
), SqlDbType
.Money
);
176 type_mapping
.Add (typeof (XmlReader
), SqlDbType
.Xml
);
177 type_mapping
.Add (typeof (SqlTypes
.SqlXml
), SqlDbType
.Xml
);
179 type_mapping
.Add (typeof (object), SqlDbType
.Variant
);
182 public SqlParameter ()
183 : this (String
.Empty
, SqlDbType
.NVarChar
, 0, ParameterDirection
.Input
, false, 0, 0, String
.Empty
, DataRowVersion
.Current
, null)
188 public SqlParameter (string parameterName
, object value)
190 if (parameterName
== null)
191 parameterName
= string.Empty
;
192 metaParameter
= new TdsMetaParameter (parameterName
, GetFrameworkValue
);
193 metaParameter
.RawValue
= value;
194 InferSqlType (value);
195 sourceVersion
= DataRowVersion
.Current
;
198 public SqlParameter (string parameterName
, SqlDbType dbType
)
199 : this (parameterName
, dbType
, 0, ParameterDirection
.Input
, false, 0, 0, null, DataRowVersion
.Current
, null)
203 public SqlParameter (string parameterName
, SqlDbType dbType
, int size
)
204 : this (parameterName
, dbType
, size
, ParameterDirection
.Input
, false, 0, 0, null, DataRowVersion
.Current
, null)
208 public SqlParameter (string parameterName
, SqlDbType dbType
, int size
, string sourceColumn
)
209 : this (parameterName
, dbType
, size
, ParameterDirection
.Input
, false, 0, 0, sourceColumn
, DataRowVersion
.Current
, null)
213 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
214 public SqlParameter (string parameterName
, SqlDbType dbType
, int size
, ParameterDirection direction
, bool isNullable
, byte precision
, byte scale
, string sourceColumn
, DataRowVersion sourceVersion
, object value)
216 if (parameterName
== null)
217 parameterName
= string.Empty
;
219 metaParameter
= new TdsMetaParameter (parameterName
, size
,
220 isNullable
, precision
,
223 metaParameter
.RawValue
= value;
224 if (dbType
!= SqlDbType
.Variant
)
226 Direction
= direction
;
227 SourceColumn
= sourceColumn
;
228 SourceVersion
= sourceVersion
;
231 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
)
232 : this (parameterName
, dbType
, size
, direction
, false, precision
, scale
, sourceColumn
, sourceVersion
, value)
234 XmlSchemaCollectionDatabase
= xmlSchemaCollectionDatabase
;
235 XmlSchemaCollectionOwningSchema
= xmlSchemaCollectionOwningSchema
;
236 XmlSchemaCollectionName
= xmlSchemaCollectionName
;
237 SourceColumnNullMapping
= sourceColumnNullMapping
;
240 // This constructor is used internally to construct a
241 // SqlParameter. The value array comes from sp_procedure_params_rowset.
242 // This is in SqlCommand.DeriveParameters.
244 // http://social.msdn.microsoft.com/forums/en-US/transactsql/thread/900756fd-3980-48e3-ae59-a15d7fc15b4c/
245 internal SqlParameter (object[] dbValues
)
246 : this (dbValues
[3].ToString (), (object) null)
248 ParameterName
= (string) dbValues
[3];
250 switch ((short) dbValues
[5]) {
252 Direction
= ParameterDirection
.Input
;
255 Direction
= ParameterDirection
.InputOutput
;
258 Direction
= ParameterDirection
.Output
;
261 Direction
= ParameterDirection
.ReturnValue
;
264 Direction
= ParameterDirection
.Input
;
268 SqlDbType
= (SqlDbType
) FrameworkDbTypeFromName ((string) dbValues
[16]);
270 if (MetaParameter
.IsVariableSizeType
) {
271 if (dbValues
[10] != DBNull
.Value
)
272 Size
= (int) dbValues
[10];
275 if (SqlDbType
== SqlDbType
.Decimal
) {
276 if (dbValues
[12] != null && dbValues
[12] != DBNull
.Value
)
277 Precision
= (byte) ((short) dbValues
[12]);
278 if (dbValues
[13] != null && dbValues
[13] != DBNull
.Value
)
279 Scale
= (byte) ((short) dbValues
[13]);
283 #endregion // Constructors
287 // Used to ensure that only one collection can contain this
289 internal SqlParameterCollection Container
{
290 get { return container; }
291 set { container = value; }
294 internal void CheckIfInitialized ()
297 throw new Exception ("all parameters to have an explicity set type");
299 if (MetaParameter
.IsVariableSizeType
) {
300 if (SqlDbType
== SqlDbType
.Decimal
&& Precision
== 0)
301 throw new Exception ("Parameter of type 'Decimal' have an explicitly set Precision and Scale");
303 throw new Exception ("all variable length parameters to have an explicitly set non-zero Size");
307 public override DbType DbType
{
308 get { return dbType; }
316 [RefreshProperties (RefreshProperties
.All
)]
318 ParameterDirection Direction
{
319 get { return direction; }
322 switch( direction
) {
323 case ParameterDirection
.Output
:
324 MetaParameter
.Direction
= TdsParameterDirection
.Output
;
326 case ParameterDirection
.InputOutput
:
327 MetaParameter
.Direction
= TdsParameterDirection
.InputOutput
;
329 case ParameterDirection
.ReturnValue
:
330 MetaParameter
.Direction
= TdsParameterDirection
.ReturnValue
;
336 internal TdsMetaParameter MetaParameter
{
337 get { return metaParameter; }
340 public override bool IsNullable
{
341 get { return metaParameter.IsNullable; }
342 set { metaParameter.IsNullable = value; }
346 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
348 get { return offset; }
349 set { offset = value; }
352 public override string ParameterName
{
353 get { return metaParameter.ParameterName; }
356 value = string.Empty
;
357 metaParameter
.ParameterName
= value;
362 public byte Precision
{
363 get { return metaParameter.Precision; }
364 set { metaParameter.Precision = value; }
369 get { return metaParameter.Scale; }
370 set { metaParameter.Scale = value; }
373 public override int Size
{
374 get { return metaParameter.Size; }
375 set { metaParameter.Size = value; }
378 public override string SourceColumn
{
380 if (sourceColumn
== null)
384 set { sourceColumn = value; }
387 public override DataRowVersion SourceVersion
{
388 get { return sourceVersion; }
389 set { sourceVersion = value; }
392 [RefreshProperties (RefreshProperties
.All
)]
393 [DbProviderSpecificTypeProperty(true)]
394 public SqlDbType SqlDbType
{
395 get { return sqlDbType; }
397 SetSqlDbType (value);
403 [TypeConverterAttribute (typeof (StringConverter
))]
404 [RefreshProperties (RefreshProperties
.All
)]
405 public override object Value
{
408 return GetSqlValue (metaParameter
.RawValue
);
409 return metaParameter
.RawValue
;
413 InferSqlType (value);
416 if (value is INullable
) {
417 sqlType
= value.GetType ();
418 value = SqlTypeToFrameworkType (value);
420 metaParameter
.RawValue
= value;
425 public SqlCompareOptions CompareInfo
{
426 get{ return compareInfo; }
427 set{ compareInfo = value; }
431 public int LocaleId
{
432 get { return localeId; }
433 set { localeId = value; }
437 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
438 public Object SqlValue
{
440 return GetSqlValue (metaParameter
.RawValue
);
447 public override bool SourceColumnNullMapping
{
448 get { return sourceColumnNullMapping; }
449 set { sourceColumnNullMapping = value; }
452 public string XmlSchemaCollectionDatabase
{
453 get { return xmlSchemaCollectionDatabase; }
454 set { xmlSchemaCollectionDatabase = (value == null ? String.Empty : value); }
457 public string XmlSchemaCollectionName
{
458 get { return xmlSchemaCollectionName; }
460 xmlSchemaCollectionName
= (value == null ? String
.Empty
: value);
464 public string XmlSchemaCollectionOwningSchema
{
465 get { return xmlSchemaCollectionOwningSchema; }
467 xmlSchemaCollectionOwningSchema
= (value == null ? String
.Empty
: value);
471 [BrowsableAttribute(false)]
472 public string UdtTypeName { get; set; }
474 #endregion // Properties
478 object ICloneable
.Clone ()
480 return new SqlParameter (ParameterName
, SqlDbType
, Size
, Direction
, IsNullable
, Precision
, Scale
, SourceColumn
, SourceVersion
, Value
);
483 // If the value is set without the DbType/SqlDbType being set, then we
484 // infer type information.
485 void InferSqlType (object value)
487 if (value == null || value == DBNull
.Value
) {
488 SetSqlDbType (SqlDbType
.NVarChar
);
492 Type type
= value.GetType ();
494 type
= Enum
.GetUnderlyingType (type
);
495 object t
= type_mapping
[type
];
497 throw new ArgumentException (String
.Format ("The parameter data type of {0} is invalid.", type
.FullName
));
498 SetSqlDbType ((SqlDbType
) t
);
502 // Returns System.Type corresponding to the underlying SqlDbType
503 internal override Type SystemType {
505 return (Type) DbTypeMapping [sqlDbType];
509 internal override object FrameworkDbType {
517 t = (DbType) DbTypeFromName ((string)value);
518 SetDbType ((DbType)t);
519 } catch (ArgumentException) {
520 t = (SqlDbType)FrameworkDbTypeFromName ((string)value);
521 SetSqlDbType ((SqlDbType) t);
527 DbType
DbTypeFromName (string name
)
529 switch (name
.ToLower ()) {
531 return DbType
.AnsiString
;
532 case "ansistringfixedlength":
533 return DbType
.AnsiStringFixedLength
;
535 return DbType
.Binary
;
537 return DbType
.Boolean
;
541 return DbType
.Currency
;
545 return DbType
.DateTime
;
547 return DbType
.Decimal
;
549 return DbType
.Double
;
559 return DbType
.Object
;
561 return DbType
.Single
;
563 return DbType
.String
;
564 case "stringfixedlength":
565 return DbType
.StringFixedLength
;
571 string exception
= String
.Format ("No mapping exists from {0} to a known DbType.", name
);
572 throw new ArgumentException (exception
);
576 // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
577 // string representation of the type name. If the DbType is not convertible
578 // to an SqlDbType, throw an exception.
579 private void SetDbType (DbType type
)
582 case DbType
.AnsiString
:
583 MetaParameter
.TypeName
= "varchar";
584 sqlDbType
= SqlDbType
.VarChar
;
585 MetaParameter
.IsVariableSizeType
= true;
587 case DbType
.AnsiStringFixedLength
:
588 MetaParameter
.TypeName
= "char";
589 sqlDbType
= SqlDbType
.Char
;
590 MetaParameter
.IsVariableSizeType
= true;
593 MetaParameter
.TypeName
= "varbinary";
594 sqlDbType
= SqlDbType
.VarBinary
;
595 MetaParameter
.IsVariableSizeType
= true;
598 MetaParameter
.TypeName
= "bit";
599 sqlDbType
= SqlDbType
.Bit
;
602 MetaParameter
.TypeName
= "tinyint";
603 sqlDbType
= SqlDbType
.TinyInt
;
605 case DbType
.Currency
:
606 sqlDbType
= SqlDbType
.Money
;
607 MetaParameter
.TypeName
= "money";
610 case DbType
.DateTime
:
611 MetaParameter
.TypeName
= "datetime";
612 sqlDbType
= SqlDbType
.DateTime
;
614 case DbType
.DateTime2
:
615 MetaParameter
.TypeName
= "datetime2";
616 sqlDbType
= SqlDbType
.DateTime2
;
619 MetaParameter
.TypeName
= "decimal";
620 sqlDbType
= SqlDbType
.Decimal
;
623 MetaParameter
.TypeName
= "float";
624 sqlDbType
= SqlDbType
.Float
;
627 MetaParameter
.TypeName
= "uniqueidentifier";
628 sqlDbType
= SqlDbType
.UniqueIdentifier
;
631 MetaParameter
.TypeName
= "smallint";
632 sqlDbType
= SqlDbType
.SmallInt
;
635 MetaParameter
.TypeName
= "int";
636 sqlDbType
= SqlDbType
.Int
;
639 MetaParameter
.TypeName
= "bigint";
640 sqlDbType
= SqlDbType
.BigInt
;
643 MetaParameter
.TypeName
= "sql_variant";
644 sqlDbType
= SqlDbType
.Variant
;
647 MetaParameter
.TypeName
= "real";
648 sqlDbType
= SqlDbType
.Real
;
651 MetaParameter
.TypeName
= "nvarchar";
652 sqlDbType
= SqlDbType
.NVarChar
;
653 MetaParameter
.IsVariableSizeType
= true;
655 case DbType
.StringFixedLength
:
656 MetaParameter
.TypeName
= "nchar";
657 sqlDbType
= SqlDbType
.NChar
;
658 MetaParameter
.IsVariableSizeType
= true;
661 MetaParameter
.TypeName
= "datetime";
662 sqlDbType
= SqlDbType
.DateTime
;
664 // Handle Xml type as string
666 MetaParameter
.TypeName
= "xml";
667 sqlDbType
= SqlDbType
.Xml
;
668 MetaParameter
.IsVariableSizeType
= true;
671 string exception
= String
.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type
);
672 throw new ArgumentException (exception
);
677 // Used by internal constructor which has a SQL Server typename
678 private SqlDbType
FrameworkDbTypeFromName (string dbTypeName
)
680 switch (dbTypeName
.ToLower ()) {
682 return SqlDbType
.BigInt
;
684 return SqlDbType
.Binary
;
686 return SqlDbType
.Bit
;
688 return SqlDbType
.Char
;
690 return SqlDbType
.DateTime
;
692 return SqlDbType
.Decimal
;
694 return SqlDbType
.Float
;
696 return SqlDbType
.Image
;
698 return SqlDbType
.Int
;
700 return SqlDbType
.Money
;
702 return SqlDbType
.NChar
;
704 return SqlDbType
.NText
;
706 return SqlDbType
.NVarChar
;
708 return SqlDbType
.Real
;
709 case "smalldatetime":
710 return SqlDbType
.SmallDateTime
;
712 return SqlDbType
.SmallInt
;
714 return SqlDbType
.SmallMoney
;
716 return SqlDbType
.Text
;
718 return SqlDbType
.Timestamp
;
720 return SqlDbType
.TinyInt
;
721 case "uniqueidentifier":
722 return SqlDbType
.UniqueIdentifier
;
724 return SqlDbType
.VarBinary
;
726 return SqlDbType
.VarChar
;
728 return SqlDbType
.Variant
;
730 return SqlDbType
.Xml
;
732 return SqlDbType
.Variant
;
736 // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
737 // string representation of the type name. If the SqlDbType is not convertible
738 // to a DbType, throw an exception.
739 internal void SetSqlDbType (SqlDbType type
)
742 case SqlDbType
.BigInt
:
743 MetaParameter
.TypeName
= "bigint";
744 dbType
= DbType
.Int64
;
746 case SqlDbType
.Binary
:
747 MetaParameter
.TypeName
= "binary";
748 dbType
= DbType
.Binary
;
749 MetaParameter
.IsVariableSizeType
= true;
751 case SqlDbType
.Timestamp
:
752 MetaParameter
.TypeName
= "timestamp";
753 dbType
= DbType
.Binary
;
755 case SqlDbType
.VarBinary
:
756 MetaParameter
.TypeName
= "varbinary";
757 dbType
= DbType
.Binary
;
758 MetaParameter
.IsVariableSizeType
= true;
761 MetaParameter
.TypeName
= "bit";
762 dbType
= DbType
.Boolean
;
765 MetaParameter
.TypeName
= "char";
766 dbType
= DbType
.AnsiStringFixedLength
;
767 MetaParameter
.IsVariableSizeType
= true;
769 case SqlDbType
.DateTime
:
770 MetaParameter
.TypeName
= "datetime";
771 dbType
= DbType
.DateTime
;
773 case SqlDbType
.SmallDateTime
:
774 MetaParameter
.TypeName
= "smalldatetime";
775 dbType
= DbType
.DateTime
;
777 case SqlDbType
.Decimal
:
778 MetaParameter
.TypeName
= "decimal";
779 dbType
= DbType
.Decimal
;
781 case SqlDbType
.Float
:
782 MetaParameter
.TypeName
= "float";
783 dbType
= DbType
.Double
;
785 case SqlDbType
.Image
:
786 MetaParameter
.TypeName
= "image";
787 dbType
= DbType
.Binary
;
788 MetaParameter
.IsVariableSizeType
= true;
791 MetaParameter
.TypeName
= "int";
792 dbType
= DbType
.Int32
;
794 case SqlDbType
.Money
:
795 MetaParameter
.TypeName
= "money";
796 dbType
= DbType
.Currency
;
798 case SqlDbType
.SmallMoney
:
799 MetaParameter
.TypeName
= "smallmoney";
800 dbType
= DbType
.Currency
;
802 case SqlDbType
.NChar
:
803 MetaParameter
.TypeName
= "nchar";
804 dbType
= DbType
.StringFixedLength
;
805 MetaParameter
.IsVariableSizeType
= true;
807 case SqlDbType
.NText
:
808 MetaParameter
.TypeName
= "ntext";
809 dbType
= DbType
.String
;
810 MetaParameter
.IsVariableSizeType
= true;
812 case SqlDbType
.NVarChar
:
813 MetaParameter
.TypeName
= "nvarchar";
814 dbType
= DbType
.String
;
815 MetaParameter
.IsVariableSizeType
= true;
818 MetaParameter
.TypeName
= "real";
819 dbType
= DbType
.Single
;
821 case SqlDbType
.SmallInt
:
822 MetaParameter
.TypeName
= "smallint";
823 dbType
= DbType
.Int16
;
826 MetaParameter
.TypeName
= "text";
827 dbType
= DbType
.AnsiString
;
828 MetaParameter
.IsVariableSizeType
= true;
830 case SqlDbType
.VarChar
:
831 MetaParameter
.TypeName
= "varchar";
832 dbType
= DbType
.AnsiString
;
833 MetaParameter
.IsVariableSizeType
= true;
835 case SqlDbType
.TinyInt
:
836 MetaParameter
.TypeName
= "tinyint";
837 dbType
= DbType
.Byte
;
839 case SqlDbType
.UniqueIdentifier
:
840 MetaParameter
.TypeName
= "uniqueidentifier";
841 dbType
= DbType
.Guid
;
843 case SqlDbType
.Variant
:
844 MetaParameter
.TypeName
= "sql_variant";
845 dbType
= DbType
.Object
;
848 MetaParameter
.TypeName
= "xml";
850 MetaParameter
.IsVariableSizeType
= true;
853 string exception
= String
.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type
);
854 throw new ArgumentOutOfRangeException ("SqlDbType", exception
);
859 public override string ToString()
861 return ParameterName
;
864 object GetFrameworkValue (object rawValue
, ref bool updated
)
868 updated
= typeChanged
|| updated
;
870 tdsValue
= SqlTypeToFrameworkType (rawValue
);
877 // TODO: Code copied from SqlDataReader, need a better approach
878 object GetSqlValue (object value)
883 case SqlDbType
.BigInt
:
884 if (value == DBNull
.Value
)
885 return SqlInt64
.Null
;
886 return (SqlInt64
) ((long) value);
887 case SqlDbType
.Binary
:
888 case SqlDbType
.Image
:
889 case SqlDbType
.VarBinary
:
890 case SqlDbType
.Timestamp
:
891 if (value == DBNull
.Value
)
892 return SqlBinary
.Null
;
893 return (SqlBinary
) (byte[]) value;
895 if (value == DBNull
.Value
)
896 return SqlBoolean
.Null
;
897 return (SqlBoolean
) ((bool) value);
899 case SqlDbType
.NChar
:
900 case SqlDbType
.NText
:
901 case SqlDbType
.NVarChar
:
903 case SqlDbType
.VarChar
:
904 if (value == DBNull
.Value
)
905 return SqlString
.Null
;
908 Type type
= value.GetType ();
909 if (type
== typeof (char))
910 str
= value.ToString ();
911 else if (type
== typeof (char[]))
912 str
= new String ((char[])value);
914 str
= ((string)value);
915 return (SqlString
) str
;
916 case SqlDbType
.DateTime
:
917 case SqlDbType
.SmallDateTime
:
918 if (value == DBNull
.Value
)
919 return SqlDateTime
.Null
;
920 return (SqlDateTime
) ((DateTime
) value);
921 case SqlDbType
.Decimal
:
922 if (value == DBNull
.Value
)
923 return SqlDecimal
.Null
;
924 if (value is TdsBigDecimal
)
925 return SqlDecimalExtensions
.FromTdsBigDecimal ((TdsBigDecimal
) value);
926 return (SqlDecimal
) ((decimal) value);
927 case SqlDbType
.Float
:
928 if (value == DBNull
.Value
)
929 return SqlDouble
.Null
;
930 return (SqlDouble
) ((double) value);
932 if (value == DBNull
.Value
)
933 return SqlInt32
.Null
;
934 return (SqlInt32
) ((int) value);
935 case SqlDbType
.Money
:
936 case SqlDbType
.SmallMoney
:
937 if (value == DBNull
.Value
)
938 return SqlMoney
.Null
;
939 return (SqlMoney
) ((decimal) value);
941 if (value == DBNull
.Value
)
942 return SqlSingle
.Null
;
943 return (SqlSingle
) ((float) value);
944 case SqlDbType
.UniqueIdentifier
:
945 if (value == DBNull
.Value
)
947 return (SqlGuid
) ((Guid
) value);
948 case SqlDbType
.SmallInt
:
949 if (value == DBNull
.Value
)
950 return SqlInt16
.Null
;
951 return (SqlInt16
) ((short) value);
952 case SqlDbType
.TinyInt
:
953 if (value == DBNull
.Value
)
955 return (SqlByte
) ((byte) value);
957 if (value == DBNull
.Value
)
959 return (SqlXml
) value;
961 throw new NotImplementedException ("Type '" + sqlDbType
+ "' not implemented.");
965 object SqlTypeToFrameworkType (object value)
967 INullable nullable
= value as INullable
;
968 if (nullable
== null)
969 return ConvertToFrameworkType (value);
974 Type type
= value.GetType ();
975 // Map to .net type, as Mono TDS respects only types from .net
977 if (typeof (SqlString
) == type
) {
978 return ((SqlString
) value).Value
;
981 if (typeof (SqlInt16
) == type
) {
982 return ((SqlInt16
) value).Value
;
985 if (typeof (SqlInt32
) == type
) {
986 return ((SqlInt32
) value).Value
;
989 if (typeof (SqlDateTime
) == type
) {
990 return ((SqlDateTime
) value).Value
;
993 if (typeof (SqlInt64
) == type
) {
994 return ((SqlInt64
) value).Value
;
997 if (typeof (SqlBinary
) == type
) {
998 return ((SqlBinary
) value).Value
;
1001 if (typeof (SqlBytes
) == type
) {
1002 return ((SqlBytes
) value).Value
;
1005 if (typeof (SqlChars
) == type
) {
1006 return ((SqlChars
) value).Value
;
1009 if (typeof (SqlBoolean
) == type
) {
1010 return ((SqlBoolean
) value).Value
;
1013 if (typeof (SqlByte
) == type
) {
1014 return ((SqlByte
) value).Value
;
1017 if (typeof (SqlDecimal
) == type
) {
1018 return ((SqlDecimal
) value).Value
;
1021 if (typeof (SqlDouble
) == type
) {
1022 return ((SqlDouble
) value).Value
;
1025 if (typeof (SqlGuid
) == type
) {
1026 return ((SqlGuid
) value).Value
;
1029 if (typeof (SqlMoney
) == type
) {
1030 return ((SqlMoney
) value).Value
;
1033 if (typeof (SqlSingle
) == type
) {
1034 return ((SqlSingle
) value).Value
;
1040 internal object ConvertToFrameworkType (object value)
1042 if (value == null || value == DBNull
.Value
)
1044 if (sqlDbType
== SqlDbType
.Variant
)
1045 return metaParameter
.Value
;
1047 Type frameworkType
= SystemType
;
1048 if (frameworkType
== null)
1049 throw new NotImplementedException ("Type Not Supported : " + sqlDbType
.ToString());
1051 Type valueType
= value.GetType ();
1052 if (valueType
== frameworkType
)
1055 object sqlvalue
= null;
1058 sqlvalue
= ConvertToFrameworkType (value, frameworkType
);
1059 } catch (FormatException ex
) {
1060 throw new FormatException (string.Format (CultureInfo
.InvariantCulture
,
1061 "Parameter value could not be converted from {0} to {1}.",
1062 valueType
.Name
, frameworkType
.Name
), ex
);
1068 object ConvertToFrameworkType (object value, Type frameworkType
)
1070 object sqlvalue
= Convert
.ChangeType (value, frameworkType
);
1071 switch (sqlDbType
) {
1072 case SqlDbType
.Money
:
1073 case SqlDbType
.SmallMoney
:
1074 sqlvalue
= Decimal
.Round ((decimal) sqlvalue
, 4);
1080 public override void ResetDbType ()
1082 InferSqlType (Value
);
1085 public void ResetSqlDbType ()
1087 InferSqlType (Value
);
1090 #endregion // Methods