SQLServer数据类型映射——.NET Framework类型、DbType和SqlDbType枚举以及SqlDataReader的访问器方法

224 阅读2分钟

SQL Server 和 .NET Framework 基于不同的类型系统。 例如,.NET Framework Decimal 结构的最大小数位数为 28,而 SQL Server 的 decimal 和 numeric 数据类型的最大小数位数为 38。 为在读取和写入数据时维护数据完整性,SqlDataReader 将公开用于返回 System.Data.SqlTypes 的对象的 SQL Server 特定的类型化访问器方法以及用于返回 .NET Framework 类型的访问器方法。 SQL Server 类型和 .NET Framework 类型也可通过 DbType 和 SqlDbType 类中的枚举表示,当您指定 SqlParameter 数据类型时可以使用这些枚举。

下表显示了推断的 .NET Framework 类型、DbType 和 SqlDbType 枚举以及 SqlDataReader 的访问器方法。

SQL Server 数据库引擎类型.NET Framework 类型SqlDbType 枚举SqlDataReader SqlTypes 类型化访问器DbType 枚举SqlDataReader DbType 类型化访问器
bigintInt64BigIntGetSqlInt64Int64GetInt64
binaryByte[]VarBinaryGetSqlBinaryBinaryGetBytes
bit布尔BitGetSqlBooleanBooleanGetBoolean
charString Char[]CharGetSqlStringAnsiStringFixedLength, StringGetString GetChars
date \\1 (SQL Server 2008 及更高版本)DateTimeDateDateGetSqlDateTimeDateDateGetDateTime
datetimeDateTimeDateTimeGetSqlDateTimeDateTimeGetDateTime
datetime2 (SQL Server 2008 及更高版本)DateTimeDateTime2DateTime2GetDateTime
datetimeoffset (SQL Server 2008 及更高版本)DateTimeOffsetDateTimeOffsetDateTimeOffsetGetDateTimeOffset
Decimal小数DecimalGetSqlDecimalDecimalGetDecimal
FILESTREAM attribute (varbinary(max))Byte[]VarBinaryGetSqlBytesBinaryGetBytes
FLOATDoubleFloatGetSqlDoubleDoubleGetDouble
imageByte[]BinaryGetSqlBinaryBinaryGetBytes
intInt32IntGetSqlInt32Int32GetInt32
money小数MoneyGetSqlMoneyDecimalGetDecimal
ncharString Char[]NCharGetSqlStringStringFixedLengthGetString GetChars
ntextString Char[]NTextGetSqlStringStringGetString GetChars
numeric小数DecimalGetSqlDecimalDecimalGetDecimal
nvarcharString Char[]NVarCharGetSqlStringStringGetString GetChars
realSingleRealGetSqlSingleSingleGetFloat
rowversionByte[]TimestampGetSqlBinaryBinaryGetBytes
smalldatetimeDateTimeDateTimeGetSqlDateTimeDateTimeGetDateTime
smallintInt16SmallIntGetSqlInt16Int16GetInt16
smallmoney小数SmallMoneyGetSqlMoneyDecimalGetDecimal
sql_variantObject \\2VariantGetSqlValueGetSqlValueObjectGetValueGetValue
textString Char[]TextGetSqlStringStringGetString GetChars
time (SQL Server 2008 及更高版本)TimeSpanTimeTimeGetDateTime
timestampByte[]TimestampGetSqlBinaryBinaryGetBytes
tinyintByteTinyIntGetSqlByteByteGetByte
uniqueidentifierGuidUniqueIdentifierGetSqlGuidGuidGetGuid
varbinaryByte[]VarBinaryGetSqlBinaryBinaryGetBytes
varcharString Char[]VarCharGetSqlStringAnsiStringStringGetString GetChars
xmlXmlXmlGetSqlXmlXml

\\1 无法将 SqlParameter 的 属性设置为 SqlDbType.Date
\\2 如果已知 的基础类型,则使用特定的类型化访问器。

SQL Server 文档

有关 SQL Server 数据类型的详细信息,请参阅数据类型 (Transact-SQL)

另请参阅

SQL Server 数据类型映射 - ADO.NET | Microsoft Docs