首页 > 代码库 > C#与 SQL Server 的数据类型对应关系

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

(一)     C#与SQL Server 2005(或以下版本):

 

C#

C#取值

SQL Server

SQL Server取值

System.DateTime

 

samlltime

 

System.Object

 

variant

 

 

(二)     C#与SQL Server 2008(或以上版本):

C#

C#取值

SQL Server

SQL Server取值

System.Boolean

true/false

bit

1/0

System.SByte

-128~127

 

 

System.Byte

0~255

tinyint

0~255

System.Int16

-32768~32767

smallint

-32768~32767

System.UInt16

0~65535

 

 

System.Int32

-2147483648~2147483647

int

-2147483648~2147483647

System.UInt32

0~4294967295

 

 

System.Int64

-9223372036854775808~ 9223372036854775807L

bigint

-9223372036854775808~9223372036854775807

System.UInt64

0~18446744073709551615UL

 

 

System.Single

 

real

 

System.Double

 

float

 

System.Decimal

 

smallmoney

-214748.3648~214748.3647

money

-922337203685477.5808~922337203685477.5807

decimal

-999999999999999999.49~999999999999999999.49

numeric

1~38位

System.Char

 

 

 

System.String

 

char

1~8000

varchar

1~8000

nchar

1~4000

nvarchar

1~4000

text

 

ntext

 

 

 

time

00:00:00.0000000~23:59:59.9999999

System.DateTime

 

date

0001-01-01~9999-12-31

smalldatetime

 

datetime

1753-01-01 00:00:00:000~9999-12-31 23:59:59.997

datetime2

 

datetimeoffset

 

timestamp

 

System.Byte[]

 

image

 

binary

 

varbinary

 

System.Guid

 

uniqueidentifier

 

System.Object

 

sql_variant

 

 

(三)     根据数据库数据类型字符,获取C#中对应的数据类型字符

/// <summary>
/// 将数据库数据类型字符串,转为C#数据类型字符串。
/// </summary>
/// <param name="dbType">数据库数据类型字符串。</param>
/// <returns>C#数据类型字符串。</returns>
private static string DBTypeToCSharpType(string dbType)
{
    string cSharpType = string.Empty;
    switch (dbType.ToLower())
    {
        case "bit":
            cSharpType = "bool";
            break;
        case "tinyint":
            cSharpType = "byte";
            break;
        case "smallint":
            cSharpType = "short";
            break;
        case "int":
            cSharpType = "int";
            break;
        case "bigint":
            cSharpType = "long";
            break;
        case "real":
            cSharpType = "float";
            break;
        case "float":
            cSharpType = "double";
            break;
        case "smallmoney":
        case "money":
        case "decimal":
        case "numeric":
            cSharpType = "decimal";
            break;
        case "char":
        case "varchar":
        case "nchar":
        case "nvarchar":
        case "text":
        case "ntext":
            cSharpType = "string";
            break;
        case "samlltime":
        case "date":
        case "smalldatetime":
        case "datetime":
        case "datetime2":
        case "datetimeoffset":
        case "timestamp":
            cSharpType = "System.DateTime";
            break;
        case "image":
        case "binary":
        case "varbinary":
            cSharpType = "byte[]";
            break;
        case "uniqueidentifier":
            cSharpType = "System.Guid";
            break;
        case "variant":
        case "sql_variant":
            cSharpType = "object";
            break;
        default:
            cSharpType = "string";
            break;
    }
    return cSharpType;
}

 

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