1internal static class ParameterTypeHelper
2{
3 public static DbType ToDbType(this Type type)
4 {
5 if (type == typeof(string)) return DbType.String;
6 if (type == typeof(UInt64)) return DbType.UInt64;
7 if (type == typeof(Int64)) return DbType.Int64;
8 if (type == typeof(Int32)) return DbType.Int32;
9 if (type == typeof(UInt32)) return DbType.UInt32;
10 if (type == typeof(DateTime)) return DbType.DateTime;
11 if (type == typeof(UInt16)) return DbType.UInt16;
12 if (type == typeof(Int16)) return DbType.Int16;
13 if (type == typeof(SByte)) return DbType.SByte;
14 if (type == typeof(Object)) return DbType.Object;
15 if (type == typeof(double)) return DbType.Double;
16 if (type == typeof(byte[])) return DbType.Binary;
17 if (type == typeof(decimal)) return DbType.Decimal;
18 if (type == typeof(Guid)) return DbType.Guid;
19 if (type == typeof(Boolean)) return DbType.Boolean;
20
21 throw new ArgumentException(string.Format("Unknown conversion for type '{0}'", type.FullName));
22 }
23
24 /// <summary>
25 /// Get the native type based on the database type
26 /// </summary>
27 /// <param name="dbType">The database type to convert</param>
28 /// <returns>The equivalent managed type, otherwise the DBNull type</returns>
29 public static Type ToManagedType(DbType dbType)
30 {
31 Type result = typeof (DBNull);
32
33 switch (dbType)
34 {
35 case DbType.String:
36 result = typeof (string);
37 break;
38
39 case DbType.UInt64:
40 result = typeof (UInt64);
41 break;
42
43 case DbType.Int64:
44 result = typeof (Int64);
45 break;
46
47 case DbType.Int32:
48 result = typeof (Int32);
49 break;
50
51 case DbType.UInt32:
52 result = typeof (UInt32);
53 break;
54
55 case DbType.Single:
56 result = typeof (float);
57 break;
58
59 case DbType.Date:
60 result = typeof (DateTime);
61 break;
62
63 case DbType.DateTime:
64 result = typeof (DateTime);
65 break;
66
67 case DbType.Time:
68 result = typeof (DateTime);
69 break;
70
71 case DbType.StringFixedLength:
72 result = typeof (string);
73 break;
74
75 case DbType.UInt16:
76 result = typeof (UInt16);
77 break;
78
79 case DbType.Int16:
80 result = typeof (Int16);
81 break;
82
83 case DbType.SByte:
84 result = typeof (byte);
85 break;
86
87 case DbType.Object:
88 result = typeof (object);
89 break;
90
91 case DbType.AnsiString:
92 result = typeof (string);
93 break;
94
95 case DbType.AnsiStringFixedLength:
96 result = typeof (string);
97 break;
98
99 case DbType.VarNumeric:
100 result = typeof (decimal);
101 break;
102
103 case DbType.Currency:
104 result = typeof (double);
105 break;
106
107 case DbType.Binary:
108 result = typeof (byte[]);
109 break;
110
111 case DbType.Decimal:
112 result = typeof (decimal);
113 break;
114
115 case DbType.Double:
116 result = typeof (Double);
117 break;
118
119 case DbType.Guid:
120 result = typeof (Guid);
121 break;
122
123 case DbType.Boolean:
124 result = typeof (bool);
125 break;
126 }
127
128 return result;
129 }
130}
131