Export (0) Print
Expand All

User-Defined Data Type (UDT) Sample

SQL Server 2005

Updated: 5 December 2005

The UserDefinedDataType sample shows the creation and use of a simple user-defined data type from both Transact-SQL and a client application using System.Data.SqlClient.

The default installation directory is drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\UserDefinedDataType.

A developer wants to represent, persist, and manipulate complex numbers.

Transact-SQL, Visual C# and Visual Basic.

The UserDefinedDataType sample uses the following SQL Server 2005 features:

Application Area Features

Overall

CLR

Before running this sample, make sure the following software is installed:

  • Microsoft SQL Server 2005 or Microsoft SQL Server 2005 Express Edition (SQL Server Express). You can obtain SQL Server Express free of charge from the SQL Server 2005 Express Edition Documentation and Samples Web site.
  • The AdventureWorks database which is included with SQL Server 2005, and is also available at the SQL Server Developer Web site.
  • The SQL Server 2005 Database Engine samples. These samples are included with SQL Server 2005. You can download the latest version of the samples at the SQL Server Developer Web site.
  • .NET Framework SDK 2.0 or Microsoft Visual Studio 2005. You can obtain .NET Framework SDK free of charge. See Installing the .NET Framework SDK.

If you have not already created a strong name key file, generate the key file using the following instructions.

  1. Open a Microsoft Visual Studio 2005 command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

    -- or --

    Open a Microsoft .NET Framework command prompt. Click Start, point to All Programs, point to Microsoft .NET Framework SDK 2.0, and then click SDK Command Prompt.

  2. Use the change directory command (CD) to change the current directory of the command prompt window to the folder where the samples are installed.
    ms160738.note(en-US,SQL.90).gifNote:
    To determine the folder where samples are located, click the Start button, point to All Programs, point to Microsoft SQL Server, point to Documentation and Tutorials, and then click Samples Directory. If the default installation location was used, the samples are located in <system_drive>:\Program Files\Microsoft SQL Server\100\Samples.
  3. At the command prompt, run the following command to generate the key file:

    sn -k SampleKey.snk

    ms160738.note(en-US,SQL.90).gifImportant:
    For more information about the strong-name key pair, see "Security Briefs: Strong Names and Security in the .NET Framework" in the .NET Development Center on MSDN.

To build the sample, do the following:

  1. Compile the sample by using Visual Studio 2005 and the provided Visual Studio solution, or by using Microsoft MSBuild, which is included in the .NET Framework SDK 2.0. Run a command similar to the following at the command prompt:

    msbuild /nologo /verbosity:quiet /property:Configuration=Debug CS\ComplexNumber.sln

  2. Ensure that the AdventureWorks database is installed, or, if necessary, create the database in which you want to install the samples.

  3. Open the scripts\installCS.sql or scripts\installVB.sql file, depending on whether you compiled the Visual C# project or the Visual Basic project, in SQL Server Management Studio. Run the script that is contained in the file, or run a command similar to the following at the command prompt:

    sqlcmd -E -I -i Scripts\InstallCS.sql

    This file contains the script to install and register the SQL CLR UDT sample in SQL Server.

To run the sample, do the following:

  1. Open the scripts\test.sql file in SQL Server Management Studio and run the script that is contained in the file. Or, run the following command at the command prompt:

    sqlcmd -E -I -i Scripts\test.sql

    This script invokes the sample SQL CLR UDT objects from Transact-SQL.

  2. Execute clientsample.exe, specifying the name of the database into which the sample user-defined data type has been installed on the command line. This simple console application invokes the sample user-defined data type by using System.Data.SqlClient.

To remove the sample, do the following:

  1. Open the scripts\cleanup.sql file in SQL Server Management Studio and run the script that is contained in the file. Or, run the following command at the command prompt:

    sqlcmd -E -I -i Scripts\cleanup.sql

    This script removes the UDT sample.

The CLR for SQL Server 2005 or SQL Server Express must be enabled for this sample to work correctly.

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples. Sample applications and assemblies should not be connected to or used with your production SQL Server database or your report server without the permission of the system administrator.

Release History

5 December 2005

Changed content:
  • Changed instructions for generating a key file, including the name and location of the key file.

Community Additions

ADD
Show:
© 2014 Microsoft