15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

958 ❘ ChaPTer 34 .net prOGrAmminG with sQl server<br />

user-defined TyPes<br />

User-defined types (UDTs) can be used similarly to normal SQL Server data types to define the type of a<br />

column in a table. With older versions of SQL Server, it was already possible to define UDTs. Of course,<br />

these UDTs could be based only on SQL types, such as the ZIP type shown in the following code. The<br />

stored procedure sp_addtype allows you to create user-defined types. Here the user-defined type ZIP is<br />

based on the CHAR data type with a length of 5. NOT NULL specifies that NULL is not allowed with the ZIP<br />

data type. By using ZIP as a data type, it is no longer necessary to remember that it should be 5 char long<br />

<strong>and</strong> not null:<br />

EXEC sp_addtype ZIP 'CHAR(5)', 'NOT NULL'<br />

With SQL Server 2005 <strong>and</strong> later, UDTs can be defined with CLR classes. However, this feature is not meant<br />

to add object orientation to the database; for example, to create a Person class to have a Person data type.<br />

SQL Server is a relational data store, <strong>and</strong> this is still true with UDTs. You cannot create a class hierarchy<br />

of UDTs, <strong>and</strong> it is not possible to reference fields or properties of a UDT type with a SELECT statement. If<br />

properties of a person (for example, FirstName or LastName) must be accessed or a list of Person objects<br />

must be sorted (for example, by FirstName or LastName), it is still better to define columns for first name<br />

or last name inside a Persons table or to use the XML data type.<br />

UDTs are meant for very simple data types. Before .<strong>NET</strong>, it was also possible to create custom data types;<br />

for example, the ZIP data type. With UDTs it is not possible to create a class hierarchy, <strong>and</strong> they are not<br />

meant to get complex data types to the database. One requirement of a UDT is that it must be convertible to<br />

a string, because the string representation is used to display the value.<br />

How the data is stored within SQL Server can be defined: either an automatic mechanism can be used to<br />

store the data in a native format, or you can convert the data to a byte stream to define how the data<br />

should be stored.<br />

Creating udTs<br />

In this example, you create a SqlCoordinate type representing the world coordinates longitude <strong>and</strong> latitude<br />

for easily defining the location of places, cities, <strong>and</strong> the like. To create CLR objects with Visual Studio,<br />

create a new Visual <strong>C#</strong> SQL CLR Database Project in the category Database Projects ➪ SQL Server, <strong>and</strong><br />

add a UDT by using the User-Defined Type template from the Solution Explorer. The name of the UDT<br />

should be SqlCoordinate.<br />

With the template, the base functionality of a custom type is already defined:<br />

using System;<br />

using System.Data;<br />

using System.Data.SqlClient;<br />

using System.Data.SqlTypes;<br />

using Microsoft.SqlServer.Server;<br />

[Serializable]<br />

[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.Native)]<br />

public struct SqlCoordinate: INullable<br />

{<br />

public override string ToString()<br />

{<br />

// Replace the following code with your code<br />

return "";<br />

}<br />

public bool IsNull<br />

{<br />

get<br />

{<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!