SQL Server 数据类型映射SQL Server Data Type Mappings

SQL Server 和 .NET Framework 基于不同的类型系统。SQL Server and the .NET Framework are based on different type systems. 例如,.NET Framework Decimal 结构的最大小数位数为 28,而 SQL Server 的 decimal 和 numeric 数据类型的最大小数位数为 38。For example, the .NET Framework Decimal structure has a maximum scale of 28, whereas the SQL Server decimal and numeric data types have a maximum scale of 38. 为了在读取和写入数据时维护数据的完整性,SqlDataReader 将公开用于返回 System.Data.SqlTypes 的对象的 SQL Server 特定的类型化访问器方法以及用于返回 .NET Framework 类型的访问器方法。To maintain data integrity when reading and writing data, the SqlDataReader exposes SQL Server–specific typed accessor methods that return objects of System.Data.SqlTypes as well as accessor methods that return .NET Framework types. SQL Server 类型和 .NET Framework 类型也可通过 DbTypeSqlDbType 类中的枚举表示,当您指定 SqlParameter 数据类型时可以使用这些枚举。Both SQL Server types and .NET Framework types are also represented by enumerations in the DbType and SqlDbType classes, which you can use when specifying SqlParameter data types.

下表显示了推断 .NET Framework 类型、 DbTypeSqlDbType枚举以及的访问器方法SqlDataReaderThe following table shows the inferred .NET Framework type, the DbType and SqlDbType enumerations, and the accessor methods for the SqlDataReader.

SQL Server 数据库引擎类型SQL Server Database Engine type .NET Framework 类型.NET Framework type SqlDbType 枚举SqlDbType enumeration SqlDataReader SqlTypes 类型化访问器SqlDataReader SqlTypes typed accessor DbType 枚举DbType enumeration SqlDataReader DbType 类型化访问器SqlDataReader DbType typed accessor
bigintbigint Int64Int64 BigInt GetSqlInt64 Int64 GetInt64
binarybinary Byte[]Byte[] VarBinary GetSqlBinary Binary GetBytes
bit BooleanBoolean Bit GetSqlBoolean Boolean GetBoolean
charchar StringString

Char[]Char[]
Char GetSqlString AnsiStringFixedLength,AnsiStringFixedLength,

String
GetString

GetChars
日期1date 1

(SQL Server 2008 及更高版本)(SQL Server 2008 and later)
DateTimeDateTime Date 1Date 1 GetSqlDateTime Date 1Date 1 GetDateTime
DATETIMEdatetime DateTimeDateTime DateTime GetSqlDateTime DateTime GetDateTime
datetime2datetime2

(SQL Server 2008 及更高版本)(SQL Server 2008 and later)
DateTimeDateTime DateTime2 None DateTime2 GetDateTime
datetimeoffsetdatetimeoffset

(SQL Server 2008 及更高版本)(SQL Server 2008 and later)
DateTimeOffsetDateTimeOffset DateTimeOffset none DateTimeOffset GetDateTimeOffset
decimaldecimal DecimalDecimal Decimal GetSqlDecimal Decimal GetDecimal
FILESTREAM attribute (varbinary(max))FILESTREAM attribute (varbinary(max)) Byte[]Byte[] VarBinary GetSqlBytes Binary GetBytes
浮点数float DoubleDouble Float GetSqlDouble Double GetDouble
imageimage Byte[]Byte[] Binary GetSqlBinary Binary GetBytes
intint Int32Int32 Int GetSqlInt32 Int32 GetInt32
moneymoney DecimalDecimal Money GetSqlMoney Decimal GetDecimal
ncharnchar StringString

Char[]Char[]
NChar GetSqlString StringFixedLength GetString

GetChars
ntextntext StringString

Char[]Char[]
NText GetSqlString String GetString

GetChars
数值numeric DecimalDecimal Decimal GetSqlDecimal Decimal GetDecimal
nvarcharnvarchar StringString

Char[]Char[]
NVarChar GetSqlString String GetString

GetChars
实数real SingleSingle Real GetSqlSingle Single GetFloat
rowversionrowversion Byte[]Byte[] Timestamp GetSqlBinary Binary GetBytes
smalldatetimesmalldatetime DateTimeDateTime DateTime GetSqlDateTime DateTime GetDateTime
smallintsmallint Int16Int16 SmallInt GetSqlInt16 Int16 GetInt16
smallmoneysmallmoney DecimalDecimal SmallMoney GetSqlMoney Decimal GetDecimal
sql_variantsql_variant 对象2Object 2 Variant GetSqlValue 2GetSqlValue 2 Object GetValue 2GetValue 2
文本text StringString

Char[]Char[]
Text GetSqlString String GetString

GetChars
timetime

(SQL Server 2008 及更高版本)(SQL Server 2008 and later)
TimeSpanTimeSpan Time none Time GetDateTime
时间戳timestamp Byte[]Byte[] Timestamp GetSqlBinary Binary GetBytes
tinyinttinyint ByteByte TinyInt GetSqlByte Byte GetByte
uniqueidentifieruniqueidentifier GuidGuid UniqueIdentifier GetSqlGuid Guid GetGuid
varbinaryvarbinary Byte[]Byte[] VarBinary GetSqlBinary Binary GetBytes
varcharvarchar StringString

Char[]Char[]
VarChar GetSqlString AnsiStringStringAnsiString, String GetString

GetChars
xmlxml XmlXml Xml GetSqlXml Xml none

1不能将的DbType属性SqlParameter设置为SqlDbType.Date1 You cannot set the DbType property of a SqlParameter to SqlDbType.Date.
2如果你知道的sql_variant基础类型,请使用特定的类型化访问器。2 Use a specific typed accessor if you know the underlying type of the sql_variant.

SQL Server 文档SQL Server documentation

有关 SQL Server 数据类型的详细信息,请参阅数据类型(transact-sql)For more information about SQL Server data types, see Data types (Transact-SQL).

请参阅See also