大型 UDT

使用者定義類型 (UDT) 可讓開發人員將 Common Language Runtime (CLR) 物件儲存在 SQL Server 資料庫中來擴充伺服器的純量類型系統。 UDT 可包含多個項目並可具有不同的行為,與只含單一 SQL Server 系統資料類型的傳統別名資料類型有所不同。

注意

您必須安裝 .NET Framework 3.5 SP1 (或更新版本) 才能運用大型 UDT 的強化 SqlClient 支援。

在過去,UDT 的大小上限為 8 KB。 在 SQL Server 2008 中,目前使用 UserDefined 格式的 UDT 已不再具有這項限制。

如需使用者定義型別的完整文件,請參閱 CLR 使用者定義型別

使用 GetSchema 來擷取 UDT 結構描述

SqlConnectionGetSchema 方法會傳回 DataTable 中的資料庫結構描述資訊。 如需詳細資訊,請參閱 SQL Server 結構描述集合

UDT 的 GetSchemaTable 資料行值

SqlDataReaderGetSchemaTable 方法會傳回描述資料行中繼資料的 DataTable。 下表描述 SQL Server 2005 與 SQL Server 2008 之間大型 UDT 之資料行中繼資料中的差異。

SqlDataReader 資料行 SQL Server 2005 SQL Server 2008 及更新版本
ColumnSize 不定 不一定
NumericPrecision 255 255
NumericScale 255 255
DataType Byte[] UDT 執行個體
ProviderSpecificDataType SqlTypes.SqlBinary UDT 執行個體
ProviderType 21 (SqlDbType.VarBinary) 29 (SqlDbType.Udt)
NonVersionedProviderType 29 (SqlDbType.Udt) 29 (SqlDbType.Udt)
DataTypeName SqlDbType.VarBinary 指定為 Database.SchemaName.TypeName 的三部分名稱。
IsLong 不定 不一定

SqlDataReader 考量

從 SQL Server 2008 開始,SqlDataReader 已擴充,可支援大型 UDT 值的擷取。 SqlDataReader 處理大型 UDT 值的方式,取決於您所使用的 SQL Server 版本,以及連接字串中指定的 Type System Version。 如需詳細資訊,請參閱ConnectionString

Type System Version 設定為 SQL Server 2005 時,下列 SqlDataReader 的方法將會傳回 SqlBinary,而不是 UDT:

Type System Version 設定為 SQL Server 2005 時,下列方法將會傳回 Byte[] (而非 UDT) 的陣列:

請注意,不會針對目前的 ADO.NET 版本進行任何轉換。

指定 SqlParameters

下列 SqlParameter 屬性已經擴充,以便與大型 UDT 搭配運作。

SqlParameter 屬性 Description
Value 取得或設定代表參數值的物件。 預設值是 null。 屬性可以是 SqlBinaryByte[] 或受控物件。
SqlValue 取得或設定代表參數值的物件。 預設值是 null。 屬性可以是 SqlBinaryByte[] 或受控物件。
Size 取得或設定要解析的參數值大小。 預設值為 0。 屬性可以是代表參數值大小的整數。 對於大型 UDT 而言,這可能是 UDT 的實際大小,-1 則代表未知。

擷取資料範例

下列程式碼片段示範如何擷取大型 UDT 資料。 connectionString 變數會假設已與 SQL Server 資料庫建立有效連線,而 commandString 變數會假設先列出具備主索引鍵資料行的有效 SELECT 陳述式。

using (SqlConnection connection = new SqlConnection(
    connectionString, commandString))  
{  
  connection.Open();  
  SqlCommand command = new SqlCommand(commandString);  
  SqlDataReader reader = command.ExecuteReader();  
  while (reader.Read())  
  {  
    // Retrieve the value of the Primary Key column.  
    int id = reader.GetInt32(0);  
  
    // Retrieve the value of the UDT.  
    LargeUDT udt = (LargeUDT)reader[1];  
  
    // You can also use GetSqlValue and GetValue.  
    // LargeUDT udt = (LargeUDT)reader.GetSqlValue(1);  
    // LargeUDT udt = (LargeUDT)reader.GetValue(1);  
  
    Console.WriteLine(  
     "ID={0} LargeUDT={1}", id, udt);  
  }  
reader.close  
}  
Using connection As New SqlConnection( _  
    connectionString, commandString)  
    connection.Open()  
    Dim command As New SqlCommand(commandString, connection)  
    Dim reader As SqlDataReader  
    reader = command.ExecuteReader  
  
    While reader.Read()  
      ' Retrieve the value of the Primary Key column.  
      Dim id As Int32 = reader.GetInt32(0)  
  
      ' Retrieve the value of the UDT.  
      Dim udt As LargeUDT = CType(reader(1), LargeUDT)  
  
     ' You can also use GetSqlValue and GetValue.  
     ' Dim udt As LargeUDT = CType(reader.GetSqlValue(1), LargeUDT)  
     ' Dim udt As LargeUDT = CType(reader.GetValue(1), LargeUDT)  
  
      ' Print values.  
      Console.WriteLine("ID={0} LargeUDT={1}", id, udt)  
    End While  
    reader.Close()  
End Using  

另請參閱