merge -r 53370:58178
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlParameter.cs
1 //
2 // System.Data.SqlClient.SqlParameter.cs
3 //
4 // Author:
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 //
11 // (C) Ximian, Inc. 2002
12 // Copyright (C) Tim Coleman, 2002
13 //
14
15 //
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 //
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
25 // 
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 // 
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 //
37
38 using Mono.Data.Tds;
39 using Mono.Data.Tds.Protocol;
40 using System;
41 using System.ComponentModel;
42 using System.Data;
43 using System.Data.Common;
44 #if NET_2_0
45 using System.Data.ProviderBase;
46 #endif // NET_2_0
47 using System.Data.SqlTypes;
48 using System.Runtime.InteropServices;
49 using System.Text;
50
51 namespace System.Data.SqlClient {
52         [TypeConverterAttribute (typeof (SqlParameterConverter))]
53 #if NET_2_0
54         public sealed class SqlParameter : DbParameterBase, IDbDataParameter, IDataParameter, ICloneable
55 #else
56         public sealed class SqlParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
57 #endif // NET_2_0
58         {
59                 #region Fields
60
61                 TdsMetaParameter metaParameter;
62
63                 SqlParameterCollection container = null;
64                 DbType dbType;
65                 ParameterDirection direction = ParameterDirection.Input;
66                 bool isNullable;
67                 bool isVariableSizeType = false;
68                 bool isTypeSet = false;
69                 int offset;
70                 SqlDbType sqlDbType;
71                 string sourceColumn;
72                 DataRowVersion sourceVersion;
73
74                 #endregion // Fields
75
76                 #region Constructors
77
78                 public SqlParameter () 
79                         : this (String.Empty, SqlDbType.NVarChar, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
80                 {
81                         isTypeSet = false;
82                 }
83
84                 public SqlParameter (string parameterName, object value) 
85                 {
86                         metaParameter = new TdsMetaParameter (parameterName, SqlTypeToFrameworkType (value));
87                         this.sourceVersion = DataRowVersion.Current;
88                         InferSqlType (value);
89                 }
90                 
91                 public SqlParameter (string parameterName, SqlDbType dbType) 
92                         : this (parameterName, dbType, 0, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
93                 {
94                 }
95
96                 public SqlParameter (string parameterName, SqlDbType dbType, int size) 
97                         : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, String.Empty, DataRowVersion.Current, null)
98                 {
99                 }
100                 
101                 public SqlParameter (string parameterName, SqlDbType dbType, int size, string sourceColumn) 
102                         : this (parameterName, dbType, size, ParameterDirection.Input, false, 0, 0, sourceColumn, DataRowVersion.Current, null)
103                 {
104                 }
105                 
106                 [EditorBrowsable (EditorBrowsableState.Advanced)]        
107                 public SqlParameter (string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, string sourceColumn, DataRowVersion sourceVersion, object value) 
108                 {
109                         metaParameter = new TdsMetaParameter (parameterName, size, 
110                                                               isNullable, precision, 
111                                                               scale, 
112                                                               SqlTypeToFrameworkType (value));
113                         SqlDbType = dbType;
114                         Direction = direction;
115                         SourceColumn = sourceColumn;
116                         SourceVersion = sourceVersion;
117                 }
118
119                 // This constructor is used internally to construct a
120                 // SqlParameter.  The value array comes from sp_procedure_params_rowset.
121                 // This is in SqlCommand.DeriveParameters.
122                 internal SqlParameter (object[] dbValues) 
123                         : this (dbValues [3].ToString (), String.Empty)
124                 {
125                         Precision = 0;
126                         Scale = 0;
127                         Direction = ParameterDirection.Input;
128
129                         ParameterName = (string) dbValues[3];
130
131                         switch ((short) dbValues[5]) {
132                         case 1:
133                                 Direction = ParameterDirection.Input;
134                                 break;
135                         case 2:
136                                 Direction = ParameterDirection.Output;
137                                 break;
138                         case 3:
139                                 Direction = ParameterDirection.InputOutput;
140                                 break;
141                         case 4:
142                                 Direction = ParameterDirection.ReturnValue;
143                                 break;
144                         default:
145                                 Direction = ParameterDirection.Input;
146                                 break;
147                         }
148                         IsNullable = (dbValues [8] != null && 
149                                       dbValues [8] != DBNull.Value) ? (bool) dbValues [8] : false;
150
151                         if (dbValues [12] != null && dbValues [12] != DBNull.Value)
152                                 Precision = (byte) ((short) dbValues [12]);
153
154                         if (dbValues [13] != null && dbValues [13] != DBNull.Value)
155                                 Scale = (byte) ( (short) dbValues [13]);
156
157                         SetDbTypeName ((string) dbValues [16]);
158                 }
159
160                 #endregion // Constructors
161
162                 #region Properties
163
164                 // Used to ensure that only one collection can contain this
165                 // parameter
166                 internal SqlParameterCollection Container {
167                         get { return container; }
168                         set { container = value; }
169                 }
170
171                 internal void CheckIfInitialized ()
172                 {
173                         if (!isTypeSet)
174                                 throw new Exception ("all parameters to have an explicity set type");
175
176                         if (isVariableSizeType) {
177                                 if (SqlDbType == SqlDbType.Decimal && Precision == 0)
178                                         throw new Exception ("Parameter of type 'Decimal' have an explicitly set Precision and Scale");
179                                 else if (Size == 0)
180                                         throw new Exception ("all variable length parameters to have an explicitly set non-zero Size");
181                         }
182                 }
183
184 #if ONLY_1_0 || ONLY_1_1        
185                 [Browsable (false)]
186                 [DataSysDescription ("The parameter generic type.")]
187                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
188                 [RefreshProperties (RefreshProperties.All)]
189 #endif                                                   
190                 [DataCategory ("Data")]
191                 public 
192 #if NET_2_0
193                 override
194 #endif // NET_2_0
195          DbType DbType {
196                         get { return dbType; }
197                         set { 
198                                 SetDbType (value); 
199                                 isTypeSet = true;
200                         }
201                 }
202
203                 [DataCategory ("Data")]
204 #if ONLY_1_0 || ONLY_1_1
205                 [DataSysDescription ("Input, output, or bidirectional parameter.")]
206                 [DefaultValue (ParameterDirection.Input)]
207 #endif
208 #if NET_2_0
209                 [RefreshProperties (RefreshProperties.All)]
210 #endif
211                 public 
212 #if NET_2_0
213                 override
214 #endif // NET_2_0
215          ParameterDirection Direction {
216                         get { return direction; }
217                         set { 
218                                 direction = value; 
219                                 switch( direction ) {
220                                         case ParameterDirection.Output:
221                                         MetaParameter.Direction = TdsParameterDirection.Output;
222                                                 break;
223                                         case ParameterDirection.InputOutput:
224                                                 MetaParameter.Direction = TdsParameterDirection.InputOutput;
225                                                 break;
226                                         case ParameterDirection.ReturnValue:
227                                                 MetaParameter.Direction = TdsParameterDirection.ReturnValue;
228                                                 break;
229                                 }
230                         }
231                 }
232
233                 internal TdsMetaParameter MetaParameter {
234                         get { return metaParameter; }
235                 }
236
237                 string IDataParameter.ParameterName {
238                         get { return metaParameter.ParameterName; }
239                         set { metaParameter.ParameterName = value; }
240                 }
241
242 #if ONLY_1_0 || ONLY_1_1
243                 [Browsable (false)]
244                 [DataSysDescription ("a design-time property used for strongly typed code-generation.")]
245                 [DefaultValue (false)]
246                 [DesignOnly (true)]
247                 [EditorBrowsable (EditorBrowsableState.Advanced)]        
248 #endif
249                 public 
250 #if NET_2_0
251                 override
252 #endif // NET_2_0
253          bool IsNullable        {
254                         get { return metaParameter.IsNullable; }
255                         set { metaParameter.IsNullable = value; }
256                 }
257
258                 [Browsable (false)]
259                 [DataCategory ("Data")]
260 #if ONLY_1_0 || ONLY_1_1
261                 [DataSysDescription ("Offset in variable length data types.")]
262                 [DefaultValue (0)]
263 #endif
264 #if NET_2_0
265                 [EditorBrowsable (EditorBrowsableState.Advanced)]
266 #endif
267                 public 
268 #if NET_2_0
269                 override
270 #endif
271                 int Offset {
272                         get { return offset; }
273                         set { offset = value; }
274                 }
275         
276 #if ONLY_1_0 || ONLY_1_1        
277                 [DataSysDescription ("Name of the parameter, like '@p1'")]
278                 [DefaultValue ("")]
279 #endif
280                 public 
281 #if NET_2_0
282                 override
283 #endif // NET_2_0
284          string ParameterName {
285                         get { return metaParameter.ParameterName; }
286                         set { metaParameter.ParameterName = value; }
287                 }
288
289                 [DataCategory ("Data")]
290 #if ONLY_1_0 || ONLY_1_1
291                 [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
292                 [DefaultValue (0)]
293 #endif
294 #if NET_2_0
295                 [Browsable (false)]
296                 [EditorBrowsable (EditorBrowsableState.Never)]
297 #endif          
298                 public 
299 #if NET_2_0
300                 override
301 #endif // NET_2_0
302          byte Precision {
303                         get { return metaParameter.Precision; }
304                         set { metaParameter.Precision = value; }
305                 }
306
307                 [DataCategory ("Data")]
308 #if ONLY_1_0 || ONLY_1_1
309                 [DataSysDescription ("For decimal, numeric, varnumeric DBTypes.")]
310                 [DefaultValue (0)]
311 #endif
312 #if NET_2_0
313                 [Browsable (false)]
314                 [EditorBrowsable (EditorBrowsableState.Never)]
315 #endif
316                 public 
317 #if NET_2_0
318                 override
319 #endif // NET_2_0
320          byte Scale {
321                         get { return metaParameter.Scale; }
322                         set { metaParameter.Scale = value; }
323                 }
324
325                 [DataCategory ("Data")]
326 #if ONLY_1_0 || ONLY_1_1
327                 [DataSysDescription ("Size of variable length data types (string & arrays).")]
328                 [DefaultValue (0)]
329 #endif
330                 public 
331 #if NET_2_0
332                 override
333 #endif // NET_2_0
334          int Size {
335                         get { return metaParameter.Size; }
336                         set { metaParameter.Size = value; }
337                 }
338
339                 [DataCategory ("Data")]
340 #if ONLY_1_0 || ONLY_1_1
341                 [DataSysDescription ("When used by a DataAdapter.Update, the source column name that is used to find the DataSetColumn name in the ColumnMappings. This is to copy a value between the parameter and a datarow.")]
342                 [DefaultValue ("")]
343 #endif
344                 public 
345 #if NET_2_0
346                 override
347 #endif // NET_2_0
348          string SourceColumn {
349                         get { return sourceColumn; }
350                         set { sourceColumn = value; }
351                 }
352
353                 [DataCategory ("Data")]
354 #if ONLY_1_0 || ONLY_1_1
355                 [DataSysDescription ("When used by a DataAdapter.Update (UpdateCommand only), the version of the DataRow value that is used to update the data source.")]
356                 [DefaultValue (DataRowVersion.Current)]
357 #endif
358                 public 
359 #if NET_2_0
360                 override
361 #endif // NET_2_0
362          DataRowVersion SourceVersion {
363                         get { return sourceVersion; }
364                         set { sourceVersion = value; }
365                 }
366                 
367                 [DataCategory ("Data")]
368 #if ONLY_1_0 || ONLY_1_1
369                 [DataSysDescription ("The parameter native type.")]
370                 [DefaultValue (SqlDbType.NVarChar)]
371 #endif
372                 [RefreshProperties (RefreshProperties.All)]
373 #if NET_2_0
374                 [DbProviderSpecificTypeProperty(true)]
375 #endif
376                 public SqlDbType SqlDbType {
377                         get { return sqlDbType; }
378                         set { 
379                                 SetSqlDbType (value); 
380                                 isTypeSet = true;
381                         }
382                 }
383
384                 [DataCategory ("Data")]
385 #if ONLY_1_0 || ONLY_1_1
386                 [DataSysDescription ("Value of the parameter.")]
387                 [DefaultValue (null)]
388 #endif
389                 [TypeConverterAttribute (typeof (StringConverter))]
390 #if NET_2_0
391                 [RefreshProperties (RefreshProperties.All)]             
392 #endif
393                 public 
394 #if NET_2_0
395                 override
396 #endif // NET_2_0
397          object Value {
398                         get { return metaParameter.Value; }
399                         set { 
400                                 if (!isTypeSet)
401                                         InferSqlType (value);
402                                 metaParameter.Value = SqlTypeToFrameworkType (value);
403                         }
404                 }
405
406 //#if NET_2_0
407 //              public SqlCompareOptions CompareInfo{
408
409 //#endif
410 #if NET_2_0
411                 public override bool SourceColumnNullMapping {
412                         get { return false ; }
413                         set { }
414                 }
415 #endif
416
417                 #endregion // Properties
418
419                 #region Methods
420
421                 object ICloneable.Clone ()
422                 {
423                         return new SqlParameter (ParameterName, SqlDbType, Size, Direction, IsNullable, Precision, Scale, SourceColumn, SourceVersion, Value);
424                 }
425
426                 // If the value is set without the DbType/SqlDbType being set, then we
427                 // infer type information.
428                 private void InferSqlType (object value)
429                 {
430                         Type type = value.GetType ();
431
432                         string exception = String.Format ("The parameter data type of {0} is invalid.", type.Name);
433
434                         switch (type.FullName) {
435                         case "System.Int64":
436                         case "System.Data.SqlTypes.SqlInt64":
437                                 SetSqlDbType (SqlDbType.BigInt);
438                                 break;
439                         case "System.Boolean":
440                         case "System.Data.SqlTypes.SqlBoolean":
441                                 SetSqlDbType (SqlDbType.Bit);
442                                 break;
443                         case "System.String":
444                         case "System.Data.SqlTypes.SqlString":
445                                 SetSqlDbType (SqlDbType.NVarChar);
446                                 break;
447                         case "System.DateTime":
448                         case "System.Data.SqlTypes.SqlDateTime":
449                                 SetSqlDbType (SqlDbType.DateTime);
450                                 break;
451                         case "System.Decimal":
452                         case "System.Data.SqlTypes.SqlDecimal":
453                                 SetSqlDbType (SqlDbType.Decimal);
454                                 break;
455                         case "System.Double":
456                         case "System.Data.SqlTypes.SqlDouble":
457                                 SetSqlDbType (SqlDbType.Float);
458                                 break;
459                         case "System.Byte[]":
460                         case "System.Data.SqlTypes.SqlBinary":
461                                 SetSqlDbType (SqlDbType.VarBinary);
462                                 break;
463                         case "System.Byte":
464                         case "System.Data.SqlTypes.SqlByte":
465                                 SetSqlDbType (SqlDbType.TinyInt);
466                                 break;
467                         case "System.Int32":
468                         case "System.Data.SqlTypes.SqlInt32":
469                                 SetSqlDbType (SqlDbType.Int);
470                                 break;
471                         case "System.Single":
472                         case "System.Data.SqlTypes.Single":
473                                 SetSqlDbType (SqlDbType.Real);
474                                 break;
475                         case "System.Int16":
476                         case "System.Data.SqlTypes.SqlInt16":
477                                 SetSqlDbType (SqlDbType.SmallInt);
478                                 break;
479                         case "System.Guid":
480                         case "System.Data.SqlTypes.SqlGuid":
481                                 SetSqlDbType (SqlDbType.UniqueIdentifier);
482                                 break;
483                         case "System.Money":
484                         case "System.SmallMoney":
485                         case "System.Data.SqlTypes.SqlMoney":
486                                 SetSqlDbType (SqlDbType.Money);
487                                 break;
488                         case "System.Object":
489                         case "System.DBNull":
490                                 SetSqlDbType (SqlDbType.Variant); // variant can contain numeric,
491                                                                 //string,binary or data and also nul                                                                //values, so we can later resolve                                                                   // it to correct type.  
492                                 break;  
493                         default:
494                                 throw new ArgumentException (exception);                                
495                         }
496                 }
497
498                 // When the DbType is set, we also set the SqlDbType, as well as the SQL Server
499                 // string representation of the type name.  If the DbType is not convertible
500                 // to an SqlDbType, throw an exception.
501                 private void SetDbType (DbType type)
502                 {
503                         string exception = String.Format ("No mapping exists from DbType {0} to a known SqlDbType.", type);
504
505                         switch (type) {
506                         case DbType.AnsiString:
507                                 MetaParameter.TypeName = "varchar";
508                                 sqlDbType = SqlDbType.VarChar;
509                                 isVariableSizeType = true;
510                                 break;
511                         case DbType.AnsiStringFixedLength:
512                                 MetaParameter.TypeName = "char";
513                                 sqlDbType = SqlDbType.Char;
514                                 isVariableSizeType = true;
515                                 break;
516                         case DbType.Binary:
517                                 MetaParameter.TypeName = "varbinary";
518                                 sqlDbType = SqlDbType.VarBinary;
519                                 isVariableSizeType = true;
520                                 break;
521                         case DbType.Boolean:
522                                 MetaParameter.TypeName = "bit";
523                                 sqlDbType = SqlDbType.Bit;
524                                 break;
525                         case DbType.Byte:
526                                 MetaParameter.TypeName = "tinyint";
527                                 sqlDbType = SqlDbType.TinyInt;
528                                 break;
529                         case DbType.Currency:
530                                 sqlDbType = SqlDbType.Money;
531                                 MetaParameter.TypeName = "money";
532                                 break;
533                         case DbType.Date:
534                         case DbType.DateTime:
535                                 MetaParameter.TypeName = "datetime";
536                                 sqlDbType = SqlDbType.DateTime;
537                                 break;
538                         case DbType.Decimal:
539                                 MetaParameter.TypeName = "decimal";
540                                 sqlDbType = SqlDbType.Decimal;
541                                 break;
542                         case DbType.Double:
543                                 MetaParameter.TypeName = "float";
544                                 sqlDbType = SqlDbType.Float;
545                                 break;
546                         case DbType.Guid:
547                                 MetaParameter.TypeName = "uniqueidentifier";
548                                 sqlDbType = SqlDbType.UniqueIdentifier;
549                                 break;
550                         case DbType.Int16:
551                                 MetaParameter.TypeName = "smallint";
552                                 sqlDbType = SqlDbType.SmallInt;
553                                 break;
554                         case DbType.Int32:
555                                 MetaParameter.TypeName = "int";
556                                 sqlDbType = SqlDbType.Int;
557                                 break;
558                         case DbType.Int64:
559                                 MetaParameter.TypeName = "bigint";
560                                 sqlDbType = SqlDbType.BigInt;
561                                 break;
562                         case DbType.Object:
563                                 MetaParameter.TypeName = "sql_variant";
564                                 sqlDbType = SqlDbType.Variant;
565                                 break;
566                         case DbType.Single:
567                                 MetaParameter.TypeName = "real";
568                                 sqlDbType = SqlDbType.Real;
569                                 break;
570                         case DbType.String:
571                                 MetaParameter.TypeName = "nvarchar";
572                                 sqlDbType = SqlDbType.NVarChar;
573                                 isVariableSizeType = true;
574                                 break;
575                         case DbType.StringFixedLength:
576                                 MetaParameter.TypeName = "nchar";
577                                 sqlDbType = SqlDbType.NChar;
578                                 isVariableSizeType = true;
579                                 break;
580                         case DbType.Time:
581                                 MetaParameter.TypeName = "datetime";
582                                 sqlDbType = SqlDbType.DateTime;
583                                 break;
584                         default:
585                                 throw new ArgumentException (exception);
586                         }
587                         dbType = type;
588                 }
589
590                 // Used by internal constructor which has a SQL Server typename
591                 private void SetDbTypeName (string dbTypeName)
592                 {
593                         switch (dbTypeName.ToLower ()) {        
594                         case "bigint":
595                                 SqlDbType = SqlDbType.BigInt;
596                                 break;
597                         case "binary":
598                                 SqlDbType = SqlDbType.Binary;
599                                 break;
600                         case "bit":
601                                 SqlDbType = SqlDbType.Bit;
602                                 break;
603                         case "char":
604                                 SqlDbType = SqlDbType.Char;
605                                 break;
606                         case "datetime":
607                                 SqlDbType = SqlDbType.DateTime;
608                                 break;
609                         case "decimal":
610                                 SqlDbType = SqlDbType.Decimal;
611                                 break;
612                         case "float":
613                                 SqlDbType = SqlDbType.Float;
614                                 break;
615                         case "image":
616                                 SqlDbType = SqlDbType.Image;
617                                 break;
618                         case "int":
619                                 SqlDbType = SqlDbType.Int;
620                                 break;
621                         case "money":
622                                 SqlDbType = SqlDbType.Money;
623                                 break;
624                         case "nchar":
625                                 SqlDbType = SqlDbType.NChar;
626                                 break;
627                         case "ntext":
628                                 SqlDbType = SqlDbType.NText;
629                                 break;
630                         case "nvarchar":
631                                 SqlDbType = SqlDbType.NVarChar;
632                                 break;
633                         case "real":
634                                 SqlDbType = SqlDbType.Real;
635                                 break;
636                         case "smalldatetime":
637                                 SqlDbType = SqlDbType.SmallDateTime;
638                                 break;
639                         case "smallint":
640                                 SqlDbType = SqlDbType.SmallInt;
641                                 break;
642                         case "smallmoney":
643                                 SqlDbType = SqlDbType.SmallMoney;
644                                 break;
645                         case "text":
646                                 SqlDbType = SqlDbType.Text;
647                                 break;
648                         case "timestamp":
649                                 SqlDbType = SqlDbType.Timestamp;
650                                 break;
651                         case "tinyint":
652                                 SqlDbType = SqlDbType.TinyInt;
653                                 break;
654                         case "uniqueidentifier":
655                                 SqlDbType = SqlDbType.UniqueIdentifier;
656                                 break;
657                         case "varbinary":
658                                 SqlDbType = SqlDbType.VarBinary;
659                                 break;
660                         case "varchar":
661                                 SqlDbType = SqlDbType.VarChar;
662                                 break;
663                         default:
664                                 SqlDbType = SqlDbType.Variant;
665                                 break;
666                         }
667                 }
668
669                 // When the SqlDbType is set, we also set the DbType, as well as the SQL Server
670                 // string representation of the type name.  If the SqlDbType is not convertible
671                 // to a DbType, throw an exception.
672                 private void SetSqlDbType (SqlDbType type)
673                 {
674                         string exception = String.Format ("No mapping exists from SqlDbType {0} to a known DbType.", type);
675
676                         switch (type) {
677                         case SqlDbType.BigInt:
678                                 MetaParameter.TypeName = "bigint";
679                                 dbType = DbType.Int64;
680                                 break;
681                         case SqlDbType.Binary:
682                                 MetaParameter.TypeName = "binary";
683                                 dbType = DbType.Binary;
684                                 isVariableSizeType = true;
685                                 break;
686                         case SqlDbType.Timestamp:
687                                 MetaParameter.TypeName = "timestamp";
688                                 dbType = DbType.Binary;
689                                 break;
690                         case SqlDbType.VarBinary:
691                                 MetaParameter.TypeName = "varbinary";
692                                 dbType = DbType.Binary;
693                                 isVariableSizeType = true;
694                                 break;
695                         case SqlDbType.Bit:
696                                 MetaParameter.TypeName = "bit";
697                                 dbType = DbType.Boolean;
698                                 break;
699                         case SqlDbType.Char:
700                                 MetaParameter.TypeName = "char";
701                                 dbType = DbType.AnsiStringFixedLength;
702                                 isVariableSizeType = true;
703                                 break;
704                         case SqlDbType.DateTime:
705                                 MetaParameter.TypeName = "datetime";
706                                 dbType = DbType.DateTime;
707                                 break;
708                         case SqlDbType.SmallDateTime:
709                                 MetaParameter.TypeName = "smalldatetime";
710                                 dbType = DbType.DateTime;
711                                 break;
712                         case SqlDbType.Decimal:
713                                 MetaParameter.TypeName = "decimal";
714                                 dbType = DbType.Decimal;
715                                 break;
716                         case SqlDbType.Float:
717                                 MetaParameter.TypeName = "float";
718                                 dbType = DbType.Double;
719                                 break;
720                         case SqlDbType.Image:
721                                 MetaParameter.TypeName = "image";
722                                 dbType = DbType.Binary;
723                                 isVariableSizeType = true;
724                                 break;
725                         case SqlDbType.Int:
726                                 MetaParameter.TypeName = "int";
727                                 dbType = DbType.Int32;
728                                 break;
729                         case SqlDbType.Money:
730                                 MetaParameter.TypeName = "money";
731                                 dbType = DbType.Currency;
732                                 break;
733                         case SqlDbType.SmallMoney:
734                                 MetaParameter.TypeName = "smallmoney";
735                                 dbType = DbType.Currency;
736                                 break;
737                         case SqlDbType.NChar:
738                                 MetaParameter.TypeName = "nchar";
739                                 dbType = DbType.StringFixedLength;
740                                 isVariableSizeType = true;
741                                 break;
742                         case SqlDbType.NText:
743                                 MetaParameter.TypeName = "ntext";
744                                 dbType = DbType.String;
745                                 isVariableSizeType = true;
746                                 break;
747                         case SqlDbType.NVarChar:
748                                 MetaParameter.TypeName = "nvarchar";
749                                 dbType = DbType.String;
750                                 isVariableSizeType = true;
751                                 break;
752                         case SqlDbType.Real:
753                                 MetaParameter.TypeName = "real";
754                                 dbType = DbType.Single;
755                                 break;
756                         case SqlDbType.SmallInt:
757                                 MetaParameter.TypeName = "smallint";
758                                 dbType = DbType.Int16;
759                                 break;
760                         case SqlDbType.Text:
761                                 MetaParameter.TypeName = "text";
762                                 dbType = DbType.AnsiString;
763                                 isVariableSizeType = true;
764                                 break;
765                         case SqlDbType.VarChar:
766                                 MetaParameter.TypeName = "varchar";
767                                 dbType = DbType.AnsiString;
768                                 isVariableSizeType = true;
769                                 break;
770                         case SqlDbType.TinyInt:
771                                 MetaParameter.TypeName = "tinyint";
772                                 dbType = DbType.Byte;
773                                 break;
774                         case SqlDbType.UniqueIdentifier:
775                                 MetaParameter.TypeName = "uniqueidentifier";
776                                 dbType = DbType.Guid;
777                                 break;
778                         case SqlDbType.Variant:
779                                 MetaParameter.TypeName = "sql_variant";
780                                 dbType = DbType.Object;
781                                 break;
782                         default:
783                                 throw new ArgumentException (exception);
784                         }
785                         sqlDbType = type;
786                 }
787
788                 public override string ToString() 
789                 {
790                         return ParameterName;
791                 }
792
793                 private object SqlTypeToFrameworkType (object value)
794                 {
795                         if (! (value is INullable)) // if the value is not SqlType
796                                 return value;
797
798                         // Map to .net type, as Mono TDS respects only types from .net
799                         switch (value.GetType ().FullName) {
800                         case "System.Data.SqlTypes.SqlBinary":
801                                 return ( (SqlBinary) value).Value;
802                         case "System.Data.SqlTypes.SqlBoolean":
803                                 return ( (SqlBoolean) value).Value;
804                         case "System.Data.SqlTypes.SqlByte":
805                                 return ( (SqlByte) value).Value;
806                         case "System.Data.SqlTypes.SqlDateTime":
807                                 return ( (SqlDateTime) value).Value;
808                         case "System.Data.SqlTypes.SqlDecimal":
809                                 return ( (SqlDecimal) value).Value;
810                         case "System.Data.SqlTypes.SqlDouble":
811                                 return ( (SqlDouble) value).Value;
812                         case "System.Data.SqlTypes.SqlGuid":
813                                 return ( (SqlGuid) value).Value;
814                         case "System.Data.SqlTypes.SqlInt16":
815                                 return ( (SqlInt16) value).Value;
816                         case "System.Data.SqlTypes.SqlInt32 ":
817                                 return ( (SqlInt32 ) value).Value;
818                         case "System.Data.SqlTypes.SqlInt64":
819                                 return ( (SqlInt64) value).Value;
820                         case "System.Data.SqlTypes.SqlMoney":
821                                 return ( (SqlMoney) value).Value;
822                         case "System.Data.SqlTypes.SqlSingle":
823                                 return ( (SqlSingle) value).Value;
824                         case "System.Data.SqlTypes.SqlString":
825                                 return ( (SqlString) value).Value;
826                         }
827                         return value;
828                 }
829
830 #if NET_2_0
831                 [MonoTODO]
832                 public override void CopyTo (DbParameter param)
833                 {
834                         throw new NotImplementedException ();
835                 }
836                 
837                 [MonoTODO]
838                 public override void ResetDbType ()
839                 {
840                         throw new NotImplementedException ();
841                 }
842 #endif // NET_2_0
843
844                 #endregion // Methods
845         }
846 }