您的位置:首页 > 数据库

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

2013-12-19 16:56 344 查看
对应关系表

SQL Server 2000C#CodeSmith
数据类型取值范围数据类型取值范围空值代替值数据类型
bigint-2^63 (-9,223,372,036,854,775,807) 至 2^63-1 (9,223,372,036,854,775,807)Int64

long

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


复制

保存

[csharp] view plaincopy

<strong class="c">/// </strong><summary><strong class="c">

</strong><strong class="c">/// 数据库中与C#中的数据类型对照

</strong><strong class="c">/// </strong></summary><strong class="c">

</strong><strong class="c">/// <param name="type">

</strong><strong class="c">/// <returns></returns>

</strong><strong class="k">private</strong> <strong class="k">string</strong> ChangeToCSharpType(<strong class="k">string</strong> type)

{

<strong class="k">string</strong> reval = <strong class="k">string</strong>.Empty;

<strong class="k">switch</strong> (type.ToLower())

{

<strong class="k">case</strong> <strong class="s">"int"</strong>:

reval = <strong class="s">"Int32"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"text"</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"bigint"</strong>:

reval = <strong class="s">"Int64"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"binary"</strong>:

reval = <strong class="s">"System.Byte[]"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"bit"</strong>:

reval = <strong class="s">"Boolean"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"char"</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"datetime"</strong>:

reval = <strong class="s">"System.DateTime"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"decimal"</strong>:

reval = <strong class="s">"System.Decimal"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"float"</strong>:

reval = <strong class="s">"System.Double"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"image"</strong>:

reval = <strong class="s">"System.Byte[]"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"money"</strong>:

reval = <strong class="s">"System.Decimal"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"nchar"</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"ntext"</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"numeric"</strong>:

reval = <strong class="s">"System.Decimal"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"nvarchar"</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"real"</strong>:

reval = <strong class="s">"System.Single"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"smalldatetime"</strong>:

reval = <strong class="s">"System.DateTime"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"smallint"</strong>:

reval = <strong class="s">"Int16"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"smallmoney"</strong>:

reval = <strong class="s">"System.Decimal"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"timestamp"</strong>:

reval = <strong class="s">"System.DateTime"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"tinyint"</strong>:

reval = <strong class="s">"System.Byte"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"uniqueidentifier"</strong>:

reval = <strong class="s">"System.Guid"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"varbinary"</strong>:

reval = <strong class="s">"System.Byte[]"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"varchar"</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

<strong class="k">case</strong> <strong class="s">"Variant"</strong>:

reval = <strong class="s">"Object"</strong>;

<strong class="k">break</strong>;

<strong class="k">default</strong>:

reval = <strong class="s">"String"</strong>;

<strong class="k">break</strong>;

}

<strong class="k">return</strong> reval;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: