已更新:
2008 年 10 月 31 日
下表列出 Microsoft SQL Server 資料類型、它們在 System.Data.SqlTypes 命名空間中 SQL Server 之 Common Language Runtime (CLR) 的對等項目,以及它們在 Microsoft .NET Framework 中的原生 CLR 對等項目。
|
SQL Server 資料類型
|
CLR 資料類型 (SQL Server)
|
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
|
SqlDateTime
|
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
|
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
|

請參閱

變更記錄
|
更新的內容
|
|---|
|
已在對應資料表中加入 SqlGeography、SqlGeometry 和 SqlHierarchyId 類型。
|