BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.Data / Microsoft.SqlServer.Server / SqlMetaData.cs
1 //
2 // Microsoft.SqlServer.Server.SqlMetaData
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2003
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34
35 using System;
36 using System.Data;
37 using System.Threading;
38 using System.Data.SqlTypes;
39
40 namespace Microsoft.SqlServer.Server {
41         public sealed class SqlMetaData
42         {
43                 #region Fields
44
45                 SqlCompareOptions compareOptions = SqlCompareOptions.None;
46                 string databaseName = null;
47                 long localeId = 0L;
48                 long maxLength = 0L;
49                 string name;
50                 byte precision = 10;
51                 byte scale = 0;
52                 string owningSchema = null;
53                 string objectName = null;
54                 SqlDbType sqlDbType = SqlDbType.NVarChar;
55                 DbType dbType = DbType.String;
56                 Type type = typeof (string);
57
58                 #endregion // Fields
59
60                 #region Constructors
61
62                 public SqlMetaData (string name, SqlDbType sqlDbType)
63                 {
64                         if (name == null)
65                                 throw new ArgumentNullException ("name can not be null");
66                         switch (sqlDbType) {
67                         case SqlDbType.Bit:
68                                 maxLength = 1;
69                                 precision = 1;
70                                 scale = 0;
71                                 localeId = 0;
72                                 compareOptions = SqlCompareOptions.None;
73                                 dbType = DbType.Boolean;
74                                 type = typeof (bool);
75                                 break;
76                         case SqlDbType.BigInt:
77                                 maxLength = 8;
78                                 precision = 19;
79                                 scale = 0;
80                                 localeId = 0;
81                                 compareOptions = SqlCompareOptions.None;
82                                 dbType = DbType.Int64;
83                                 type = typeof (long);
84                                 break;
85                         case SqlDbType.DateTime:
86                                 maxLength = 8;
87                                 precision = 23;
88                                 scale = 3;
89                                 localeId = 0;
90                                 compareOptions = SqlCompareOptions.None;
91                                 dbType = DbType.DateTime;
92                                 type = typeof (DateTime);
93                                 break;
94                         case SqlDbType.Decimal:
95                                 maxLength = 9;
96                                 precision = 18;
97                                 scale = 0;
98                                 localeId = 0;
99                                 compareOptions = SqlCompareOptions.None;
100                                 dbType = DbType.Decimal;
101                                 type = typeof (decimal);
102                                 break;
103                         case SqlDbType.Float:
104                                 maxLength = 8;
105                                 precision = 53;
106                                 scale = 0;
107                                 localeId = 0;
108                                 compareOptions = SqlCompareOptions.None;
109                                 dbType = DbType.Double;
110                                 type = typeof (float);
111                                 break;
112                         case SqlDbType.Int:
113                                 maxLength = 4;
114                                 precision = 10;
115                                 scale = 0;
116                                 localeId = 0;
117                                 compareOptions = SqlCompareOptions.None;
118                                 dbType = DbType.Int32;
119                                 type = typeof (int);
120                                 break;
121                         case SqlDbType.Money:
122                                 maxLength = 8;
123                                 precision = 19;
124                                 scale = 4;
125                                 localeId = 0;
126                                 compareOptions = SqlCompareOptions.None;
127                                 dbType = DbType.Currency;
128                                 type = typeof (double);
129                                 break;
130                           /*
131                         case SqlDbType.Numeric:
132                                 maxLength = ;
133                                 precision = ;
134                                 scale = ;
135                                 localeId = 0;
136                                 compareOptions = SqlCompareOptions.None;
137                                 break;
138                           */
139                         case SqlDbType.SmallDateTime:
140                                 maxLength = 4;
141                                 precision = 16;
142                                 scale = 0;
143                                 localeId = 0;
144                                 compareOptions = SqlCompareOptions.None;
145                                 dbType = DbType.DateTime;
146                                 type = typeof (DateTime);
147                                 break;
148                         case SqlDbType.SmallInt:
149                                 maxLength = 2;
150                                 precision = 5;
151                                 scale = 0;
152                                 localeId = 0;
153                                 compareOptions = SqlCompareOptions.None;
154                                 dbType = DbType.Int16;
155                                 type = typeof (short);
156                                 break;
157                         case SqlDbType.SmallMoney:
158                                 maxLength = 4;
159                                 precision = 10;
160                                 scale = 4;
161                                 localeId = 0;
162                                 compareOptions = SqlCompareOptions.None;
163                                 dbType = DbType.Currency;
164                                 type = typeof (double);
165                                 break;
166                         case SqlDbType.Timestamp:
167                                 maxLength = 8;
168                                 precision = 0;
169                                 scale = 0;
170                                 localeId = 0;
171                                 compareOptions = SqlCompareOptions.None;
172                                 dbType = DbType.DateTime;
173                                 type = typeof (DateTime);
174                                 break;
175                         case SqlDbType.TinyInt:
176                                 maxLength = 1;
177                                 precision = 3;
178                                 scale = 0;
179                                 localeId = 0;
180                                 compareOptions = SqlCompareOptions.None;
181                                 dbType = DbType.Int16;
182                                 type = typeof (short);
183                                 break;
184                         case SqlDbType.UniqueIdentifier:
185                                 maxLength = 16;
186                                 precision = 0;
187                                 scale = 0;
188                                 localeId = 0;
189                                 compareOptions = SqlCompareOptions.None;
190                                 dbType = DbType.Guid;
191                                 type = typeof (Guid);
192                                 break;
193                         case SqlDbType.Xml:
194                                 maxLength = -1;
195                                 precision = 0;
196                                 scale = 0;
197                                 localeId = 0;
198                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
199                                 dbType = DbType.Xml;
200                                 type = typeof (string);
201                                 break;
202                         default:
203                                 throw new ArgumentException ("SqlDbType not supported");
204                         }
205                         this.name = name;
206                         this.sqlDbType = sqlDbType;
207                 }
208
209                 public SqlMetaData (string name, SqlDbType sqlDbType, long maxLength)
210                 {
211                         if (name == null)
212                                 throw new ArgumentNullException ("name can not be null");
213                         switch (sqlDbType) {
214                         case SqlDbType.Binary:
215                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
216                                 dbType = DbType.Binary;
217                                 type = typeof (byte []);
218                                 break;
219                         case SqlDbType.Char:
220                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
221                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
222                                 dbType = DbType.AnsiStringFixedLength;
223                                 type = typeof (string);
224                                 break;
225                         case SqlDbType.Image:
226                                 maxLength = -1;
227                                 precision = 0;
228                                 scale = 0;
229                                 localeId = 0;
230                                 compareOptions = SqlCompareOptions.None;
231                                 dbType = DbType.Binary;
232                                 type = typeof (byte []);
233                                 break;
234                         case SqlDbType.NChar:
235                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
236                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
237                                 dbType = DbType.String;
238                                 type = typeof (string);
239                                 break;
240                         case SqlDbType.NText:
241                                 maxLength = -1;
242                                 precision = 0;
243                                 scale = 0;
244                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
245                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
246                                 dbType = DbType.String;
247                                 type = typeof (string);
248                                 break;
249                         case SqlDbType.NVarChar:
250                                 maxLength = -1;
251                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
252                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
253                                 dbType = DbType.String;
254                                 type = typeof (string);
255                                 break;
256                         case SqlDbType.Text:
257                                 maxLength = -1;
258                                 precision = 0;
259                                 scale = 0;
260                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
261                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
262                                 dbType = DbType.String;
263                                 type = typeof (char []);
264                                 break;
265                         case SqlDbType.VarBinary:
266                                 maxLength = -1;
267                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
268                                 dbType = DbType.Binary;
269                                 type = typeof (byte []);
270                                 break;
271                         case SqlDbType.VarChar:
272                                 maxLength = -1;
273                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
274                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
275                                 dbType = DbType.String;
276                                 type = typeof (char []);
277                                 break;
278                         default:
279                                 throw new ArgumentException ("SqlDbType not supported");
280                         }
281                         this.maxLength = maxLength;
282                         this.name = name;
283                         this.sqlDbType = sqlDbType;
284                 }
285
286                 [MonoTODO]
287                 public SqlMetaData (string name, SqlDbType sqlDbType, Type userDefinedType)
288                 {
289                         if (name == null)
290                                 throw new ArgumentNullException ("name can not be null");
291                         switch (sqlDbType) {
292                         case SqlDbType.Udt:
293                                 maxLength = -1;
294                                 precision = 0;
295                                 scale = 0;
296                                 localeId = 0;
297                                 compareOptions = SqlCompareOptions.None;
298                                 dbType = DbType.Guid;
299                                 type = typeof (Guid);
300                                 break;
301                         default:
302                                 throw new ArgumentException ("SqlDbType not supported");
303                         }
304                         this.name = name;
305                         // FIXME:
306                         //this.sqlDbType = userDefinedType;
307                         throw new NotImplementedException ();
308                 }
309
310                 public SqlMetaData (string name, SqlDbType sqlDbType, byte precision, byte scale)
311                 {
312                         if (name == null)
313                                 throw new ArgumentNullException ("name can not be null");
314                         switch (sqlDbType) {
315                         case SqlDbType.Decimal:
316                                 maxLength = 9;
317                                 this.precision = precision;
318                                 this.scale = scale;
319                                 localeId = 0;
320                                 compareOptions = SqlCompareOptions.None;
321                                 dbType = DbType.Decimal;
322                                 type = typeof (decimal);
323                                 break;
324                         default:
325                                 throw new ArgumentException ("SqlDbType not supported");
326                         }
327                         this.name = name;
328                         this.sqlDbType = sqlDbType;
329                 }
330
331                 public SqlMetaData (string name, SqlDbType sqlDbType, long maxLength, long locale, SqlCompareOptions compareOptions)
332                 {
333                         if (name == null)
334                                 throw new ArgumentNullException ("name can not be null");
335                         switch (sqlDbType) {
336                         case SqlDbType.Char:
337                                 dbType = DbType.AnsiStringFixedLength;
338                                 type = typeof (char []);
339                                 break;
340                         case SqlDbType.NChar:
341                                 dbType = DbType.StringFixedLength;
342                                 type = typeof (char []);
343                                 break;
344                         case SqlDbType.NText:
345                         case SqlDbType.NVarChar:
346                                 dbType = DbType.String;
347                                 type = typeof (string);
348                                 break;
349                         case SqlDbType.Text:
350                         case SqlDbType.VarChar:
351                                 dbType = DbType.AnsiString;
352                                 type = typeof (char []);
353                                 break;
354                         default:
355                                 throw new ArgumentException ("SqlDbType not supported");
356                         }
357                         this.compareOptions = compareOptions;
358                         this.localeId = locale;
359                         this.maxLength = maxLength;
360                         this.name = name;
361                         this.sqlDbType = sqlDbType;
362                 }
363
364                 public SqlMetaData (string name, SqlDbType sqlDbType, string database, string owningSchema, string objectName)
365                 {
366                         if ((name == null || objectName == null) && database != null && owningSchema != null)
367                                 throw new ArgumentNullException ("name can not be null");
368                         switch (sqlDbType) {
369                         case SqlDbType.Xml:
370                                 maxLength = -1;
371                                 precision = 0;
372                                 scale = 0;
373                                 localeId = 0;
374                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
375                                 dbType = DbType.String;
376                                 type = typeof (string);
377                                 break;
378                         default:
379                                 throw new ArgumentException ("SqlDbType not supported");
380                         }
381                         this.name = name;
382                         this.sqlDbType = sqlDbType;
383                         databaseName = database;
384                         this.owningSchema = owningSchema;
385                         this.objectName = objectName;
386                 }
387
388                 public SqlMetaData (string name, SqlDbType sqlDbType, long maxLength, byte precision,
389                                     byte scale, long localeId, SqlCompareOptions compareOptions,
390                                     Type userDefinedType)
391                 {
392                         if (name == null)
393                                 throw new ArgumentNullException ("name can not be null");
394                         this.compareOptions = compareOptions;
395                         this.localeId = localeId;
396                         this.maxLength = maxLength;
397                         this.precision = precision;
398                         this.scale = scale;
399                         switch (sqlDbType) {
400                         case SqlDbType.Bit:
401                                 maxLength = 1;
402                                 precision = 1;
403                                 scale = 0;
404                                 localeId = 0;
405                                 compareOptions = SqlCompareOptions.None;
406                                 dbType = DbType.Boolean;
407                                 type = typeof (bool);
408                                 break;
409                         case SqlDbType.BigInt:
410                                 maxLength = 8;
411                                 precision = 19;
412                                 scale = 0;
413                                 localeId = 0;
414                                 compareOptions = SqlCompareOptions.None;
415                                 dbType = DbType.Int64;
416                                 type = typeof (long);
417                                 break;
418                         case SqlDbType.DateTime:
419                                 maxLength = 8;
420                                 precision = 23;
421                                 scale = 3;
422                                 localeId = 0;
423                                 compareOptions = SqlCompareOptions.None;
424                                 dbType = DbType.DateTime;
425                                 type = typeof (DateTime);
426                                 break;
427                         case SqlDbType.Decimal:
428                                 maxLength = 9;
429                                 precision = 18;
430                                 scale = 0;
431                                 localeId = 0;
432                                 compareOptions = SqlCompareOptions.None;
433                                 dbType = DbType.Decimal;
434                                 type = typeof (decimal);
435                                 break;
436                         case SqlDbType.Float:
437                                 maxLength = 8;
438                                 precision = 53;
439                                 scale = 0;
440                                 localeId = 0;
441                                 compareOptions = SqlCompareOptions.None;
442                                 dbType = DbType.Decimal;
443                                 type = typeof (float);
444                                 break;
445                         case SqlDbType.Image:
446                                 maxLength = -1;
447                                 precision = 0;
448                                 scale = 0;
449                                 localeId = 0;
450                                 compareOptions = SqlCompareOptions.None;
451                                 dbType = DbType.Binary;
452                                 type = typeof (byte []);
453                                 break;
454                         case SqlDbType.Int:
455                                 maxLength = 4;
456                                 precision = 10;
457                                 scale = 0;
458                                 localeId = 0;
459                                 compareOptions = SqlCompareOptions.None;
460                                 dbType = DbType.Int32;
461                                 type = typeof (int);
462                                 break;
463                         case SqlDbType.Money:
464                                 maxLength = 8;
465                                 precision = 19;
466                                 scale = 4;
467                                 localeId = 0;
468                                 compareOptions = SqlCompareOptions.None;
469                                 dbType = DbType.Currency;
470                                 type = typeof (decimal);
471                                 break;
472                         case SqlDbType.NText:
473                                 maxLength = -1;
474                                 precision = 0;
475                                 scale = 0;
476                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
477                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
478                                 dbType = DbType.String;
479                                 type = typeof (string);
480                                 break;
481                           /*
482                         case SqlDbType.Numeric:
483                                 maxLength = ;
484                                 precision = ;
485                                 scale = ;
486                                 localeId = 0;
487                                 compareOptions = SqlCompareOptions.None;
488                                 break;
489                           */
490                         case SqlDbType.Real:
491                                 maxLength = 4;
492                                 precision = 24;
493                                 scale = 0;
494                                 localeId = 0;
495                                 compareOptions = SqlCompareOptions.None;
496                                 dbType = DbType.Single;
497                                 type = typeof (Single);
498                                 break;
499                         case SqlDbType.SmallDateTime:
500                                 maxLength = 4;
501                                 precision = 16;
502                                 scale = 0;
503                                 localeId = 0;
504                                 compareOptions = SqlCompareOptions.None;
505                                 dbType = DbType.DateTime;
506                                 type = typeof (DateTime);
507                                 break;
508                         case SqlDbType.SmallInt:
509                                 maxLength = 2;
510                                 precision = 5;
511                                 scale = 0;
512                                 localeId = 0;
513                                 compareOptions = SqlCompareOptions.None;
514                                 dbType = DbType.Int16;
515                                 type = typeof (short);
516                                 break;
517                         case SqlDbType.SmallMoney:
518                                 maxLength = 4;
519                                 precision = 10;
520                                 scale = 4;
521                                 localeId = 0;
522                                 compareOptions = SqlCompareOptions.None;
523                                 dbType = DbType.Currency;
524                                 type = typeof (decimal);
525                                 break;
526                         case SqlDbType.Text:
527                                 maxLength = -1;
528                                 precision = 0;
529                                 scale = 0;
530                                 localeId = Thread.CurrentThread.CurrentCulture.LCID;
531                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
532                                 dbType = DbType.AnsiString;
533                                 type = typeof (char []);
534                                 break;
535                         case SqlDbType.Timestamp:
536                                 maxLength = 8;
537                                 precision = 0;
538                                 scale = 0;
539                                 localeId = 0;
540                                 compareOptions = SqlCompareOptions.None;
541                                 dbType = DbType.Byte;
542                                 type = typeof (byte []);
543                                 break;
544                         case SqlDbType.TinyInt:
545                                 maxLength = 1;
546                                 precision = 3;
547                                 scale = 0;
548                                 localeId = 0;
549                                 compareOptions = SqlCompareOptions.None;
550                                 dbType = DbType.Int16;
551                                 type = typeof (short);
552                                 break;
553                         case SqlDbType.UniqueIdentifier:
554                                 maxLength = 16;
555                                 precision = 0;
556                                 scale = 0;
557                                 localeId = 0;
558                                 compareOptions = SqlCompareOptions.None;
559                                 dbType = DbType.Guid;
560                                 type = typeof (Guid);
561                                 break;
562                         case SqlDbType.Udt:
563                                 maxLength = -1;
564                                 precision = 0;
565                                 scale = 0;
566                                 localeId = 0;
567                                 compareOptions = SqlCompareOptions.None;
568                                 dbType = DbType.Object;
569                                 type = typeof (object);
570                                 break;
571                         case SqlDbType.Variant:
572                                 maxLength = 8016;
573                                 precision = 0;
574                                 scale = 0;
575                                 localeId = 0;
576                                 compareOptions = SqlCompareOptions.None;
577                                 dbType = DbType.Object;
578                                 type = typeof (object);
579                                 break;
580                         case SqlDbType.Xml:
581                                 maxLength = -1;
582                                 precision = 0;
583                                 scale = 0;
584                                 localeId = 0;
585                                 compareOptions = SqlCompareOptions.IgnoreCase | SqlCompareOptions.IgnoreKanaType | SqlCompareOptions.IgnoreWidth;
586                                 dbType = DbType.Xml;
587                                 type = typeof (string);
588                                 break;
589                         default:
590                           /*
591                                 if (typeof (DbType.Row) == typeof (userDefinedType)) {
592                                         // FIXME:
593                                         // maxLength = Number of columns;
594                                         precision = 0;
595                                         scale = 0;
596                                         localeId = 0;
597                                         compareOptions = SqlCompareOptions.None;
598                                 } else
599                           */
600                                         throw new ArgumentException ("SqlDbType not supported");
601                         }
602                         this.name = name;
603                         this.sqlDbType = sqlDbType;
604                 }
605
606                 #endregion // Constructors
607
608                 #region Properties
609
610                 public SqlCompareOptions CompareOptions {
611                         get { return compareOptions; }
612                 }
613
614                 public DbType DbType {
615                         get { return dbType; }
616                 }
617
618                 public long LocaleId {
619                         get { return localeId; }
620                 }
621
622                 public static long Max {
623                         get { return -1; }
624                 }
625
626                 public long MaxLength {
627                         get { return maxLength; }
628                 }
629
630                 public string Name {
631                         get { return name; }
632                 }
633
634                 public byte Precision { 
635                         get { return precision; }
636                 }
637
638                 public byte Scale { 
639                         get { return scale; }
640                 }
641
642                 public SqlDbType SqlDbType {
643                         get { return sqlDbType; }
644                 }
645
646                 public string XmlSchemaCollectionDatabase {
647                         get { return databaseName; }
648                 }
649
650                 public string XmlSchemaCollectionName {
651                         get { return objectName; }
652                 }
653
654                 public string XmlSchemaCollectionOwningSchema {
655                         get { return owningSchema; }
656                 }
657
658                 [MonoTODO]
659                 public string TypeName {
660                         get { throw new NotImplementedException (); }
661                 }
662
663                 #endregion // Properties
664
665                 #region Methods
666
667                 public bool Adjust (bool value)
668                 {
669                         if (type != typeof (bool))
670                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
671                         return value;
672                 }
673
674                 public byte Adjust (byte value)
675                 {
676                         if (type != typeof (byte))
677                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
678                         return value;
679                 }
680
681                 public byte[] Adjust (byte[] value)
682                 {
683                         if (type != typeof (byte []))
684                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
685                         return value;
686                 }
687
688                 public char Adjust (char value)
689                 {
690                         if (type != typeof (char))
691                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
692                         return value;
693                 }
694
695                 public char[] Adjust (char[] value)
696                 {
697                         if (type != typeof (char []))
698                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
699                         return value;
700                 }
701
702                 public DateTime Adjust (DateTime value)
703                 {
704                         if (type != typeof (DateTime))
705                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
706                         return value;
707                 }
708
709                 public decimal Adjust (decimal value)
710                 {
711                         if (type != typeof (decimal))
712                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
713                         return value;
714                 }
715
716                 public double Adjust (double value)
717                 {
718                         if (type != typeof (double))
719                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
720                         return value;
721                 }
722
723                 public Guid Adjust (Guid value)
724                 {
725                         if (type != typeof (Guid))
726                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
727                         return value;
728                 }
729
730                 public short Adjust (short value)
731                 {
732                         if (type != typeof (short))
733                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
734                         return value;
735                 }
736
737                 public int Adjust (int value)
738                 {
739                         if (type != typeof (int))
740                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
741                         return value;
742                 }
743
744                 public long Adjust (long value)
745                 {
746                         if (type != typeof (long))
747                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
748                         return value;
749                 }
750
751                 public object Adjust (object value)
752                 {
753                         if (type != typeof (object))
754                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
755                         return value;
756                 }
757
758                 public float Adjust (float value)
759                 {
760                         if (type != typeof (float))
761                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
762                         return value;
763                 }
764
765                 public SqlBinary Adjust (SqlBinary value)
766                 {
767                         if (type != typeof (byte []))
768                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
769                         return value;
770                 }
771
772                 public SqlBoolean Adjust (SqlBoolean value)
773                 {
774                         if (type != typeof (bool))
775                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
776                         return value;
777                 }
778
779                 public SqlByte Adjust (SqlByte value)
780                 {
781                         if (type != typeof (byte))
782                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
783                         return value;
784                 }
785
786                 public SqlBytes Adjust (SqlBytes value)
787                 {
788                         if (type != typeof (byte []))
789                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
790                         return value;
791                 }
792
793                 public SqlChars Adjust (SqlChars value)
794                 {
795                         if (type != typeof (char []))
796                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
797                         return value;
798                 }
799
800                 public SqlDateTime Adjust (SqlDateTime value)
801                 {
802                         if (type != typeof (DateTime))
803                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
804                         return value;
805                 }
806
807                 public SqlDecimal Adjust (SqlDecimal value)
808                 {
809                         if (type != typeof (decimal))
810                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
811                         return value;
812                 }
813
814                 public SqlDouble Adjust (SqlDouble value)
815                 {
816                         if (type != typeof (double))
817                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
818                         return value;
819                 }
820
821                 public SqlGuid Adjust (SqlGuid value)
822                 {
823                         if (type != typeof (Guid))
824                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
825                         return value;
826                 }
827
828                 public SqlInt16 Adjust (SqlInt16 value)
829                 {
830                         if (type != typeof (short))
831                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
832                         return value;
833                 }
834
835                 public SqlInt32 Adjust (SqlInt32 value)
836                 {
837                         if (type != typeof (int))
838                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
839                         return value;
840                 }
841
842                 public SqlInt64 Adjust (SqlInt64 value)
843                 {
844                         if (type != typeof (long))
845                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
846                         return value;
847                 }
848
849                 public SqlMoney Adjust (SqlMoney value)
850                 {
851                         if (type != typeof (decimal))
852                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
853                         return value;
854                 }
855
856                 public SqlSingle Adjust (SqlSingle value)
857                 {
858                         if (type != typeof (Single))
859                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
860                         return value;
861                 }
862
863                 public SqlString Adjust (SqlString value)
864                 {
865                         if (type != typeof (string))
866                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
867                         return value;
868                 }
869
870                 public string Adjust (string value)
871                 {
872                         if (type != typeof (string))
873                                 throw new ArgumentException ("Value does not match the SqlMetaData type");
874                         return value;
875                 }
876
877                 public static SqlMetaData InferFromValue (object value, string name)
878                 {
879                         if (name == null)
880                                 throw new ArgumentNullException ("name can not be null");
881                         if (value == null)
882                                 throw new ArgumentException ("value can not be null");
883                         SqlMetaData sqlMetaData = null;
884                         switch (value.GetType ().ToString ()) {
885                         case "System.Boolean":
886                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Bit);
887                                 break;
888                         case "System.Byte":
889                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Binary);
890                                 break;
891                         case "System.Byte[]":
892                                 sqlMetaData = new SqlMetaData (name, SqlDbType.VarBinary);
893                                 break;
894                         case "System.Char":
895                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Char);
896                                 break;
897                         case "System.Char[]":
898                                 sqlMetaData = new SqlMetaData (name, SqlDbType.VarChar);
899                                 break;
900                         case "System.DateTime":
901                                 sqlMetaData = new SqlMetaData (name, SqlDbType.DateTime);
902                                 break;
903                         case "System.Decimal":
904                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Decimal);
905                                 break;
906                         case "System.Double":
907                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Float);
908                                 break;
909                         case "System.Guid":
910                                 sqlMetaData = new SqlMetaData (name, SqlDbType.UniqueIdentifier);
911                                 break;
912                         case "System.Int16":
913                                 sqlMetaData = new SqlMetaData (name, SqlDbType.SmallInt);
914                                 break;
915                         case "System.Int32":
916                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Int);
917                                 break;
918                         case "System.Int64":
919                                 sqlMetaData = new SqlMetaData (name, SqlDbType.BigInt);
920                                 break;
921                         case "System.Single":
922                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Real);
923                                 break;
924                         case "System.String":
925                                 sqlMetaData = new SqlMetaData (name, SqlDbType.NVarChar);
926                                 break;
927                         case "System.Object":
928                         default:
929                                 sqlMetaData = new SqlMetaData (name, SqlDbType.Variant);
930                                 break;
931                         }
932                         return sqlMetaData;
933                 }
934
935                 #endregion // Methods
936         }
937 }
938
939 #endif