语言

大型 UDT

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

备注

若要让客户端访问大型 UDT,请安装 Microsoft.Data.SqlClient NuGet 包并导入 Microsoft.Data.SqlClient。 定义服务器端 UDT 的 SQL CLR 类型仍保留在 Microsoft.SqlServer.Server 中; 不要将这些类型替换为客户端提供程序类型。

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

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

使用 GetSchema 检索 UDT 架构

该 GetSchema 方法返回 DataTable 对象中的数据库架构信息。 有关详细信息,请参阅 SQL Server 架构集合。

UDT 的 GetSchemaTable 列值

该 GetSchemaTable 方法返回 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 值。 读取器处理这些值的方式取决于 SQL Server 版本和连接字符串中的 Type System Version。 有关详细信息,请参阅 ConnectionString。

SQL Server 2005 时Type System Version,以下SqlDataReader方法使用大型 UDT 的二进制表示形式:

对于同一类型系统设置, GetValue 将大型 UDT 值返回为 Byte[]。 GetValues 用列值填充提供的数组,并返回填充的元素数。 该数组中的大型 UDT 值使用 Byte[]。

注意,未对当前版本的 ADO.NET 执行转换。

指定 SqlParameters

以下 SqlParameter 属性支持大型 UDT。

SqlParameter 属性 说明
Value 获取或设置表示参数值的对象。 默认值为 NULL。 此属性可以是 SqlBinary、Byte[] 或托管对象。
SqlValue 获取或设置表示参数值的对象。 默认值为 NULL。 此属性可以是 SqlBinary、Byte[] 或托管对象。
Size 获取或设置要解析的参数值的大小。 默认值为 0。 此属性可以是一个表示参数值大小的整数。 对于大型 UDT,此属性可以是 UDT 的实际大小,也可以是 -1,表示未知。

检索数据示例

下面的代码段演示如何检索大型 UDT 数据。 该connectionString变量假定与 SQL Server 数据库建立有效的连接,并且该commandString变量假定先列出主键列的有效SELECT语句。

using (SqlConnection connection = new SqlConnection(
    connectionString))
{
  connection.Open();
  SqlCommand command = new SqlCommand(commandString, connection);
  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)
    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

请参阅