tailieunhanh - Microsoft SQL Server 2005 Developer’s Guide- Part 13

Microsoft SQL Server 2005 Developer’s Guide- P13:SQL Server 2005 is a feature-rich release that provides a host of new tools and technologies for the database developer. This book is written to help database developers and DBAs become productive immediately with the new features and capabilities found in SQL Server 2005. | Chapter 3 Developing CLR Database Objects 99 User-Defined Types Another important new feature in SQL Server 2005 that is enabled by the integration of the .NET CLR is the ability to create true user-defined types UDTs . Using UDTs you can extend the raw types provided by SQL Server and add data types that are specialized to your application or environment. In the following example you ll see how to create a UDT that represents a gender code either M for male or F for female. While you could store this data in a standard one-byte character field using a UDT ensures that the field will accept only these two values with no additional need for triggers constraints or other data validation techniques. To create a UDT using Visual Studio 2005 select the New Project option give your project a name and click OK to create the project. For this project I used the name of Gender for the new UDT. After naming the project and clicking OK I filled out the New Database Reference dialog using the required connection values to deploy the project to the appropriate SQL Server system and database. Next I used the Project Add User-Defined Type option to display the Add New Item dialog that you can see in Figure 3-11. Figure 3-11 Creating a .NET SQL Server UDT 100 Microsoft SQL Server 2005 Developer s Guide Method Description IsNull This required method is used to indicate if the object is nullable. SQL Server 2005 requires all UDTs to implement nullability so this method must always return true. Parse This required method accepts a string parameter and stores it as a UDT. ToString This required method converts the contents of the UDT to a string. Default constructor This required method creates a new instance of the UDT. Table 3-1 Required UDT Methods Select User-Defined Type from the list of SQL Server templates. Enter the name that you want to assign to the class and then click Open to have Visual Studio generate a starter project file for the UDT. The starter project file .

TỪ KHÓA LIÊN QUAN