大型 UDT

用户定义类型 (UDT) 使开发人员可以通过在 SQL Server 数据库中存储公共语言运行时 (CLR) 对象来扩展服务器的标量类型系统。 UDT 可以包含多个元素,也可以具有多种行为,不同于传统的由单个 SQL Server 系统数据类型组成的别名数据类型。

备注

必须安装 .NET Framework 3.5 SP1(或更高版本)才能利用针对大型 UDT 增强的 SqlClient 支持。

以前 UDT 的最大大小限制为 8 KB。 在 SQL Server 2008 中,对于 UserDefined 格式的 UDT,已不再进行此限制。

有关用户定义类型的完整文档,请参阅 CLR User-Defined 类型

使用 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 注意事项

SqlDataReader 在 SQL Server 2008 中已得到扩展,可支持检索大型 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 属性 说明
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  

请参阅