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