Share via


對應 CLR 參數資料

下表列出 Microsoft SQL Server 資料類型、它們在 System.Data.SqlTypes 命名空間中 SQL Server 之 Common Language Runtime (CLR) 的對等項目,以及它們在 Microsoft .NET Framework 中的原生 CLR 對等項目。

SQL Server 資料類型

類型 (在 System.Data.SqlTypes 或 Microsoft.SqlServer.Types 中)

CLR 資料類型 (.NET Framework)

bigint

SqlInt64

Int64, Nullable<Int64>

binary

SqlBytes, SqlBinary

Byte[]

bit

SqlBoolean

Boolean, Nullable<Boolean>

char

cursor

date

SqlDateTime

DateTime, Nullable<DateTime>

datetime

SqlDateTime

DateTime, Nullable<DateTime>

datetime2

DateTime, Nullable<DateTime>

DATETIMEOFFSET

None

DateTimeOffset, Nullable<DateTimeOffset>

decimal

SqlDecimal

Decimal, Nullable<Decimal>

float

SqlDouble

Double, Nullable<Double>

geography

SqlGeography

SqlGeography 定義於 Microsoft.SqlServer.Types.dll 中,它會隨 SQL Server 一起安裝,且可從 SQL Server 2008 功能套件下載。

geometry

SqlGeometry

SqlGeometry 定義於 Microsoft.SqlServer.Types.dll 中,會隨 SQL Server 一起安裝,且可從 SQL Server 2008 功能套件下載。

hierarchyid

SqlHierarchyId

SqlHierarchyId 定義於 Microsoft.SqlServer.Types.dll 中,會隨 SQL Server 一起安裝,且可從 SQL Server 2008 功能套件下載。

image

int

SqlInt32

Int32, Nullable<Int32>

money

SqlMoney

Decimal, Nullable<Decimal>

nchar

SqlChars, SqlString

String, Char[]

ntext

numeric

SqlDecimal

Decimal, Nullable<Decimal>

nvarchar

SqlChars, SqlString

SQLChars 比較適合資料傳輸和存取,而 SQLString 比較適合執行 String 作業。

String, Char[]

nvarchar(1), nchar(1)

SqlChars, SqlString

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

real

SqlSingle

Single, Nullable<Single>

rowversion

Byte[]

smallint

SqlInt16

Int16, Nullable<Int16>

smallmoney

SqlMoney

Decimal, Nullable<Decimal>

sql_variant

Object

table

text

time

TimeSpan, Nullable<TimeSpan>

timestamp

tinyint

SqlByte

Byte, Nullable<Byte>

uniqueidentifier

SqlGuid

Guid, Nullable<Guid>

User-defined type(UDT)

繫結到相同組件或相依組件中之使用者定義型別的相同類別。

varbinary

SqlBytes, SqlBinary

Byte[]

varbinary(1), binary(1)

SqlBytes, SqlBinary

byte, Byte[], Nullable<byte>

varchar

xml

SqlXml

利用 Out 參數自動轉換資料類型

CLR 方法可以利用 out 修飾詞 (Microsoft Visual C#) 或 <Out()> ByRef (Microsoft Visual Basic) 標示輸入參數,以便將資訊傳回到呼叫程式碼或程式。如果輸入參數在 System.Data.SqlTypes 命名空間中為 CLR 資料類型,而且呼叫程式會將其對等的 SQL Server 資料類型指定為輸入參數,當 CLR 方法傳回資料類型時,會自動進行類型轉換。

例如,下列 CLR 預存程序所擁有的 SqlInt32 CLR 資料類型輸入參數會以 out (C#) 或 <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

在資料庫中建置與建立組件之後,就會使用下列 Transact-SQL,在 SQL Server 中建立預存程序,這會將 int 的 SQL Server 資料類型指定為 OUTPUT 參數:

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

呼叫 CLR 預存程序時,SqlInt32 資料類型會自動轉換為 int 資料類型,並傳回到呼叫程式。

不過,並非所有 CLR 資料類型都可以透過 out 參數,自動轉換為其對等的 SQL Server 資料類型。下表列出這些例外。

CLR 資料類型 (SQL Server)

SQL Server 資料類型

Decimal

smallmoney

SqlMoney

smallmoney

Decimal

money

DateTime

smalldatetime

SQLDateTime

smalldatetime