適用於:SQL Server
下表列出 SQL Server 數據類型、其在 System.Data.SqlTypes 命名空間中 SQL Server 的 Common Language Runtime (CLR) 對等專案,以及 .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> |
| 地理位置 |
SqlGeography
1 |
無 |
| 幾何 |
SqlGeometry
1 |
無 |
| hierarchyid |
SqlHierarchyId
1 |
無 |
| image | 無 | 無 |
| int | SqlInt32 |
Int32、Nullable<Int32> |
| money | SqlMoney |
Decimal、Nullable<Decimal> |
| nchar |
SqlChars、SqlString |
String、Char[] |
| ntext | 無 | 無 |
| numeric | SqlDecimal |
Decimal、Nullable<Decimal> |
| nvarchar |
SqlChars、SqlStringSQLChars 是數據傳輸和存取的較佳比對,SQLString 較適合執行 String 作業。 |
String、Char[] |
| nvarchar(1), nchar(1) |
SqlChars、SqlString |
Char、String、Char[]、Nullable<char> |
| real |
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> |
| 使用者定義型別 (UDT) | 無 | 系結至相同元件或相依元件中用戶定義型別的相同類別。 |
| varbinary |
SqlBytes、SqlBinary |
Byte[] |
| varbinary(1), 二進位(1) |
SqlBytes、SqlBinary |
byte、Byte[]、Nullable<byte> |
| varchar | 無 | 無 |
| xml | SqlXml |
無 |
1 定義於 Microsoft.SqlServer.Types.dll中,它會隨 SQL Server 一起安裝,而且可以從 SQL Server 功能套件下載。
使用輸出參數自動轉換數據類型
CLR 方法可以藉由使用 out 修飾詞 (C#) 或 <Out()> ByRef 標記輸入參數,將資訊傳回呼叫程式代碼或程式式。。 如果輸入參數是 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)
{ ... }
在資料庫中建置並建立元件之後,預存程式會使用下列 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 |
相關內容
- 在 .NET Framework 中
SQL Server 數據類型