Mapeando dados de parâmetro CLR

A tabela a seguir lista os tipos de dados da Microsoft SQL Server, seus equivalentes no Common Language Runtime (CLR) para SQL Server no namespace System.Data.SqlTypes e seus equivalentes de CLR nativos no Microsoft .NET Framework.

Tipo de dados do SQL Server

Tipo (em System.Data.SqlTypes ou Microsoft.SqlServer.Types)

Tipo de dados CLR (.NET Framework)

bigint

SqlInt64

Int64, Nullable<Int64>

binary

SqlBytes, SqlBinary

Byte[]

bit

SqlBoolean

Boolean, Nullable<Boolean>

char

Nenhum

Nenhum

cursor

Nenhum

Nenhum

date

SqlDateTime

DateTime, Nullable<DateTime>

datetime

SqlDateTime

DateTime, Nullable<DateTime>

datetime2

Nenhum

DateTime, Nullable<DateTime>

DATETIMEOFFSET

None

DateTimeOffset, Nullable<DateTimeOffset>

decimal

SqlDecimal

Decimal, Nullable<Decimal>

float

SqlDouble

Double, Nullable<Double>

geography

SqlGeography

SqlGeography é definido no Microsoft.SqlServer.Types.dll que é instalado com o SQL Server e pode ser baixado do SQL Server 2008 feature pack (em inglês).

Nenhum

geometry

SqlGeometry

SqlGeometry é definido no Microsoft.SqlServer.Types.dll, que é instalado com o SQL Server e pode ser baixado do pacote de recursos do SQL Server 2008 (em inglês).

Nenhum

hierarchyid

SqlHierarchyId

SqlHierarchyId é definido no Microsoft.SqlServer.Types.dll, que é instalado com o SQL Server e pode ser baixado do pacote de recursos do SQL Server 2008.

Nenhum

image

Nenhum

Nenhum

int

SqlInt32

Int32, Nullable<Int32>

money

SqlMoney

Decimal, Nullable<Decimal>

nchar

SqlChars, SqlString

String, Char[]

ntext

Nenhum

Nenhum

numeric

SqlDecimal

Decimal, Nullable<Decimal>

nvarchar

SqlChars, SqlString

SQLChars é uma melhor correspondência para transferência e acesso a dados, e SQLString é uma melhor correspondência para executar operações de cadeia de caracteres.

String, Char[]

nvarchar(1), nchar(1)

SqlChars, SqlString

Char, String, Char[], Nullable<char>

real

SqlSingle

Single, Nullable<Single>

rowversion

Nenhum

Byte[]

smallint

SqlInt16

Int16, Nullable<Int16>

smallmoney

SqlMoney

Decimal, Nullable<Decimal>

sql_variant

Nenhum

Object

table

Nenhum

Nenhum

text

Nenhum

Nenhum

time

Nenhum

TimeSpan, Nullable<TimeSpan>

timestamp

Nenhum

Nenhum

tinyint

SqlByte

Byte, Nullable<Byte>

uniqueidentifier

SqlGuid

Guid, Nullable<Guid>

User-defined type(UDT)

Nenhum

A mesma classe que está associada ao tipo definido pelo usuário no mesmo assembly ou em um assembly dependente.

varbinary

SqlBytes, SqlBinary

Byte[]

varbinary(1), binary(1)

SqlBytes, SqlBinary

byte, Byte[], Nullable<byte>

varchar

Nenhuma

Nenhuma

xml

SqlXml

Nenhum

Conversão automática de tipo de dados com parâmetros out

Um método CLR pode retornar informações para o código de chamada ou programa marcando um parâmetro input com o modificador out (Microsoft Visual C#) ou <Out()> ByRef (Microsoft Visual Basic). Se o parâmetro input for um tipo de dados CLR no namespace System.Data.SqlTypes, e o programa de chamada especificar seu tipo de dados equivalente do SQL Server como o parâmetro input, uma conversão de tipo ocorrerá automaticamente quando o método CLR retorna o tipo de dados..

Por exemplo, o seguinte procedimento armazenado CLR tem um parâmetro input do tipo de dados CLR SqlInt32 que é marcado com out (C#) ou <Out()> ByRef (Visual Basic):

   [Microsoft.SqlServer.Server.SqlProcedure]
   public static void PriceSum(out SqlInt32 value)
   { … }
    <Microsoft.SqlServer.Server.SqlProcedure> _
    Public Shared Sub PriceSum( <Out()> ByRef value As SqlInt32)
    …
    End Sub

Depois que o assembly é criado no banco de dados, o procedimento armazenado é criado no SQL Server com a seguinte Transact-SQL, que especifica um tipo de dados do SQL Server de int como um parâmetro OUTPUT:

CREATE PROCEDURE PriceSum (@sum int OUTPUT)
AS EXTERNAL NAME TestStoredProc.StoredProcedures.PriceSum

Quando o procedimento armazenado CLR é chamado, o tipo de dados SqlInt32 é automaticamente convertido em um tipo de dados int e retornado para o programa de chamada.

Entretanto, nem todos os tipos de dados CLR podem ser automaticamente convertidos em seus tipos de dados SQL Server equivalentes por um fora parâmetro out. A tabela a seguir lista estas exceções.

Tipo de dados CLR (SQL Server)

Tipo de dados do SQL Server

Decimal

smallmoney

SqlMoney

smallmoney

Decimal

money

DateTime

smalldatetime

SQLDateTime

smalldatetime

Consulte também

Outros recursos