首页 何问起 前端特效 htbtn-4 闪电 使用方法

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

对应关系表
SQL Server 2000 http://hovertree.com/menu/sqlserver/ C# CodeSmith
数据类型 取值范围 数据类型 取值范围 空值代替值 数据类型
bigint -2^63 (-9,223,372,036,854,775,807) 至 2^63-1 (9,223,372,036,854,775,807) Int64 -9,223,372,036,854,775,808;即十六进制的 0x8000000000000000至9,223,372,036,854,775,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,228,162,514,264,337,593,543,950,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,147,483,648) 到 2^31 - 1 (2,147,483,647) 的整型数据(所有数字) int -2,147,483,648 到 2,147,483,647 -2147483648 Int32
money 货币数据值介于 -2^63 (-922,337,203,685,477.5808) 与 2^63 - 1 (+922,337,203,685,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,228,162,514,264,337,593,543,950,335至79,228,162,514,264,337,593,543,950,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,147,483,647) 个字符。当服务器代码页使用双字节字符时,存储量仍是 2,147,483,647 字节。存储大小可能小于 2,147,483,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#类型的函数

/// <summary>
/// 数据库中与C#中的数据类型对照 by 何问起
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private string ChangeToCSharpType(string type)
{
    string reval = string.Empty; switch (type.ToLower())
    { case "int": reval = "Int32"; break; case "text": reval = "String"; break; case "bigint": reval = "Int64"; break; case "binary": reval = "System.Byte[]"; break; case "bit": reval = "Boolean"; break; case "char": reval = "String"; break; case "datetime": reval = "System.DateTime"; break; case "decimal": reval = "System.Decimal"; break; case "float": reval = "System.Double"; break; case "image": reval = "System.Byte[]"; break; case "money": reval = "System.Decimal"; break; case "nchar": reval = "String"; break; case "ntext": reval = "String"; break; case "numeric": reval = "System.Decimal"; break; case "nvarchar": reval = "String"; break; case "real": reval = "System.Single"; break; case "smalldatetime": reval = "System.DateTime"; break; case "smallint": reval = "Int16"; break; case "smallmoney": reval = "System.Decimal"; break; case "timestamp": reval = "System.DateTime"; break; case "tinyint": reval = "System.Byte"; break; case "uniqueidentifier": reval = "System.Guid"; break; case "varbinary": reval = "System.Byte[]"; break; case "varchar": reval = "String"; break; case "Variant": reval = "Object"; break; default: reval = "String"; break; }
    return reval;
}/* 何问起 hovertree.com */

推荐:http://www.cnblogs.com/roucheng/p/netkuangjia.html

posted @ 2016-06-11 16:13  roucheng  阅读(539)  评论(0编辑  收藏  举报