SQL Server类型与C#类型对应关系

 

SQL Server类型C#类型
bitbool
tinyintbyte
smallintshort
intint
bigintlong
realfloat
floatdouble
moneydecimal
datetimeDateTime
charstring
varcharstring
ncharstring
nvarcharstring
textstring
ntextstring
imagebyte[]
binarybyte[]
uniqueidentifierGuid

SQL Server 2000

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

 

null

AnsiString

 

 

// 将sql server中的数据类型,转化为C#中的类型的字符串 
        public static string SqlTypeString2CsharpTypeString(string sqlTypeString)
        {
            Type type = SqlTypeString2CsharpType(sqlTypeString);

            return type.Name;
        }
        // sql server中的数据类型,转换为C#中的类型类型 
        public static Type SqlTypeString2CsharpType(string sqlTypeString)
        {
            SqlDbType dbTpe = SqlTypeString2SqlType(sqlTypeString);
            return SqlType2CsharpType(dbTpe);
        }
        // sql server数据类型(如:varchar) // 转换为SqlDbType类型
        public static SqlDbType SqlTypeString2SqlType(string sqlTypeString)
        {
            SqlDbType dbType = SqlDbType.Variant;//默认为Object
            switch (sqlTypeString)
            {
                case "int": dbType = SqlDbType.Int; break;
                case "varchar": dbType = SqlDbType.VarChar; break;
                case "bit": dbType = SqlDbType.Bit; break;
                case "datetime": dbType = SqlDbType.DateTime; break;
                case "decimal": dbType = SqlDbType.Decimal; break;
                case "float": dbType = SqlDbType.Float; break;
                case "image": dbType = SqlDbType.Image; break;
                case "money": dbType = SqlDbType.Money; break;
                case "ntext": dbType = SqlDbType.NText; break;
                case "nvarchar": dbType = SqlDbType.NVarChar; break;
                case "smalldatetime": dbType = SqlDbType.SmallDateTime; break;
                case "smallint": dbType = SqlDbType.SmallInt; break;
                case "text": dbType = SqlDbType.Text; break;
                case "bigint": dbType = SqlDbType.BigInt; break;
                case "binary": dbType = SqlDbType.Binary; break;
                case "char": dbType = SqlDbType.Char; break;
                case "nchar": dbType = SqlDbType.NChar; break;
                case "numeric": dbType = SqlDbType.Decimal; break;
                case "real": dbType = SqlDbType.Real; break;
                case "smallmoney": dbType = SqlDbType.SmallMoney; break;
                case "sql_variant": dbType = SqlDbType.Variant; break;
                case "timestamp": dbType = SqlDbType.Timestamp; break;
                case "tinyint": dbType = SqlDbType.TinyInt; break;
                case "uniqueidentifier": dbType = SqlDbType.UniqueIdentifier; break;
                case "varbinary": dbType = SqlDbType.VarBinary; break;
                case "xml": dbType = SqlDbType.Xml; break; }
            return dbType;
        }

        //SqlDbType转换为C#数据类型
        public static Type SqlType2CsharpType(SqlDbType sqlType)
        {
            switch (sqlType)
            {
                case SqlDbType.BigInt: return typeof(Int64);
                case SqlDbType.Binary: return typeof(Object);
                case SqlDbType.Bit: return typeof(Boolean);
                case SqlDbType.Char: return typeof(String);
                case SqlDbType.DateTime: return typeof(DateTime);
                case SqlDbType.Decimal: return typeof(Decimal);
                case SqlDbType.Float: return typeof(Double);
                case SqlDbType.Image: return typeof(Object);
                case SqlDbType.Int: return typeof(Int32);
                case SqlDbType.Money: return typeof(Decimal);
                case SqlDbType.NChar: return typeof(String);
                case SqlDbType.NText: return typeof(String);
                case SqlDbType.NVarChar: return typeof(String);
                case SqlDbType.Real: return typeof(Single);
                case SqlDbType.SmallDateTime: return typeof(DateTime);
                case SqlDbType.SmallInt: return typeof(Int16);
                case SqlDbType.SmallMoney: return typeof(Decimal);
                case SqlDbType.Text: return typeof(String);
                case SqlDbType.Timestamp: return typeof(Object);
                case SqlDbType.TinyInt: return typeof(Byte);
                case SqlDbType.Udt:
                //自定义的数据类型          return typeof(Object);        
                case SqlDbType.UniqueIdentifier: return typeof(Object);
                case SqlDbType.VarBinary: return typeof(Object);
                case SqlDbType.VarChar: return typeof(String);
                case SqlDbType.Variant: return typeof(Object);
                case SqlDbType.Xml: return typeof(Object);
                default: return null;
            }
        }

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值