共用方式為


對應 CLR 參數數據

適用於:SQL Server

下表列出 SQL Server 數據類型、其在 System.Data.SqlTypes 命名空間中 SQL Server 的 Common Language Runtime (CLR) 對等專案,以及 .NET Framework 中的原生 CLR 對等專案。

SQL Server 資料類型 類型 (System.Data.SqlTypesMicrosoft.SqlServer.Types CLR 資料類型 (.NET Framework)
bigint SqlInt64 Int64Nullable<Int64>
binary SqlBytesSqlBinary Byte[]
bit SqlBoolean BooleanNullable<Boolean>
char
cursor
date SqlDateTime DateTimeNullable<DateTime>
datetime SqlDateTime DateTimeNullable<DateTime>
datetime2 DateTimeNullable<DateTime>
datetimeoffset None DateTimeOffsetNullable<DateTimeOffset>
decimal SqlDecimal DecimalNullable<Decimal>
float SqlDouble DoubleNullable<Double>
地理位置 SqlGeography 1
幾何 SqlGeometry 1
hierarchyid SqlHierarchyId 1
image
int SqlInt32 Int32Nullable<Int32>
money SqlMoney DecimalNullable<Decimal>
nchar SqlCharsSqlString StringChar[]
ntext
numeric SqlDecimal DecimalNullable<Decimal>
nvarchar SqlCharsSqlString

SQLChars 是數據傳輸和存取的較佳比對,SQLString 較適合執行 String 作業。
StringChar[]
nvarchar(1)nchar(1) SqlCharsSqlString CharStringChar[]Nullable<char>
real (不過, 的範圍大於實際 SingleNullable<Single>
rowversion Byte[]
smallint SqlInt16 Int16Nullable<Int16>
smallmoney SqlMoney DecimalNullable<Decimal>
sql_variant Object
table
text
time TimeSpanNullable<TimeSpan>
timestamp
tinyint SqlByte ByteNullable<Byte>
uniqueidentifier SqlGuid GuidNullable<Guid>
使用者定義型別 (UDT) 系結至相同元件或相依元件中用戶定義型別的相同類別。
varbinary SqlBytesSqlBinary Byte[]
varbinary(1)二進位(1) SqlBytesSqlBinary byteByte[]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 中建立,它會將 intSQL 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 數據類型