Share via


Mapeamentos de tipo de dados do SQL Server

Aplicável a: .NET Framework .NET .NET Standard

Baixar ADO.NET

O SQL Server e o .NET Framework baseiam-se em diferentes sistemas de tipo. Por exemplo, a estrutura Decimal do .NET Framework tem uma escala máxima de 28, enquanto os tipos de dados decimais e numéricos do SQL Server têm uma escala máximo de 38. Para manter a integridade dos dados ao ler e gravar dados, o SqlDataReader expõe métodos acessadores tipados específicos do SQL Server que retornam objetos de System.Data.SqlTypes, bem como métodos acessadores que retornam tipos do .NET Framework. Os tipos do SQL Server e do .NET Framework também são representados por enumerações nas classes DbType e SqlDbType, que você pode usar ao especificar tipos de dados SqlParameter.

A tabela a seguir mostra o tipo de .NET Framework inferido, as enumerações de DbType e SqlDbType e os métodos acessadores de SqlDataReader.

Tipo de mecanismo do Banco de Dados do SQL Server Tipo de .NET Framework Enumeração SqlDbType Acessador tipado SqlTypes de SqlDataReader Enumeração DbType Acessador tipado DbType de SqlDataReader
BIGINT Int64 BigInt GetSqlInt64 Int64 GetInt64
binary Byte[] VarBinary GetSqlBinary Binary GetBytes
bit Boolean Bit GetSqlBoolean Boolean GetBoolean
char String

Char[]
Char GetSqlString AnsiStringFixedLength,

String
GetString

GetChars
data 1

(SQL Server 2008 e posteriores)
Datetime Date1 GetSqlDateTime Date1 GetDateTime
DATETIME Datetime DateTime GetSqlDateTime DateTime GetDateTime
datetime2

(SQL Server 2008 e posteriores)
Datetime DateTime2 Nenhum DateTime2 GetDateTime
datetimeoffset

(SQL Server 2008 e posteriores)
DateTimeOffset DateTimeOffset nenhum DateTimeOffset GetDateTimeOffset
decimal Decimal Decimal GetSqlDecimal Decimal GetDecimal
FILESTREAM attribute (varbinary(max)) Byte[] VarBinary GetSqlBytes Binary GetBytes
FLOAT Double Float GetSqlDouble Double GetDouble
image Byte[] Binary GetSqlBinary Binary GetBytes
INT Int32 Int GetSqlInt32 Int32 GetInt32
money Decimal Money GetSqlMoney Decimal GetDecimal
NCHAR String

Char[]
NChar GetSqlString StringFixedLength GetString

GetChars
ntext String

Char[]
NText GetSqlString String GetString

GetChars
numeric Decimal Decimal GetSqlDecimal Decimal GetDecimal
NVARCHAR String

Char[]
NVarChar GetSqlString String GetString

GetChars
real Single Real GetSqlSingle Single GetFloat
rowversion Byte[] Timestamp GetSqlBinary Binary GetBytes
smalldatetime Datetime DateTime GetSqlDateTime DateTime GetDateTime
SMALLINT Int16 SmallInt GetSqlInt16 Int16 GetInt16
SMALLMONEY Decimal SmallMoney GetSqlMoney Decimal GetDecimal
sql_variant Objeto 2 Variant GetSqlValue2 Object GetValue2
text String

Char[]
Text GetSqlString String GetString

GetChars
time

(SQL Server 2008 e posteriores)
TimeSpan Time nenhum Time GetDateTime
timestamp Byte[] Timestamp GetSqlBinary Binary GetBytes
TINYINT Byte TinyInt GetSqlByte Byte GetByte
UNIQUEIDENTIFIER Guid UniqueIdentifier GetSqlGuid Guid GetGuid
varbinary Byte[] VarBinary GetSqlBinary Binary GetBytes
varchar String

Char[]
VarChar GetSqlString AnsiString, String GetString

GetChars
xml Xml Xml GetSqlXml Xml nenhum

1 Não é possível definir a propriedade DbType de um SqlParameter como SqlDbType.Date.
2 Use um acessador tipado específico se você souber o tipo subjacente de sql_variant.

documentação do SQL Server

Para obter mais informações sobre tipos de dados do SQL Server, confira Tipos de dados (Transact-SQL).

Confira também