Large UDTs

 

User-defined types (UDTs) allow a developer to extend the server's scalar type system by storing common language runtime (CLR) objects in a SQL Server database. UDTs can contain multiple elements and can have behaviors, unlike the traditional alias data types, which consist of a single SQL Server system data type.

System_CAPS_ICON_note.jpg Note

You must install the .NET Framework 3.5 SP1 (or later) to take advantage of the enhanced SqlClient support for large UDTs.

Previously, UDTs were restricted to a maximum size of 8 kilobytes. In SQL Server 2008, this restriction has been removed for UDTs that have a format of UserDefined.

For the complete documentation for user-defined types, see the version of SQL Server Books Online for the version of SQL Server you are using.

SQL Server Books Online

  1. CLR User-Defined Types

The GetSchema method of SqlConnection returns database schema information in a DataTable. For more information, see SQL Server Schema Collections.

GetSchemaTable Column Values for UDTs

The GetSchemaTable method of a SqlDataReader returns a DataTable that describes column metadata. The following table describes the differences in the column metadata for large UDTs between SQL Server 2005 and SQL Server 2008.

SqlDataReader columnSQL Server 2005SQL Server 2008 and later
ColumnSizeVariesVaries
NumericPrecision255255
NumericScale255255
DataTypeByte[]UDT instance
ProviderSpecificDataTypeSqlTypes.SqlBinaryUDT instance
ProviderType21 (SqlDbType.VarBinary)29 (SqlDbType.Udt)
NonVersionedProviderType29 (SqlDbType.Udt)29 (SqlDbType.Udt)
DataTypeNameSqlDbType.VarBinaryThe three part name specified as Database.SchemaName.TypeName.
IsLongVariesVaries

The SqlDataReader has been extended beginning in SQL Server 2008 to support retrieving large UDT values. How large UDT values are processed by a SqlDataReader depends on the version of SQL Server you are using, as well as on the Type System Version specified in the connection string. For more information, see ConnectionString.

The following methods of SqlDataReader will return a SqlBinary instead of a UDT when the Type System Version is set to SQL Server 2005:

The following methods will return an array of Byte[] instead of a UDT when the Type System Version is set to SQL Server 2005:

Note that no conversions are made for the current version of ADO.NET.

The following SqlParameter properties have been extended to work with large UDTs.

SqlParameter PropertyDescription
ValueGets or sets an object that represents the value of the parameter. The default is null. The property can be SqlBinary, Byte[], or a managed object.
SqlValueGets or sets an object that represents the value of the parameter. The default is null. The property can be SqlBinary, Byte[], or a managed object.
SizeGets or sets the size of the parameter value to resolve. The default value is 0. The property can be an integer that represents the size of the parameter value. For large UDTs, it can be the actual size of the UDT, or -1 for unknown.

The following code fragment demonstrates how to retrieve large UDT data. The connectionString variable assumes a valid connection to a SQL Server database and the commandString variable assumes a valid SELECT statement with the primary key column listed first.

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  
}  

Configuring Parameters and Parameter Data Types
Retrieving Database Schema Information
SQL Server Data Type Mappings
SQL Server Binary and Large-Value Data
ADO.NET Managed Providers and DataSet Developer Center

Show: