SQL Server类型 | C#类型 |
bit |
bool |
tinyint |
byte |
smallint |
short |
int |
int |
bigint |
long |
real |
float |
float |
double |
money |
decimal |
datetime |
DateTime |
char |
string |
varchar |
string |
nchar |
string |
nvarchar |
string |
text |
string |
ntext |
string |
image |
byte[] |
binary |
byte[] |
uniqueidentifier |
Guid |
相关代码如下:
1: // SqlDbType转换为C#数据类型
2: public static Type SqlType2CsharpType(SqlDbType sqlType)
3: {
4: switch (sqlType)
5: {
6: case SqlDbType.BigInt:
7: return typeof(Int64);
8: case SqlDbType.Binary:
9: return typeof(Object);
10: case SqlDbType.Bit:
11: return typeof(Boolean);
12: case SqlDbType.Char:
13: return typeof(String);
14: case SqlDbType.DateTime:
15: return typeof(DateTime);
16: case SqlDbType.Decimal:
17: return typeof(Decimal);
18: case SqlDbType.Float:
19: return typeof(Double);
20: case SqlDbType.Image:
21: return typeof(Object);
22: case SqlDbType.Int:
23: return typeof(Int32);
24: case SqlDbType.Money:
25: return typeof(Decimal);
26: case SqlDbType.NChar:
27: return typeof(String);
28: case SqlDbType.NText:
29: return typeof(String);
30: case SqlDbType.NVarChar:
31: return typeof(String);
32: case SqlDbType.Real:
33: return typeof(Single);
34: case SqlDbType.SmallDateTime:
35: return typeof(DateTime);
36: case SqlDbType.SmallInt:
37: return typeof(Int16);
38: case SqlDbType.SmallMoney:
39: return typeof(Decimal);
40: case SqlDbType.Text:
41: return typeof(String);
42: case SqlDbType.Timestamp:
43: return typeof(Object);
44: case SqlDbType.TinyInt:
45: return typeof(Byte);
46: case SqlDbType.Udt://自定义的数据类型
47: return typeof(Object);
48: case SqlDbType.UniqueIdentifier:
49: return typeof(Object);
50: case SqlDbType.VarBinary:
51: return typeof(Object);
52: case SqlDbType.VarChar:
53: return typeof(String);
54: case SqlDbType.Variant:
55: return typeof(Object);
56: case SqlDbType.Xml:
57: return typeof(Object);
58: default:
59: return null;
60: }
61: }
1: // sql server数据类型(如:varchar)转换为SqlDbType类型
2: public static SqlDbType SqlTypeString2SqlType(string sqlTypeString)
3: {
4: SqlDbType dbType = SqlDbType.Variant;//默认为Object
5:
6: switch (sqlTypeString)
7: {
8: case "int":
9: dbType = SqlDbType.Int;
10: break;
11: case "varchar":
12: dbType = SqlDbType.VarChar;
13: break;
14: case "bit":
15: dbType = SqlDbType.Bit;
16: break;
17: case "datetime":
18: dbType = SqlDbType.DateTime;
19: break;
20: case "decimal":
21: dbType = SqlDbType.Decimal;
22: break;
23: case "float":
24: dbType = SqlDbType.Float;
25: break;
26: case "image":
27: dbType = SqlDbType.Image;
28: break;
29: case "money":
30: dbType = SqlDbType.Money;
31: break;
32: case "ntext":
33: dbType = SqlDbType.NText;
34: break;
35: case "nvarchar":
36: dbType = SqlDbType.NVarChar;
37: break;
38: case "smalldatetime":
39: dbType = SqlDbType.SmallDateTime;
40: break;
41: case "smallint":
42: dbType = SqlDbType.SmallInt;
43: break;
44: case "text":
45: dbType = SqlDbType.Text;
46: break;
47: case "bigint":
48: dbType = SqlDbType.BigInt;
49: break;
50: case "binary":
51: dbType = SqlDbType.Binary;
52: break;
53: case "char":
54: dbType = SqlDbType.Char;
55: break;
56: case "nchar":
57: dbType = SqlDbType.NChar;
58: break;
59: case "numeric":
60: dbType = SqlDbType.Decimal;
61: break;
62: case "real":
63: dbType = SqlDbType.Real;
64: break;
65: case "smallmoney":
66: dbType = SqlDbType.SmallMoney;
67: break;
68: case "sql_variant":
69: dbType = SqlDbType.Variant;
70: break;
71: case "timestamp":
72: dbType = SqlDbType.Timestamp;
73: break;
74: case "tinyint":
75: dbType = SqlDbType.TinyInt;
76: break;
77: case "uniqueidentifier":
78: dbType = SqlDbType.UniqueIdentifier;
79: break;
80: case "varbinary":
81: dbType = SqlDbType.VarBinary;
82: break;
83: case "xml":
84: dbType = SqlDbType.Xml;
85: break;
86: }
87: return dbType;
88: }
1: // sql server中的数据类型,转换为C#中的类型类型
2: public static Type SqlTypeString2CsharpType(string sqlTypeString)
3: {
4: SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);
5:
6: return SqlType2CsharpType(dbTpe);
7: }
8:
9: // 将sql server中的数据类型,转化为C#中的类型的字符串
10: public static string SqlTypeString2CsharpTypeString(string sqlTypeString)
11: {
12: Type type = SqlTypeString2CsharpType(sqlTypeString);
13:
14: return type.Name;
15: }