微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SQLSERVER与C#中数据类型的对应关系

对应关系表
sql Server 2000 C# CodeSmith
数据类型 取值范围 数据类型 取值范围 空值代替值 数据类型
bigint -2^63 (-9,223,372,036,854,775,807) 至 2^63-1 (9,807) Int64 -9,808;即十六进制的 0x8000000000000000至9,807;即十六进制的 0x7FFFFFFFFFFFFFFF Int64.MinValue Int64
binary 固定长度的 n 个字节二进制数据。N 必须从 1 到 8,000。存储空间大小为 n+4 字节。 Byte[]   null Binary
bit True,False enum 0,1,-1(使用一个枚举变量来代替) public enum bitNull { False,True,Null = -1 }; -1 Boolean
char 长度为 n 个字节的固定长度且非 Unicode 的字符数据。n 必须是一个介于 1 和 8,000 之间的数值。存储大小为 n 个字节。 string   null AnsiStringFixedLength
datetime 存储从 1753 年 1 月 1 日至 9999 年 12 月 31 日的日期(每个数值要求 8 个字节的存储空间) DateTime 0001 年 1 月 1 日 00:00:00 .0000000至9999 年 12 月 31 日 23:59:59.9999999 1753-01-01 0:00:00 DateTime
decimal 从 - 10^38 +1 到 10^38 - 1 Decimal -79,228,162,514,264,337,593,543,950,335至79,335 Decimal.MinValue Decimal
float 从 - 1.79E + 308 到 1.79E + 308 之间的浮点数字数据 Double -1.79769313486232e308至+1.79769313486232e308 Double.MinValue Double
image 可变长度二进制数据介于 0 与 231-1 (2,147,483,647) 字节之间。 Byte[]   null Binary
int 从 -2^31 (-2,648) 到 2^31 - 1 (2,647) 的整型数据(所有数字) int -2,648 到 2,647 -2147483648 Int32
money 货币数据值介于 -2^63 (-922,203,685,477.5808) 与 2^63 - 1 (+922,477.5807) 之间,精确到货币单位的千分之十。存储大小为 8 个字节。 Single -3.402823e38至+3.402823e38 Single.MinValue Currency
nchar 至多为 4000 个 Unicode 字符 string   null StringFixedLength
ntext 可变长度 Unicode 数据的最大长度为 230- 1 (1,073,741,823) 个字符。存储大小是所输入字符个数的两倍(以字节为单位)。 string   null String
numeric 使用最大精度时,有效值从 - 10^38 +1 到 10^38 - 1 Decimal -79,335 Decimal.MinValue Decimal
nvarchar 包含 n 个字符的可变长度 Unicode 字符数据。n 的值必须介于 1 与 4,000 之间。 string   null String
real 从 ?3.40E + 38 到 3.40E + 38 之间的浮点数字数据。存储大小为 4 字节。 Single -3.402823e38至+3.402823e38 Single.MinValue Single
smalldatetime 从 1900 年 1 月 1 日至 2079 年 6 月 6 日的日期(每个数值要求 4 个字节的存储空间)。 DateTime 0001 年 1 月 1 日 00:00:00 .0000000至9999 年 12 月 31 日 23:59:59.9999999 1900-01-01 0:00:00 DateTime
smallint 从 -2^15 (-32,768) 到 2^15 - 1 (32,767) 的整型数据。存储大小为 2 个字节。 Int16 -32768至32767 Int16.MinValue Int16
smallmoney 货币数据值介于 -214,748.3648 与 +214.748,3647 之间,精确到货币单位的千分之十。存储大小为 4 个字节。 Single -3.402823e38至+3.402823e38 Single.MinValue Currency
sql_variant sql Server 2000中不支持大数据类型text,ntext,image,timestamp,其他类型均支持 Object   null Object
text 服务器代码页中的可变长度非 Unicode 数据的最大长度为 231-1 (2,647) 个字符。当服务器代码页使用双字节字符时,存储量仍是 2,647 字节。存储大小可能小于 2,647 字节(取决于字符串)。 string   null AnsiString
timestamp timestamp 这种数据类型表现自动生成的二进制数,确保这些数在数据库中是唯一的。timestamp 一般用作给表行加版本戳的机制。存储大小为 8 字节。 Byte[]   null Binary
tinyint 从 0 到 255 的整型数据。存储大小为 1 字节。 Byte 0至255 Byte.MinValue Byte
uniqueidentifier 存储 16 字节的二进制值,该值的使用与全局唯一标识符 (GUID) 一样。GUID 是一个唯一的二进制数字;世界上的任何两台计算机都不会生成重复的 GUID 值。GUID 主要用于在拥有多个节点、多台计算机的网络中,分配必须具有唯一性的标识符。 Guid   Guid.Empty Guid
varbinary n 个字节变长二进制数据。n 必须从 1 到 8,000。存储空间大小为实际输入数据长度 +4 个字节,而不是 n 个字节。输入的数据长度可能为 0 字节。 Byte[]   null Binary
varchar 长度为 n 个字节的可变长度且非 Unicode 的字符数据。n 必须是一个介于 1 和 8,000 之间的数值。存储大小为输入数据的字节的实际长度,而不是 n 个字节。所输入的数据字符长度可以为零。 string  
sql类型转换为C#类型的函数

复制

