博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中的类型和SQL Server中的类型对应关系
阅读量:6758 次
发布时间:2019-06-26

本文共 6947 字,大约阅读时间需要 23 分钟。

 

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:          }

转载于:https://www.cnblogs.com/chaosimple/archive/2012/09/06/2672813.html

你可能感兴趣的文章
linux清理内存
查看>>
得到一个范围的随机数函数
查看>>
经典算法-链表(golang)
查看>>
leetcode — search-a-2d-matrix
查看>>
魔板 bfs() 预处理,记录每种状态。然后状态置换,(重点要用到全排列的hash记录状态)...
查看>>
构建之法课后作业第一次作业(15个题选一个)
查看>>
操作redis方法
查看>>
C语言函数
查看>>
Python3-异常处理
查看>>
Python-简单打印进度条
查看>>
【02】天气查询应用(第二课)
查看>>
监听微信返回按钮
查看>>
第二次实验报告
查看>>
HDU ACM 3790 最短路径问题
查看>>
python生成器
查看>>
linux 安装 ftp
查看>>
python 监控FTP目录下的文件个数
查看>>
MapInfo格式转arggis格式
查看>>
Network - SSL/TLS的基本概念
查看>>
python学习之老男孩python全栈第九期_day012知识点总结
查看>>