(一)C#与SQL Server 2005(或以下版本):
C# |
C#取值 |
SQL Server |
SQL Server取值 |
System.DateTime |
samlltime |
||
System.Object |
variant |
(二)C#与SQL Server 2008(或以上版本):
C# |
C#取值 |
SQL Server |
SQL Server取值 |
System.Boolean |
true/false |
bit |
1/0 |
System.SByte |
-128~127 |
||
System.Byte |
0~255 |
tinyint |
0~255 |
System.Int16 |
-32768~32767 |
smallint |
-32768~32767 |
System.UInt16 |
0~65535 |
||
System.Int32 |
-2147483648~2147483647 |
int |
-2147483648~2147483647 |
System.UInt32 |
0~4294967295 |
||
System.Int64 |
-9223372036854775808~ 9223372036854775807L |
bigint |
-9223372036854775808~9223372036854775807 |
System.UInt64 |
0~18446744073709551615UL |
||
System.Single |
real |
||
System.Double |
float |
||
System.Decimal |
smallmoney |
-214748.3648~214748.3647 |
|
money |
-922337203685477.5808~922337203685477.5807 |
||
decimal |
-999999999999999999.49~999999999999999999.49 |
||
numeric |
1~38位 |
||
System.Char |
|||
System.String |
char |
1~8000 |
|
varchar |
1~8000 |
||
nchar |
1~4000 |
||
nvarchar |
1~4000 |
||
text |
|||
ntext |
|||
time |
00:00:00.0000000~23:59:59.9999999 |
||
System.DateTime |
date |
0001-01-01~9999-12-31 |
|
smalldatetime |
|||
datetime |
1753-01-01 00:00:00:000~9999-12-31 23:59:59.997 |
||
datetime2 |
|||
datetimeoffset |
|||
System.Byte[] |
timestamp |
||
image |
|||
binary |
|||
varbinary |
|||
System.Guid |
uniqueidentifier |
||
System.Object |
sql_variant |
(三)根据数据库数据类型字符,获取C#中对应的数据类型字符
1 /// <summary>
2 /// 将数据库数据类型字符串,转为C#数据类型字符串。
3 /// </summary>
4 /// <param name="dbType">数据库数据类型字符串。</param>
5 /// <returns>C#数据类型字符串。</returns>
6 private static string DBTypeToCSharpType(string dbType)
7 {
8 string cSharpType = string.Empty;
9 switch (dbType.ToLower())
10 {
11 case "bit":
12 cSharpType = "bool";
13 break;
14 case "tinyint":
15 cSharpType = "byte";
16 break;
17 case "smallint":
18 cSharpType = "short";
19 break;
20 case "int":
21 cSharpType = "int";
22 break;
23 case "bigint":
24 cSharpType = "long";
25 break;
26 case "real":
27 cSharpType = "float";
28 break;
29 case "float":
30 cSharpType = "double";
31 break;
32 case "smallmoney":
33 case "money":
34 case "decimal":
35 case "numeric":
36 cSharpType = "decimal";
37 break;
38 case "char":
39 case "varchar":
40 case "nchar":
41 case "nvarchar":
42 case "text":
43 case "ntext":
44 cSharpType = "string";
45 break;
46 case "samlltime":
47 case "date":
48 case "smalldatetime":
49 case "datetime":
50 case "datetime2":
51 case "datetimeoffset":
52 cSharpType = "System.DateTime";
53 break;
54 case "timestamp":
55 case "image":
56 case "binary":
57 case "varbinary":
58 cSharpType = "byte[]";
59 break;
60 case "uniqueidentifier":
61 cSharpType = "System.Guid";
62 break;
63 case "variant":
64 case "sql_variant":
65 cSharpType = "object";
66 break;
67 default:
68 cSharpType = "string";
69 break;
70 }
71 return cSharpType;
72 }