保存
[csharp] view plain copy
  1. <strong class="c">/// </strong><summary><strong class="c">  
  2. </strong><strong class="c">/// 数据库中与C#中的数据类型对照  
  3. </strong><strong class="c">/// </strong></summary><strong class="c">  
  4. </strong><strong class="c">/// <param name="type">  
  5. </strong><strong class="c">/// <returns></returns>  
  6. </strong><strong class="k">private</strong> <strong class="k">string</strong> ChangetoCSharpType(<strong class="k">string</strong> type)  
  7. {  
  8.     <strong class="k">string</strong> reval = <strong class="k">string</strong>.Empty;  
  9.     <strong class="k">switch</strong> (type.ToLower())  
  10.     {  
  11.     <strong class="k">case</strong> <strong class="s">"int"</strong>:  
  12.     reval = <strong class="s">"Int32"</strong>;  
  13.     <strong class="k">break</strong>;  
  14.     <strong class="k">case</strong> <strong class="s">"text"</strong>:  
  15.     reval = <strong class="s">"String"</strong>;  
  16.     <strong class="k">break</strong>;  
  17.     <strong class="k">case</strong> <strong class="s">"bigint"</strong>:  
  18.     reval = <strong class="s">"Int64"</strong>;  
  19.     <strong class="k">break</strong>;  
  20.     <strong class="k">case</strong> <strong class="s">"binary"</strong>:  
  21.     reval = <strong class="s">"System.Byte[]"</strong>;  
  22.     <strong class="k">break</strong>;  
  23.     <strong class="k">case</strong> <strong class="s">"bit"</strong>:  
  24.     reval = <strong class="s">"Boolean"</strong>;  
  25.     <strong class="k">break</strong>;  
  26.     <strong class="k">case</strong> <strong class="s">"char"</strong>:  
  27.     reval = <strong class="s">"String"</strong>;  
  28.     <strong class="k">break</strong>;  
  29.     <strong class="k">case</strong> <strong class="s">"datetime"</strong>:  
  30.     reval = <strong class="s">"System.DateTime"</strong>;  
  31.     <strong class="k">break</strong>;  
  32.     <strong class="k">case</strong> <strong class="s">"decimal"</strong>:  
  33.     reval = <strong class="s">"System.Decimal"</strong>;  
  34.     <strong class="k">break</strong>;  
  35.     <strong class="k">case</strong> <strong class="s">"float"</strong>:  
  36.     reval = <strong class="s">"System.Double"</strong>;  
  37.     <strong class="k">break</strong>;  
  38.     <strong class="k">case</strong> <strong class="s">"image"</strong>:  
  39.     reval = <strong class="s">"System.Byte[]"</strong>;  
  40.     <strong class="k">break</strong>;  
  41.     <strong class="k">case</strong> <strong class="s">"money"</strong>:  
  42.     reval = <strong class="s">"System.Decimal"</strong>;  
  43.     <strong class="k">break</strong>;  
  44.     <strong class="k">case</strong> <strong class="s">"nchar"</strong>:  
  45.     reval = <strong class="s">"String"</strong>;  
  46.     <strong class="k">break</strong>;  
  47.     <strong class="k">case</strong> <strong class="s">"ntext"</strong>:  
  48.     reval = <strong class="s">"String"</strong>;  
  49.     <strong class="k">break</strong>;  
  50.     <strong class="k">case</strong> <strong class="s">"numeric"</strong>:  
  51.     reval = <strong class="s">"System.Decimal"</strong>;  
  52.     <strong class="k">break</strong>;  
  53.     <strong class="k">case</strong> <strong class="s">"nvarchar"</strong>:  
  54.     reval = <strong class="s">"String"</strong>;  
  55.     <strong class="k">break</strong>;  
  56.     <strong class="k">case</strong> <strong class="s">"real"</strong>:  
  57.     reval = <strong class="s">"System.Single"</strong>;  
  58.     <strong class="k">break</strong>;  
  59.     <strong class="k">case</strong> <strong class="s">"smalldatetime"</strong>:  
  60.     reval = <strong class="s">"System.DateTime"</strong>;  
  61.     <strong class="k">break</strong>;  
  62.     <strong class="k">case</strong> <strong class="s">"smallint"</strong>:  
  63.     reval = <strong class="s">"Int16"</strong>;  
  64.     <strong class="k">break</strong>;  
  65.     <strong class="k">case</strong> <strong class="s">"smallmoney"</strong>:  
  66.     reval = <strong class="s">"System.Decimal"</strong>;  
  67.     <strong class="k">break</strong>;  
  68.     <strong class="k">case</strong> <strong class="s">"timestamp"</strong>:  
  69.     reval = <strong class="s">"System.DateTime"</strong>;  
  70.     <strong class="k">break</strong>;  
  71.     <strong class="k">case</strong> <strong class="s">"tinyint"</strong>:  
  72.     reval = <strong class="s">"System.Byte"</strong>;  
  73.     <strong class="k">break</strong>;  
  74.     <strong class="k">case</strong> <strong class="s">"uniqueidentifier"</strong>:  
  75.     reval = <strong class="s">"System.Guid"</strong>;  
  76.     <strong class="k">break</strong>;  
  77.     <strong class="k">case</strong> <strong class="s">"varbinary"</strong>:  
  78.     reval = <strong class="s">"System.Byte[]"</strong>;  
  79.     <strong class="k">break</strong>;  
  80.     <strong class="k">case</strong> <strong class="s">"varchar"</strong>:  
  81.     reval = <strong class="s">"String"</strong>;  
  82.     <strong class="k">break</strong>;  
  83.     <strong class="k">case</strong> <strong class="s">"Variant"</strong>:  
  84.     reval = <strong class="s">"Object"</strong>;  
  85.     <strong class="k">break</strong>;  
  86.     <strong class="k">default</strong>:  
  87.     reval = <strong class="s">"String"</strong>;  
  88.     <strong class="k">break</strong>;  
  89.     }  
  90.     <strong class="k">return</strong> reval; 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